Forked from rtomaszewski/example_paramiko_with_tty.py
Created
August 28, 2019 13:58
-
-
Save 0ccupi3R/7bc2bc35b1f499daf3219c18539d03b3 to your computer and use it in GitHub Desktop.
example paramiko script with interactive terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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