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.