0

So I've got a program that will ssh on to a remote server and start an iperf sever of that end. When thats running it will iperf that sever from the client side.

It works fine when I give the IP address statically for example:

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

But not when I use:

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

I get a:

'pexpect.TIMEOUT'

ADDRESS is definitely going in correctly. Any one have any ideas about whats going wrong?

    #!/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
Paddy
  • 195
  • 1
  • 2
  • 10
  • what is `iperf` variable? and what is `ADDRESS` ? – wim May 31 '12 at 12:32
  • Both are strings. ADDRESS is the IP address and iperf string holding the command I wish to run eg.'iperf -s'. – Paddy May 31 '12 at 12:36
  • Sorry but the connect failed: Connection refused and read failed: Connection refused are not from the ssh. But I am getting a 'pexpect.TIMEOUT' – Paddy May 31 '12 at 12:47
  • Do you have any spaces in ADDRESS? Can you show us exactly how you define ADDRESS and iperf and what `print` each of these values. These are quite important parts of the question and should really be included. – Chris May 31 '12 at 12:59
  • There are no spaces in the Address. Sorry hear is the relevant code. – Paddy May 31 '12 at 13:25
  • 1
    That is quite a lot of code. Can you reduce it down to a small self contained problem which reproduces the issue you are seeing? Also, can you fix your indentation, this quite important in Python! Finally, can you show us what ADDRESS is?! Not the code you use to get user input, but the exact value of ADDRESS when you get a timeout. – Chris May 31 '12 at 13:33
  • 1
    Could be relevant: if you answer "no" the first time you are asked to confirm the address, `ADDRESS` is never set to `var`. – chepner May 31 '12 at 13:39
  • OK I hope this is a bit easer to read. – Paddy May 31 '12 at 14:33

1 Answers1

0

It worked when I took the pexpect ssh out of the subroutine.

Paddy
  • 195
  • 1
  • 2
  • 10