Ping until a system comes back up

35 pts.
Windows 7
In Python, how do I write a script that will continuously ping until the system is back up; using a windows platform. When I get "reply", the system is not down yet, when I get "request timeout", the system is down, finally when the system "reply" returns; the system is back up.

Software/Hardware used:
Python 2.7

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question: 5  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • TheRealRaven
    os.system('ping %s -n 1' % (address,))

    Will return 0 when successful (as most things will), and 1 when it fails. Redirect output to NUL to discard it.
    37,045 pointsBadges:
  • yopappy
    Maybe I didn't clearly state the problem:

    When response is "Reply from":
    Assert the system is not down yet. (this could take 4 minutes)
    if response is "Request timed out".
    Assert the system has rebooted. (this could take 4 minutes)
    When the system comes backup again the response is "Reply from":
    Assert the system has is backup.
    After two "Reply from", break and goto the next command.
    35 pointsBadges:
  • Subhendu Sen
    Is this your class work/an assignment? If you know python, and if this is really a business case, then you must come with your python script. and we can provide reply on the basis of that script. You must try it first yourself, and if there is error/problem, we can provide assistance for you.
    142,670 pointsBadges:
  • yopappy
    This is a work related problem, I'm new to python. One week experience. And I have tried and got an error.
    import os
    from time import time, sleep
    import pytest, logging
    from time import time, sleep
    import re
    import subprocess

    log = logging.getLogger()
    uut_ip = ""

    #--------------------------------------------------Telnet Connection--------------------------------------------------------------------------------------
    @pytest.fixture(scope = "class")
    def setup(tb_testbed,request):"setup")
    def fin():"starting cleanup")

    #--------------------------------------------------Class Beginning----------------------------------------------------------------------------
    class Test_Demo(object):

    def test_preSetup(self,tb_testbed, tb_telnetsession, setup):

    for i in range(240):

    for k in range(15):
    response = os.system("ping -t 1 " + uut_ip)
    if response == "Reply from":
    pingstatus1 = "The SCM has not lost connection yet"
    elif response == "Request timed out.":
    pingstatus2 = "The SCR successfully rebooted"
    elif response == "Reply from":
    pingstatus3 = "The SCM is back up"
    Error below:
    ________________ ERROR collecting tests/ _________________
    c:\python27\lib\site-packages\_pytest\ in _importtestmodule
        mod = self.fspath.pyimport(ensuresyspath=importmode)
    c:\python27\lib\site-packages\py\_path\ in pyimport
    E     File "C:\Users\aspencer\Desktop\Pytest\PythonTestAutomation\tests\", line 30
    E       for i in range(240):
    E                          ^
    E   IndentationError: unindent does not match any outer indentation level
    !!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
    =========================== 1 error in 0.17 seconds ===========================
    35 pointsBadges:
  • TheRealRaven
    It's not clear what the purpose of this line is:
     for i in range(240):
    But this line is a problem:
    The indentation of sleep() puts it at the same level as the for that precedes it. Essentially that would mean that the for would run with 240 increments of i (taking some fraction of a second to finish), and then sleep() would sit for a quarter second.

    I'd guess that you want an extra white-space (or two or three) in front of sleep() to have it run inside the for-loop. But you could more easily delete the for line and just have sleep(60) if you just want a 60-second delay.

    37,045 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: