0
我正在使用python腳本ssh並連接兩臺pc用於運行iperf命令,但該腳本需要大量時間才能執行。我怎樣才能縮短執行時間:腳本是從eclipse IDE運行的。SSH python腳本執行花費太多時間
這是我使用的腳本:
import pexpect
import pxssh
import time
import os,re,sys
def tc01s(ipaddr,password):
try:
ss = pexpect.spawn(ipaddr)
print ipaddr
ss.logfile = open("/tmp/mynewlog", "w")
#ss.logfile = sys.stdout
print "SSH connecting"
print
except:
print "connection refused"
print
#sys.exit()
try:
print password
ss.expect (':')
ss.sendline (password +"\n")
print "connected"
time.sleep(30)
ss.expect (">")
print "connection established"
print
except:
print "Permission denied, please try again."
print
sys.exit()
try:
ss.sendline ('taskkill /F /IM iperf.exe\n')
#time.sleep(30)
ss.expect ('>')
#os.system("D:\iperf-2.0.5-2-win32\iperf.exe -s\n")
ss.sendline ('cd /D D:\iperf-2.0.5-2-win32')
ss.expect ('>')
ss.sendline ('iperf -s -u -f m -p 5000 -l 1470 -i 1')
ss.expect (r'TCP window size: .*yte \(default\)')
time.sleep(30)
except:
print
print
#sys.exit()
###############################################################################
time.sleep(30)
def tc01c(ipaddr,password):
try:
css = pexpect.spawn(ipaddr)
css.logfile = open("/tmp/mynewlog", "w")
#css.logfile = (sys.stdout)
print "SSH connecting"
print
except:
print "connection refused"
print
sys.exit()
try:
css.expect (':')
css.sendline (password + "\n")
print "connected"
time.sleep(30)
css.expect (">")
print "connection established"
print
except:
print "Permission denied, please try again."
print
sys.exit()
try:
#css.sendline ('taskkill /F /IM iperf.exe\n')
print css.logfile
css.expect ('>')
css.sendline ('D:' + "\r")
#css.sendline ('cd /D D:\iperf-2.0.5-2-win32')
css.expect ('>')
css.sendline ('dir' + "\r")
css.sendline ('cd iperf ' + "\r")
css.expect ('>')
css.sendline ('iperf -u -f m -c 192.168.100.101 -p 5000 -b 3.00M -t 10 -l 1470 -i 1 \r\n')
css.expect (r'TCP window size: .*yte \(default\)')
time.sleep(30)
except:
print
print
#sys.exit()
###############################################
the above code is executed using
import os
import pexpect
import ssh
import time
ssh.tc01s("ssh [email protected]","pass123")
ssh.tc01c("ssh [email protected]","123pass")
我相信Eclipse讓它變慢。嘗試在Eclipse外部運行。 – Netro 2013-05-13 09:50:50
我們需要看到腳本建議。 – jvallver 2013-05-13 09:51:10
你可以重新格式化嗎?另外,什麼是'很久'?有一堆'time.sleep' – 2013-05-13 10:32:38