0

我正在使用 python 脚本来 ssh 并连接两台电脑来运行 iperf 命令,但是该脚本需要很长时间才能执行。如何减少执行时间:脚本是从 Eclipse IDE 运行的。

这是我正在使用的脚本:

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 user@192.168.100.101","pass123")
ssh.tc01c("ssh domain@192.168.100.85","123pass")    
4

2 回答 2

1

如果建立连接需要很长时间,您可能需要在两台机器的 /etc/ssh/sshd_config 中禁用 UseDNS 选项。

于 2013-05-13T10:38:25.060 回答
1

正如评论者所提到的,如果没有代码,这可能真的很难回答。但如果你不想/不能在这里附加代码,我会有一些猜测:

  • 如果您正在运行可执行的 SSH 客户端,请尝试切换到
  • 也许您正在运行的特定命令很慢,而不是连接本身?尝试运行其他东西(更简单)以确保 SSH 连接速度很慢。
  • 其他可能会让你慢下来的疯狂猜测:网络很慢,接收端很忙,你的主机很忙,等等。
于 2013-05-13T10:06:46.103 回答