利用sshtunnel实现跳板机的效果[嵌套ssh实现]

时间:2021-07-04 09:21:14
    with SSHTunnelForwarder (
ssh_address_or_host = (conf.server_ip,conf.server_port),
ssh_username=conf.server_uname,
ssh_password=conf.server_pwd,
remote_bind_address=(dn, 22),
local_bind_address=(conf.local_ip, conf.local_port)
) as server:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(conf.local_ip, conf.local_port, username=conf.remote_uname,password=conf.remote_pwd)

  

pc1------server---------remote

场景是这样的,pc需要访问remote的设备,但是网络不通,pc只能访问server,但是server确可以访问remote

sshtunnel模块通过server设备,实现在pc和remote之前搭建一条ssh隧道,这样,我们就可以直接在本机上ssh访问远端的设备了

 client.connect(conf.local_ip, conf.local_port, username=conf.remote_uname,password=conf.remote_pwd)

  

这里的local_ip和local_port是pc1的ip和端口,这个端口是任意未使用的端口即可

username和password是remote的用户名和密码