Nested SSH session with Paramiko

后端 未结 5 999
眼角桃花
眼角桃花 2020-11-27 03:42

I\'m rewriting a Bash script I wrote into Python. The crux of that script was

ssh -t first.com \"ssh second.com very_remote_command\"

I\'m

5条回答
  •  天命终不由人
    2020-11-27 04:12

    Sinas's answer works well but didn't provide all the output from very long commands for me. However, using chan.makefile() allows me to retrieve all the output.

    The below works on a system that requires tty and also prompts for sudo password

    ssh = paramiko.SSHClient()
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.WarningPolicy())
    ssh.connect("10.10.10.1", 22, "user", "password")
    chan=ssh.get_transport().open_session()
    chan.get_pty()
    f = chan.makefile()
    chan.exec_command("sudo dmesg")
    chan.send("password\n")
    print f.read()
    ssh.close()
    

提交回复
热议问题