Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

How to ping and shutdown a remote computer?


I am working on a Python script to perform as a remote computer
manager. So far I have a WOL function working and I would like to add
the ability to show if a machine is on or off (I figured I would do so
by pinging the machine and seeing if I get a response). I would also
like to add the ability to remotely shutdown a computer from the
python script. Does anyone have a code snippet for pinging an IP, a
code snippet for shutting down a remote Windows XP machine, and a code
snippet for sending a HTTP request?

Here is my current setup:

- PC  running python script
- FreeNAS (media server running on FreeBSD. Can be shutdown from web
interface so I planned on just sending that same web button click from
the python script to shutdown the FreeNAS server)
- 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.

Thank you in advance for any help provided.

- John

joj@gmail.com a crit :

import os
os.system("shutdown -s -f")
Try other switches if you want. Requires Windows XP at the minimum.

joj@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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc