1

I am not a complete newbie to python but I am fairly new and inexperienced. I discovered paramiko recently when I wanted to write a script to automate some package version checking + comparing with our production environment, but each connection takes a very long time :(

for app in apps:
        tstapp_address = test_env['prefix'] + str(app['tstapp']) + test_env['suffix']
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(tstapp_address, username='our_username', password='secret')
        sys.stdout.write('\n' + str(app_num) + ') ' + app['name'] + ' on ' + 'tstapp' + str(app['tstapp']) + '\n')
        stdin, stdout, sderr = ssh.exec_command('rpm -q ' + app['name'])
        tstapp_rpm = stdout.readline()
        sys.stdout.write(str(tstapp_rpm))
        stdin, stdout, stderr = ssh.exec_command('exit')
        app_num += 1
        ssh.close()

I have a JSON document that I retrieve before all this and it contains all the information on where to connect and so on. The problem is - every connection takes roughly 10 seconds to ssh to the host, to a rpm -q app-name, output it on the screen and close the connection before it connects to the next host. Is that regular behaviour with python+paramiko? :/

Please let me know if you need more information about what I do in my script, but I thought this would be sufficient.

EDIT: also wanted to mention that connecting to these hosts manually is really fast, they are not distant remote machines nor super slow. It must be something with my code... I just don't have any more ideas.

tsaulic
  • 707
  • 1
  • 11
  • 22
  • Please, show what time for `stdin, stdout, sderr = ssh.exec_command('rpm -q ' + app['name'])` and for other command. TIA. – Evgenii Apr 10 '13 at 04:34

1 Answers1

0

I found the 'solution'... both downgrading pycrypto to 2.4.1 from 2.6 and also lowering the bit size of my public ssh key greatly improved the speed.

FYI, to downgrade pycrypto use:

sudo pip install -U https://pypi.python.org/packages/source/p/pycrypto/pycrypto-2.4.1.tar.gz

to lower the bit size of the key use:

ssh-keygen -t rsa -b 768 -C "email@example.com"
tsaulic
  • 707
  • 1
  • 11
  • 22