首页 > 其他 > 详细

ssh 通过跳板机连接到远程服务器

时间:2019-04-19 01:17:38      阅读:971      评论:0      收藏:0      [点我收藏+]

ssh 通过跳板机连接到远程服务器

import paramiko
from sshtunnel import SSHTunnelForwarder
import threading


def readData(shell):
    while True:
        data = shell.recv(2048)
        if not data:
            print("quit now")
            break
        data = data.decode()
        if "logout" in data:
            break
        print(data, end = "")


def main():
    with SSHTunnelForwarder(ssh_address_or_host=(192.168.1.104, 22), ssh_username="104user", ssh_password="104pwd",
                            remote_bind_address=("192.168.1.105", 22), local_bind_address=(127.0.0.1, 10022)) as tunnel:
        print("111", tunnel.local_bind_address, tunnel.local_bind_port)
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        print("222")
        client.connect(127.0.0.1, tunnel.local_bind_port, username = "105user", password = "105pwd")
        print("333")
        shell = client.invoke_shell()

        thread = threading.Thread(target=readData, args=(shell,))
        thread.start()

        shell.sendall("pwd\n")
        shell.sendall("ifconfig\n")
        shell.sendall("exit\n")

        thread.join()
        client.close()

 

上面的代码通过, 192.168.1.104 连接到 192.168.1.105

 

ssh 通过跳板机连接到远程服务器

原文:https://www.cnblogs.com/diysoul/p/10733445.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!