ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('first.com', username='luser', password='secret')
chan = ssh.invoke_shell()
chan.send('ssh second.com\n')
buff = ''
while not buff.endswith("'s password: "):
    resp = chan.recv(9999)
buff += resp
chan.send('secret\n')
buff = ''
while not buff.endswith('some-prompt$ '):
    resp = chan.recv(9999)
buff += resp
chan.send('ls\n')
buff = ''
while not buff.endswith('some-prompt$ '):
    resp = chan.recv(9999)
buff += resp
print('buff', buff)
ssh.close()
Nested SSH session with Paramiko