-1

Basically I am writing a script in Python that will check for me the status of my servers. I need it to ping five different IPs and then detect what the output is. I will then need an if statement for example:

import.os
Server1 = os.system('ping 123.123.123.123')
if Server1 == 'Request timed out':
     print('Server 1 is down.')
else:
     print('Server 1 is up.')

I am not exactly sure how to go about this. Let me know.

Thanks.

oreid
  • 1,336
  • 2
  • 13
  • 25
  • 2
    possible duplicate of [Pinging servers in Python](http://stackoverflow.com/questions/2953462/pinging-servers-in-python) – lvc Apr 12 '14 at 01:39

1 Answers1

0

You posted big X/Y here. You want do do X. You don't know how to do X. You think Y is the best way to do X and are asking about Y. If you want to know about X, see Ivc's comment.

I'm going to answer Y but letting you know that Y is not the best way to do it.

import subprocess
import locale

encoding = locale.getdefaultlocale()[1]
proc = subprocess.Popen(["ping", "123.123.123.123"], stdout=subprocess.PIPE)
out = proc.communicate()[0]
if 'Request timed out' in out.decode(encoding):
    print 'the host is down'
else: 
    print 'the host is up'

Then you would need to decide exactly what to do when the host is down and when the host is up.

Community
  • 1
  • 1
carlosdc
  • 12,022
  • 4
  • 45
  • 62
  • This is working well. How would I go about parsing it though? – oreid Apr 12 '14 at 02:58
  • @user2934716 it depends on the output. Please update the question the output ping gives you when successful and when unsuccessful. – carlosdc Apr 12 '14 at 04:59
  • This output would mean the server is up: b'\r\nPinging 123.123.123.123 with 32 bytes of data:\r\nReply from 123.123.123.123: bytes=32 time=230ms TTL=44\r\nReply from 123.123.123.123: bytes=32 time=230ms TTL=44\r\nReply from 123.123.123.123: bytes=32 time=233ms TTL=44\r\nReply from 123.123.132.123: bytes=32 time=233ms TTL=44\r\n\r\nPing statistics for 123.123.123.123:\r\n Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),\r\nApproximate round trip times in milli-seconds:\r\n Minimum = 230ms, Maximum = 233ms, Average = 231ms\r\n' (Second part in next comment) – oreid Apr 12 '14 at 06:05
  • This would mean the server is down.

    b'\r\nPinging 123.123.123.123 with 32 bytes of data:\r\nRequest timed out.\r\nRequest timed out.\r\nRequest timed out.\r\nRequest timed out.\r\n\r\nPing statistics for 123.123.123.123:\r\n Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),\r\n'

    Any thoughts?
    – oreid Apr 12 '14 at 06:06
  • It's not unusual for `ping` to get some timeouts on a working connection. Maybe better to check for `Reply from` (so it would consider "host down" only, if all packets got lost) – Stephan Apr 12 '14 at 14:58
  • Thanks for your help so far but, "TypeError: Type str doesn't support the buffer API" – oreid Apr 13 '14 at 02:34
  • You probably need to decode the output first because you're reading binary data not a string. I've updated the answer. – carlosdc Apr 13 '14 at 11:52
  • Thank you very much for the help. This is working perfectly. You sir deserve a medal. – oreid Aug 21 '14 at 10:02