0

I am using python script to ssh and connect two pc's for running iperf commands, but the script is taking a lot of time for execution. How can I reduce the execution time: the script is being run from eclipse IDE.

this is the script that i am using:

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")    
3
  • i believe Eclipse is making it slow. Try running outside Eclipse. Commented May 13, 2013 at 9:50
  • 1
    We need to see the script to advise. Commented May 13, 2013 at 9:51
  • 1
    can you go over the formatting again? Also, what is 'a long time'? There's a bunch of time.sleep Commented May 13, 2013 at 10:32

2 Answers 2

1

As commenters mentioned, without the code this could be really hard to answer. But in case you don't want/can't attach the code here, I would have some guesses:

  • If you're running an executable SSH client, try switching to a library.
  • Maybe the specific commands you're running is what's slow, and not the connection itself? Try running something else (simpler) to know for sure that the SSH connection is what's slow.
  • Other wild guesses of things that might slow you down: Network is slow, receiving ends are busy, your host is busy, etc.
Sign up to request clarification or add additional context in comments.

Comments

1

If it is taking a long time for establishing the connection, you might want to disable the UseDNS option in the /etc/ssh/sshd_config of both the machines.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.