Page 1 of 4 123 ... LastLast
Results 1 to 10 of 36

Thread: [SOLVED] I'm a noob learning python... need help!

  1. #1
    Join Date
    Jan 2008
    Beans
    213
    Distro
    Ubuntu 8.04 Hardy Heron

    [SOLVED] I'm a noob learning python... need help!

    I'm making a script, but I need a function like 'grep'.

    For example:
    Code:
    ping google.com
    
    if ("1 packets transmitted, 1 received" exists):
        print "result1"
    elif (1 packets transmitted, 0 received" exists):
        print "result2"
    I'm making that script for 3 hours and I'm stuck here

    Here's the idea - ping google.com, if the packet is received -> ok; if not -> reboot my adsl modem.

    Please, Help me!
    Last edited by LinuX-M@n1@k; August 17th, 2011 at 02:45 PM.

  2. #2
    Join Date
    May 2007
    Location
    Colorado
    Beans
    Hidden!

    Re: I'm a noob learning python... need help!

    PHP Code:
    #!/usr/bin/python


    import socket

    def net_test
    ():
        print 
    "\nTesting your network connection..."    

        
    try:
            
    socket.socket(socket.AF_INETsocket.SOCK_STREAM)
            
    s.connect(("www.google.com"80))
            return 
    True

        except socket
    .error:
            return 
    False

    if __name__ == "__main__":
        
    # Testing network connection
        
    network net_test()
        if 
    network == True
            print 
    "Good to go"
        
    else:
            print 
    "Reboot it" 
    Last edited by walkerk; July 3rd, 2008 at 10:17 PM.

  3. #3

    Re: I'm a noob learning python... need help!

    I can't give a good answer with such a sort description, but

    If you're working with strins, you can try somenthing with the function "find".
    Code:
    >>> 'asdf'.find('a')
    0
    >>> 'asdf'.find('f')
    3
    >>> 'asdf'.find('x')
    -1
    If you're using the function that let you execute a command, just add
    the grep in there 'ping www.google.com | grep "1 received"'
    How strange it is, that a fool or a knave, with riches, should be treated with more respect by the world, than a good man, or a wise man in poverty!

  4. #4
    Join Date
    May 2007
    Location
    Colorado
    Beans
    Hidden!

    Re: I'm a noob learning python... need help!

    PHP Code:
    #!/usr/bin/python

    import ossys

    temp 
    "ping www.google.com -C 3"

    ping_raw os.popen(temp)
    ping_output ping_raw.read()


    if 
    ping_output.find("bytes") != -1:
        print 
    "network there"
    else:
        print 
    "reboot" 
    I suggest my first option
    Last edited by walkerk; July 3rd, 2008 at 10:40 PM.

  5. #5
    Join Date
    Dec 2007
    Location
    .
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: I'm a noob learning python... need help!

    Code:
    #!/usr/bin/env python
    import commands
    result = commands.getoutput("ping -c 1 google.com")
    if result.find("Unreachable") == -1:
       result = True
    else:
       result = False
    
    if result == False:
    #restart modem

  6. #6
    Join Date
    Jan 2008
    Beans
    213
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: I'm a noob learning python... need help!

    Thank you all!! I have One more thing to do:
    Sometimes the telnet session to my modem freezes and needs '^]' (Ctrl-C)... Is there any possible way to send that signal from python?

  7. #7
    Join Date
    Dec 2007
    Location
    .
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: I'm a noob learning python... need help!

    Quote Originally Posted by walkerk View Post
    PHP Code:
    #!/usr/bin/python

    import ossys

    temp 
    "ping www.google.com"

    ping_raw os.popen(temp)
    ping_output ping_raw.read()


    if 
    ping_output.find("bytes") != -1:
        print 
    "network there"
    else:
        print 
    "reboot" 
    I suggest my first option
    This won't even work because it will ping forever.
    You need to add "-c N" where N is the number of "pings" to
    attempt(see my other post).

    Your first one works though

  8. #8
    Join Date
    May 2007
    Location
    Colorado
    Beans
    Hidden!

    Re: I'm a noob learning python... need help!

    Quote Originally Posted by days_of_ruin View Post
    This won't even work because it will ping forever.
    You need to add "-c N" where N is the number of "pings" to
    attempt(see my other post).

    Your first one works though
    lol.. good catch

  9. #9
    Join Date
    Dec 2007
    Location
    .
    Beans
    Hidden!
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: I'm a noob learning python... need help!

    Quote Originally Posted by LinuX-M@n1@k View Post
    Thank you all!! I have One more thing to do:
    Sometimes the telnet session to my modem freezes and needs '^]' (Ctrl-C)... Is there any possible way to send that signal from python?
    Don't know if there is a way to do that.That would involve interaction
    with a different process which can be done in python
    but I have never done it.

    Maybe doing "killall nameofprocess".
    How would you signal to run that though?

    EDIT:Would having your telnet session freezing make pinging google fail?
    Then you could just ad that to whatever script you are using.
    Last edited by days_of_ruin; July 3rd, 2008 at 10:41 PM.

  10. #10
    Join Date
    Jan 2008
    Beans
    213
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: I'm a noob learning python... need help!

    Quote Originally Posted by days_of_ruin View Post
    Don't know if there is a way to do that.That would involve interaction
    with a different process which can be done in python
    but I have never done it.

    Maybe doing "killall nameofprocess".
    How would you signal to run that though?

    EDIT:Would having your telnet session freezing make pinging google fail?
    Then you could just ad that to whatever script you are using.
    Well, I can wait one minute and it will pass over that part and let me log into the modem Anyway... THANK YOU!! Thank you very much for the help! I'll post the final script in a minute.

    Edit: No, it doesn't block the ping... It just doesn't let me log into the modem.

Page 1 of 4 123 ... LastLast

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
  •