Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
example paramiko script with interactive terminal
import paramiko
import time
import re
bastion_ip='ip'
bastion_pass='pass'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect(bastion_ip, username='root', password=bastion_pass)
chan = ssh.invoke_shell()
# other cloud server
priv_ip='ip'
passw='pass'
test_script='/root/check_rackconnect.sh'
def run_cmd(cmd):
buff = ''
while not buff.endswith(':~# '):
resp = chan.recv(9999)
buff += resp
print(resp)
# Ssh and wait for the password prompt.
chan.send(cmd + '\n')
buff = ''
while not buff.endswith('\'s password: '):
resp = chan.recv(9999)
buff += resp
print(resp)
# Send the password and wait for a prompt.
time.sleep(3)
chan.send(passw + '\n')
buff = ''
while buff.find(' done.') < 0 :
resp = chan.recv(9999)
buff += resp
print(resp)
ret=re.search( '(\d) done.', buff).group(1)
ssh.close()
print('command was successful:' + str(ret=='0'))
scp_opt=""
cmd='scp -q ' + scp_opt + ' -o NumberOfPasswordPrompts=1 -o StrictHostKeyChecking=no %s root@%s:~/; echo $? done.' % ( test_script, priv_ip )
print('\n test 2\n cmd %s\n' % cmd)
run_cmd(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment