0

因此,我有一个程序可以通过 ssh 连接到远程服务器并启动该端的 iperf 服务器。当那运行时,它将从客户端iperf该服务器。

当我静态给出 IP 地址时它工作正常,例如:

        p=pexpect.spawn('ssh -t -x paddy@20.20.20.20 ' + iperf)

但不是当我使用:

        p=pexpect.spawn('ssh -t -x paddy@'+ADDRESS+' ' + iperf)

我得到一个:

'pexpect.TIMEOUT'

ADDRESS 肯定是正确的。有人对出了什么问题有任何想法吗?

    #!/usr/bin/env python
import pexpect
import re
import shutil
import getpass
import struct, fcntl, os, sys, signal, time

def start_Server(iperf, password, ADDRESS):
    ssh_newkey = 'Are you sure you want to continue connecting'
    fix = ADDRESS+' ' + iperf
    p=pexpect.spawn('ssh -t -x paddy@'+ fix)
    i=p.expect([ssh_newkey,'password:',pexpect.EOF,pexpect.TIMEOUT],1)
    if i==0:
        print "I say yes"
        p.sendline('yes')
        i=p.expect([ssh_newkey,'password:',pexpect.EOF])
    if i==1: 


        pwtp = False
        trysout = True
        while pwtp == False:
            trysout = True
            p.sendline(password)
            loginStuff=p.expect(['Permission denied, please try again.','Permission denied (publickey,password).', '------------------------------------------------------------', pexpect.TIMEOUT,pexpect.EOF],1)
            if loginStuff == 0:
                password = getpass.getpass("Please enter "+ADDRESS+"'s Password")
            elif loginStuff == 1:
                print 'Sorry but you faild to login'
                sys.exit(0)
                pwtp = True
                trysout = False
            elif loginStuff == 2:
                pwtp = True
                i=3
            elif loginStuff == 4:
                pwtp = True
                pass
            else:
                pass       

    elif i==2:
        print "I either got key or connection timeout"
        pass
    elif i==4:
        print "I either got key or connection timeout"
        pass
    if i==3: #timeout
        print fix
        print ADDRESS
        print 'we find outselfs in a timeout'
        print i
        pass
    return p, password


def RepresentsInt(s):
    try: 
        int(s)
        return True
    except ValueError:
        return False

var = raw_input("Enter the destination IP address: ")
ADDRESS = var

password = getpass.getpass("Please enter "+ADDRESS+"'s Password")        

t, password = start_Server('iperf -s', password, ADDRESS)

u, password = start_Server('iperf -u -s', password, ADDRESS)
print ADDRESS
p=pexpect.spawn('ssh -t -x paddy@20.20.20.20 iperf -u -s')
ssh_newkey = 'Are you sure you want to continue connecting'
i=p.expect([ssh_newkey,'password:',pexpect.EOF])
if i == 0:
    print ssh_newkey
elif i == 1:
    print 'password:'
elif i == 2:
    print pexpect.EOF
else:
    print 'Sorry what!?'
    print i
4

1 回答 1

0

当我将 pexpect ssh 从子例程中取出时,它起作用了。

于 2012-06-01T10:16:38.243 回答