View Full Version : Telling if a client is up.
Benny
09-09-2002, 11:46 AM
I'm was bored and I whipped together this crude little script that takes a list of hosts and reports back on if the host is avaliable.
The way, I've done it is a very crappy, quick hack and system dependent (uses the windows ping utility)....
Here is what the script looks like:
import os, string, time
hostFile = open("hosts.txt", 'r')
hostList = hostFile.readlines()
hostFile.close()
x = -1
for line in hostList:
x+=1
line = string.split(line, " ")
y = -1
for item in line:
y+=1
line[y] = string.strip(item)
hostList[x] = line
while 1:
os.system("cls")
print "%-12s | %-18s ==> %5s" % ("Host Title", "Host Name", "Details")
print "--------------------------------------------------------"
for host in hostList:
hostName = host[1]
hostTitle = host[0]
print "%-12s | %-18s ==> " % (hostTitle, hostName),
ping = os.popen("ping -n 2 -w 1 "+hostName)
data = ping.readlines()
if data[len(data)-1][-4:] == "ms\r\n":
print "%10s" % "Client Reachable"
else:
print "%10s" % "Client Unreachable"
print "\nSleeping for 30 seconds....."
time.sleep(30)
os.system("cls")
Here is what the output would look like.
Host Title | Host Name ==> Details
--------------------------------------------------------
Marlo | marlolake.com.au ==> Client Reachable
Dave | 210.xx.xxx.xxx ==> Client Reachable
Yahoo | yahoo.com ==> Client Reachable
Sleeping for 30 seconds.....
My question is - how can I make this more system independent and more reliable. As in, what other options do I have rather then "ping" to check if a client is up? Also how can I do a system independednt "clear screen"?
Cheers..
Strike
09-09-2002, 12:24 PM
You can try and open a socket on a given port, but that is a different sort of thing entirely. I'm not sure how to send ICMP packets with Python, truth be told. But if you want to search on info to do so, that's an acronym you should look for :)
http://pynms.sourceforge.net/
try that
inkedmn
09-09-2002, 01:15 PM
this'd work (and it's much shorter):
import socket
# get your list of hosts the same way you did
for host in hostlist:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 23)) # or whatever port
s.close()
print "connected to", host
that's not tested (or really even done right, honestly), but you get the idea...
Strike
09-09-2002, 01:33 PM
Originally posted by inkedmn
this'd work (and it's much shorter):
import socket
# get your list of hosts the same way you did
for host in hostlist:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 23)) # or whatever port
s.close()
print "connected to", host
that's not tested (or really even done right, honestly), but you get the idea...
That's different than pinging a host, though. That test would fail on my machine where pinging my machine would succeed.
inkedmn
09-09-2002, 01:36 PM
why would it fail? (assuming you had TCP 23 open - which you probably don't :)). but, if he's just testing a list of network hosts, i think my code would work just fine. the reason being, it's going to return a "connection refused" on just about every machine, which means it's up.
if he's just trying to hack something together, i don't see why mine wouldn't work fine.
seriously, look at my link. it's got a python module with ping already in it.
Strike
09-09-2002, 09:14 PM
Originally posted by inkedmn
why would it fail? (assuming you had TCP 23 open - which you probably don't :)). but, if he's just testing a list of network hosts, i think my code would work just fine. the reason being, it's going to return a "connection refused" on just about every machine, which means it's up.
if he's just trying to hack something together, i don't see why mine wouldn't work fine.
Right, port 23 isn't open, that's why. :)
kmj's stuff does have a ping module, but it doesn't seem very straightforward to use or really any better than the method he is using (it requires a DLL to be installed, I think).
GnuVince
09-09-2002, 10:05 PM
How about this?:
require 'ping'
if Ping.pingecho('192.168.1.1', 3)
puts "Alive"
else
puts "Dead"
end
Benny
09-09-2002, 10:51 PM
Originally posted by Strike
Right, port 23 isn't open, that's why. :)
kmj's stuff does have a ping module, but it doesn't seem very straightforward to use or really any better than the method he is using (it requires a DLL to be installed, I think).
Yeh I'd looked at the stuff kmj mentioned and it seemed like a pain, ideally I'd like to be able to do it without having to use a third party module. But it's not looking like I'm going to be able to....
GnuVince: Whats that?
Inkedmn: I see what your suggesting and I agree something like that could work - using a couple of try: and excepts:, however it is damn slow, as you have to wait for the connection to either time out (if the client doesn't exist) or be denied (if it does exist). It is probably just as cumbersome as what I am currently doing, the only advantage is that it doesn't rely on an external app.
Thanks for the suggestions so far guys, I dont understand why ping would be so hard, Perl has Net::Ping....
Also, any ideas about clearing the screen, would I have to look into curses for that?
Thanks.
inkedmn
09-09-2002, 11:03 PM
try:
os.system('cls')
except:
os.system('clear')
that should do it.
Benny
09-09-2002, 11:06 PM
Going to look at the "Ping and Traceroute" package from this site:
http://www.python.org/~jeremy/python.html
Seems simpler then the pynms stuff kmj mentioned.
Benny
09-09-2002, 11:08 PM
Originally posted by inkedmn
try:
os.system('cls')
except:
os.system('clear')
that should do it.
Yeh it would, but that still isn't foolproof....thought there might be a way to clear the screen, without the use of cls and clear. But that method would do.
Thanks Inkedmn.
GnuVince
09-09-2002, 11:42 PM
See if the value of data is different on windows:
>>> import os
>>> data = os.popen('clear').read()
>>> data
'\x1b[H\x1b[2J'
>>>
doing print data then clears the screen.
jemfinch
09-10-2002, 02:11 AM
Don't clear the screen at all, there's no reason. If the user wants to clear the screen, he can do it himself.
If you were running in *nix, I'd say to accept the host.txt file on stdin, since you can just "./pythonscript.py < hosts.txt" but you can also generate your hosts.txt equivalent some other way.
You should use string methods, not the string module: "line.split(' ')" instead of "string.split(line, ' ')". And unless you want a needlessly strict config file, just use "line.split()" without specifying the separator -- it's split on multiple spaces, too.
This:
for host in hostList:
hostName = host[1]
hostTitle = host[0]
should be this:
for hostTitle, hostName in hostList:
The idiom you have here:
x = -1
for line in hostList:
x += 1
should be:
for x in range(len(hostList)):
line = hostList[x]
Your imports should be on separate lines.
That's all Jeremy "Anal Python Style Man" Fincher has to say for today :)
Jeremy
Benny
09-10-2002, 03:38 AM
Originally posted by jemfinch
Don't clear the screen at all, there's no reason. If the user wants to clear the screen, he can do it himself.
True true, I'm just big on snazzy output, plus was curious....
If you were running in *nix, I'd say to accept the host.txt file on stdin, since you can just "./pythonscript.py < hosts.txt" but you can also generate your hosts.txt equivalent some other way.
You should use string methods, not the string module: "line.split(' ')" instead of "string.split(line, ' ')". And unless you want a needlessly strict config file, just use "line.split()" without specifying the separator -- it's split on multiple spaces, too.
Ahh cool, forgot I could use the string methods. Cheers.
This:
for host in hostList:
hostName = host[1]
hostTitle = host[0]
should be this:
for hostTitle, hostName in hostList:
Neato, didn't realise I could do this.
The idiom you have here:
x = -1
for line in hostList:
x += 1
should be:
for x in range(len(hostList)):
line = hostList[x]
Yeh - I always forget to do that, bad habits are hard to shake I guess.
Your imports should be on separate lines.
Any reason other then looks, readability of the code? Just curious to know.
That's all Jeremy "Anal Python Style Man" Fincher has to say for today :)
Jeremy
Tis all good, helpful critisim is always welcome, god knows I need it. hehe I suck when it come to writing efficient/neat code, so any tips I can get, I'll readily accept.
Cheers.
jemfinch
09-10-2002, 05:55 AM
[QUOTE]Originally posted by Benny
[Any reason other then looks, readability of the code? Just curious to know.
[/code]
Readability is of the utmost importance, of course :)
But even so, the main reasons to put them on separate lines is that it's standard Python style to put them on separate lines. It also lets you comment out or remove an import more easily than if they're imported on one line.
Jeremy
Benny
09-10-2002, 09:15 AM
Originally posted by jemfinch
Readability is of the utmost importance, of course :)
But even so, the main reasons to put them on separate lines is that it's standard Python style to put them on separate lines. It also lets you comment out or remove an import more easily than if they're imported on one line.
Jeremy [/B]
Yep fair enough, that all makes sense.
Cheers.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.