[Tutor] why "ifconfig" is alway running?
Evert Rol
evert.rol at gmail.com
Sun Dec 19 13:09:42 CET 2010
>>> #!/usr/bin/env python
>>> import datetime
>>> import subprocess
>>> import sys
>>> import os
>>> import signal
>>> from time import sleep
>>>
>>> def host_run(cmd, secs=10):
>>> print("running %s" % cmd)
>>> timeout = datetime.timedelta(seconds=secs)
>>> proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
>>> stderr=subprocess.STDOUT, shell=True)
>>> start = datetime.datetime.now()
>>> while ( proc.poll() is None and (datetime.datetime.now() - start)
>>> < timeout): #not timed out
>>> print proc.stdout.readline() #TODO timestamp?
>>> print "hello,i'm here"
>>> sleep(1)
>>> if 0 == proc.poll():
>>> print("'%s' is program exited" %cmd)
>>> else:
>>> try:
>>> os.kill(proc.pid, signal.SIGINT)
>>> print "Process timeout: '%s'" % cmd
>>> except OSError:
>>> pass
>>> #cmd="ping 128.114.122.2"
>>> cmd="ssh root at 10.0.0.1"
>>> host_run(cmd,10)
>>>
>>> if cmd="ssh root at 10.0.0.1"
>>> it never print "hello i'm here" , how can i handle this issue, and I
>>> find my script cant process the "timeout" to kill it.
>>> if cmd="ping 128.114.122.2", no this issue.
>>
>> Have you tried running these two commands from the command line?
>> Spot the difference: ssh is waiting for input (a password. And if passwordless, it may still not produce output to stdout), while ping just continues to produce output.
>> Then look at the line
>> print proc.stdout.readline()
>> Will that ever read a full line for the ssh command? Probably not, while for ping it will.
>>
>
> it's a test, actually, I can't access 10.0.0.1, i just want to kill it
> if *no responding" in 10 second, but it doesn't kill it. I don't know
> why.
> my script is to run a cmd, if more than 10 sec, kill it
> can you show me where is wrong, thanks in advance
But you never said where the script gets stuck. That would be a very important first step for debugging.
And if I run this script, I get the following:
"
$> python bla.py
running ssh root at 10.0.0.1
ssh: connect to host 10.0.0.1 port 22: Connection refused
hello,i'm here
"
So seems to work for me.
thus, what is the result of
$> ssh root at 10.0.0.1
for you?
Evert
>> So my guess is, your script gets stuck at that line.
>> But if you ctrl-C to stop the script, you should see where your program gets stuck. You didn't say where it got stuck, that would have helped.
>>
>> Good luck,
>>
>> Evert
>>
>>
>>> On Sun, Dec 19, 2010 at 4:57 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>>>>
>>>> "lei yang" <yanglei.fage at gmail.com> wrote
>>>>
>>>>
>>>> def runForAWhile(cmd, secs=10):
>>>> print("running %s" % cmd)
>>>> timeout = datetime.timedelta(seconds=secs)
>>>> print timeout
>>>> proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
>>>> stderr=subprocess.STDOUT, shell=True)
>>>> status = proc.poll()
>>>>
>>>> You are still only checking status once outside the while loop.
>>>>
>>>> start = datetime.datetime.now()
>>>> while (status is None and (datetime.datetime.now() - start) <
>>>> timeout): #not timed out
>>>> print proc.stdout.readline() #TODO timestamp?
>>>> #print status
>>>> #print datetime.datetime.now() - start
>>>>
>>>>
>>>>> I see that "status" always "!=0“ why program is NOT exited
>>>>
>>>>
>>>> --
>>>> Alan Gauld
>>>> Author of the Learn to Program web site
>>>> http://www.alan-g.me.uk/
>>>>
>>>>
>>>> _______________________________________________
>>>> Tutor maillist - Tutor at python.org
>>>> To unsubscribe or change subscription options:
>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>
>>> _______________________________________________
>>> Tutor maillist - Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
More information about the Tutor
mailing list