Click here to Skip to main content
1,823 members
Articles / Security / .NET 1.1
Article

Remote Shutdown or Reboot with Telnet and C#

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
10 Apr 2012 18.3K   32   1
Reboot a computer remotely with Telnet on port 50000, written in C#.

This article is a sponsored article. Articles such as these are intended to provide you with information on products and services that we consider useful and of value to developers

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


Written By
Web Developer
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
GeneralShutdown Pin
Member 87313-Dec-13 7:30
Member 87313-Dec-13 7:30 
Hy,
sorry that i ask, you have made that a long time ago.
I actually try to shutdown my Windows 8.1 but i got only a reboot.

I was wondering how the shutdown/poweroff work, since i always got a reboot only.

thought "DoExitWin(EWX_POWEROFF + EWX_FORCE);" would power off the System but it just restarts.

Do you have a tip for me how to shut it down?

And: installutil from Framework 4 work to install as a Service too

tnx
red

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.