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
- Unzip the project into your Visual Studio Projects directory.
- From the command prompt (InstallUtil must be version 1.1...) in the release directory, type Installutil
RemoteShutdown.exe
- Run Makeconfig.exe in the release directory.
- Enter your desired password and port.
- Then rename the rename RemoteShutdown.exe.config.new to
RemoteShutdown.exe.config
- Reboot the computer
- Goto Administrative Tools/Services and check that the new RemoteShutdown service has started.
- To test from the command prompt, type telnet localhost 50000
type "your password" and the computer will reboot.
- 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.
This member doesn't quite have enough reputation to be able to display their biography and homepage.