How to ping and shutdown a remote computer?

J

joja15

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
 
C

Christophe

(e-mail address removed) a écrit :
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?)

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

Tim Golden

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
 
J

joja15

(e-mail address removed) a écrit :






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

That is a good idea but I don't have Windows XP on the machine
performing the shutdown. I should have been more detailed. I said PC
but what it is is a Xbox running XBMC: http://www.xboxmediacenter.com

I should have been more detailed in that.

- John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top