Click here to Skip to main content
6,596,602 members and growing! (19,883 online)
Email Password   helpLost your password?
General Reading » Hardware & System » System     Intermediate

Remote Shutdown or Reboot with Telnet and C#

By Paul J Barrett

Reboot a computer remotely with Telnet on port 50000, written in C#.
C#.NET 1.0, .NET 1.1, Win2K, WinXPVS.NET2003, Dev
Posted:21 Oct 2003
Updated:17 Oct 2004
Views:97,687
Bookmarked:48 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
14 votes for this article.
Popularity: 3.15 Rating: 2.75 out of 5
5 votes, 35.7%
1
1 vote, 7.1%
2
4 votes, 28.6%
3
3 votes, 21.4%
4
1 vote, 7.1%
5

Introduction

I frequently shutdown services on my computers when working on them and forget to restart them when I am done. It occurred to me that it would be great to be able to secure shell to my Linux system behind my firewall and then telnet on a port to my NT and XP boxes to make them reboot.

Being far more efficient with C#, I decided this would be a great way to learn about creating a Windows service. So I was off and running. The service would require three basic functions: the Windows service itself, the ability to reboot or shutdown the computer, and a listening socket on a thread.

I therefore started with a little quick web search and found the code to restart the computer for C# and wrote a quick test program for it.

Next I looked up how to listen on a socket and read a line with the StreamReader. After testing this little bit of code, it was obvious that it would have to be on a thread so that control could be returned to the windows service. Having tested the parts of my project I then put them all together into one windows service project and added the installer.

This is a new and improved version of the first RemoteShutdown that I created. It now has a configuration file, SHA1 hashed password in the configuration file, and a utility to create the configuration file. The source code for the makeconfig.exe is makeconfig.cs and is compiled at the command prompt with "csc /t:exe makeconfig.cs".

One thing I noticed is that if you have both 1.0 and 1.1 and 2.0 of .net installed make sure you use the right version of InstallUtil. The binary in release directory is compiled with VS.net 2003 so you will need to use installutil.exe version 1.1....

Please leave comments so I can find out how useful this program is. I noticed from my private website it had been downloaded over 9000 times. So let me know how it has saved your bacon.

Using the code

  1. Unzip the project into your Visual Studio Projects directory.
  2. From the command prompt (InstallUtil must be version 1.1...) in the release directory, type Installutil RemoteShutdown.exe
  3. Run Makeconfig.exe in the release directory.
  4. Enter your desired password and port.
  5. Then rename the rename RemoteShutdown.exe.config.new to RemoteShutdown.exe.config
  6. Reboot the computer
  7. Goto Administrative Tools/Services and check that the new RemoteShutdown service has started.
  8. To test from the command prompt, type telnet localhost 50000
    type "your password" and the computer will reboot.
  9. To uninstall the service from the release directory type Installutil RemoteShutdown.exe /u

Points of interest

This project demonstrates several interesting capabilities:

  • Reading from configuration file.
  • Calling the Win API to reboot of the computer.
  • SHA1 Hashing of password.
  • Windows Service program.
  • Listening on a port.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Paul J Barrett


Member
I have been programming since 1983 when I first programmed on a HeathKit 8088 Breadboard Computer in machine code. Since then I have dabbled with many things. I am extremely fluent with VB6, Access, Sql Server. I am currently programming a major project in C#.

I am also a system administrator for Unix, Linux, MS 4.0 - 2003 and also dabble with CISCO routers and firewalls.
Occupation: Web Developer
Location: United States United States

Other popular Hardware & System articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralCommand line? PinsussAnonymous15:47 14 Dec '04  
GeneralRe: Command line? PinmemberPaul J Barrett7:01 6 Oct '05  
Generalweb service? PinsussAnonymous10:41 2 Sep '04  
GeneralRe: web service? PinmemberPaul J Barrett7:04 6 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Oct 2004
Editor: Nishant Sivakumar
Copyright 2003 by Paul J Barrett
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project