Nested SSH using Python Paramiko

后端 未结 3 1517
情话喂你
情话喂你 2020-11-27 06:23

I have this scenario:

Local-host --------- jump-host ------- target-machine

I am trying to write a code in Python using Paramiko to first SSH from local-host

相关标签:
3条回答
  • 2020-11-27 06:44

    Try the following edited code, it should work:

    #!/usr/bin/python
    #
    # Paramiko
    #
    import paramiko
    import sys
    import subprocess
    #
    # we instantiate a new object referencing paramiko's SSHClient class
    #
    vm = paramiko.SSHClient()
    vm.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    vm.connect('192.168.115.103', username='osmanl', password='xxxxxx')
    #
    vmtransport = vm.get_transport()
    dest_addr = ('10.103.53.26', 22) #edited#
    local_addr = ('192.168.115.103', 22) #edited#
    vmchannel = vmtransport.open_channel("direct-tcpip", dest_addr, local_addr)
    #
    jhost = paramiko.SSHClient()
    jhost.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    #jhost.load_host_keys('/home/osmanl/.ssh/known_hosts') #disabled#
    jhost.connect('10.103.53.26', username='latiu', password='xxxx', sock=vmchannel)
    #
    stdin, stdout, stderr = jhost.exec_command("show version | no-more") #edited#
    #
    print stdout.read() #edited#
    #
    jhost.close()
    vm.close()
    # End
    
    0 讨论(0)
  • 2020-11-27 06:55

    I know OP has specifically asked for Paramiko but i can do this very easily with fabric. Here is my solution

    from fabric import Connection
    
    out = Connection('host1').run('host2 uptime')
    print(out.stdout.strip())
    

    This works just fine for me, and i have the output stored in a variable as well.

    0 讨论(0)
  • 2020-11-27 07:08

    I found this easiest route to login to remote server via jumphost.This works amazing!

        link :https://pypi.org/project/jumpssh/
        import jumpssh
    
    0 讨论(0)
提交回复
热议问题