Close

Results 1 to 7 of 7
  1. #1
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1155

    Default Python - Sending os.system command to a string

    All,

    I am stumped at the moment....
    I am trying to run an os.system command and in turn write the value to a string so I can check the outcome.

    Basically I need to ping a machine on the server, write the results of the ping to a string and then search the string and extract the result.

    So far I have this (I have not got onto the searching string code yet as I keep getting 0!!):

    Code:
    import os
    
    # Ping Server to make sure it is available
    os.system('cls')
    print("Pinging Server to ensure it is online")
    print("")
    Server_Up = os.system('ping 192.168.0.1')
    print("")
    print(Server_Up)
    print("")
    I just keep getting '0' as the output??? Any ideas....

    What I expect to see whent I print the string is:

    Pinging Server [192.168.0.1] with 32 bytes of data:

    Reply from 192.168.0.1: bytes=32 time<1ms TTL=64
    Reply from 192.168.0.1: bytes=32 time<1ms TTL=64
    Reply from 192.168.0.1: bytes=32 time<1ms TTL=64
    Reply from 192.168.0.1: bytes=32 time<1ms TTL=64

    Ping statistics for 192.168.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
    We all make mistakes sometimes

  2. #2
    DF VIP Member blacksheep's Avatar
    Join Date
    Jun 2006
    Location
    Manchester
    Posts
    3,877
    Thanks
    87
    Thanked:        265
    Karma Level
    548

    Default Re: Python - Sending os.system command to a string

    That's going to return you the exit status of what you're run, have a look at os.popen.

  3. #3
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1155

    Default Re: Python - Sending os.system command to a string

    Yeah but os.popen seems to fail on Windows??
    We all make mistakes sometimes

  4. #4
    DF VIP Member blacksheep's Avatar
    Join Date
    Jun 2006
    Location
    Manchester
    Posts
    3,877
    Thanks
    87
    Thanked:        265
    Karma Level
    548

    Default Re: Python - Sending os.system command to a string

    hmmmm not sure why that is, but everything I've done in python has been linux - maybe commands.getoutput?

  5. #5
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1155

    Default Re: Python - Sending os.system command to a string

    I'll have a play later. For now I just send the ping command and view the result manually rather than automatically.

    Cheers again mate.
    We all make mistakes sometimes

  6. #6
    DF VIP Member Ashy's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle
    Posts
    457
    Thanks
    0
    Thanked:        1
    Karma Level
    283

    Default Re: Python - Sending os.system command to a string

    You should be able to use os.popen even in windows.
    Code:
    import os
    tmp = os.popen("ping 127.0.0.1").readlines()
    print tmp
    It works fine for me in windows

    edit:
    I was bored
    This should do what you want.
    Code:
    import os
    def checkHost( host ):
        tmp = os.popen("ping -n 2 %s" % host).readlines()
        for line in tmp:
            if "Reply from" in line:
                return True
        return False
    
    if checkHost("127.0.0.1"):
        print "Its UP"
    else:
        print "Its DOWN"
    Last edited by Ashy; 19th November 2008 at 11:46 AM. Reason: new code ;)
    Leechr will grab your tv-ep nzb's for you :)

  7. #7
    DF Probation Goldberg's Avatar
    Join Date
    Jun 2001
    Location
    Landaaaan!
    Posts
    14,453
    Thanks
    1,325
    Thanked:        1,547
    Karma Level
    1155

    Default Re: Python - Sending os.system command to a string

    Legend mate, works a treat. It was the readlines bit I was missing.
    We all make mistakes sometimes

Similar Threads

  1. System Crashes During Burning
    By djc53320661 in forum PC Problems
    Replies: 13
    Last Post: 1st December 2003, 12:13 PM
  2. alarm system help needed
    By shadygeezer in forum The Dog and Duck
    Replies: 3
    Last Post: 13th September 2002, 08:05 PM
  3. Yet another copy protection system
    By sanjuro in forum PC Hardware
    Replies: 2
    Last Post: 30th August 2002, 12:50 PM

Social Networking Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •