How to ping and shutdown a remote computer?

Tim Golden mail at timgolden.me.uk
Wed Feb 14 11:31:40 EST 2007


joja15 at gmail.com wrote:
> Here is my current setup:
> 
 > [... BSD ...]
> - Windows XP machine with folder share (What packet is sent over the
> network to remotely shutdown a Windows XP machine?)
> 
> My hope is to have a script then when you start it will list all your
> remote computers/servers and show if they are currently on/off. Then
> you can select a server and turn it off if it is on or turn it on if
> it is off.

Couple of bits of info, speaking only about Windows. First, I'd
be quite worried if someone could send me a packet (maliciously
or otherwise) which simply shut my machine down. Is this possible?
Second, machines -- or networks -- may be configured to reject
or swallow pings so the lack of a ping may not indicate vitality.

Since you specify that the machine has a folder share, that means
it's running SMB/NMB/whatever it's called across a few well-known
ports, including 135 and 137-139 and 445. So you could attempt a
socket connection to one of those:

<code>
import socket
s = socket.socket ()
s.settimeout (0.25)
try:
   s.connect (("192.168.100.84", 135))
except socket.error:
   print "not alive"
else:
   print "alive"

</code>

To shut it down, someone has already suggested the
shutdown command, although I think you'd have to
specify the -m param to pass the remote machine name.
Alternatively, you could use WMI (which inadvertently
provides a means of determining vitality):

http://timgolden.me.uk/python/wmi_cookbook.html#reboot_remote_machine

(adapted a bit, but you get the idea)

TJG



More information about the Python-list mailing list