Ping until a system comes back up

35 pts.
Tags:
Python
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
    This:
    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.
    27,080 pointsBadges:
    report
  • 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:
    report
  • 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.
    114,060 pointsBadges:
    report
  • 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 = "10.59.50.71"

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

    #--------------------------------------------------Class Beginning----------------------------------------------------------------------------
    @pytest.mark.auto
    class Test_Demo(object):



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


    for i in range(240):
    sleep(0.25)

    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/test_pingtillalive.py _________________
    c:\python27\lib\site-packages\_pytest\python.py:418: in _importtestmodule
        mod = self.fspath.pyimport(ensuresyspath=importmode)
    c:\python27\lib\site-packages\py\_path\local.py:662: in pyimport
        __import__(modname)
    E     File "C:\Users\aspencer\Desktop\Pytest\PythonTestAutomation\tests\test_pingtillalive.py", 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 ===========================
    
    C:\Users\aspencer\Desktop\Pytest\PythonTestAutomation\tests>
    35 pointsBadges:
    report
  • TheRealRaven
    It's not clear what the purpose of this line is:
     for i in range(240):
    But this line is a problem:
     sleep(0.25)
    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.


    27,080 pointsBadges:
    report

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.

Following

Share this item with your network: