Python3基于PySocks,paramiko来使用socks代理连接SSH服务器


==SSHClient 封装Transport==

import paramiko, socks
sock = socks.socksocket()
host, port = '103.67.206.10', 11113
sock.set_proxy(proxy_type=socks.SOCKS5,addr=host,port=port,username='lx',password='Qaz123')
sock.connect(('10.200.124.37', 33899))
private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/starsliao-gmt')
transport = paramiko.Transport(sock)
transport.connect(username='www', pkey=private_key)
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('df')
print(stdout.read())
transport.close()
sock.close()

==基于公钥秘钥连接==

import paramiko, socks
sock = socks.socksocket()
host, port = '103.67.206.10', 11113
sock.set_proxy(proxy_type=socks.SOCKS5,addr=host,port=port,username='lx',password='Qaz123')
sock.connect(('10.200.124.37', 33899))
private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/starsliao-gmt')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='10.200.124.37', port=33899, username='www', pkey=private_key, sock=sock)
stdin, stdout, stderr = ssh.exec_command('df')
print(stdout.read())
ssh.close()
sock.close()