[问题] paramiko ssh连线

楼主: cc0827 (唬烂)   2018-09-27 17:45:41
小弟是学习python半年的菜鸟,想请教ptt各位先进,如何利用python 建立ssh连线,经由跳板机到远端的设备下指令呢?
路径: my pc -> jump host -> target machine
有参考网络上的写法
import paramiko
import sys
import subprocess
vm=paramiko.SSHClient()
vm.set_missing_host_key_policy(paramiko.AutoAddPolicy())
vm.connect('jump_host_ip', username='jump_host_username', password='jump_host_password')
#
vmtransport = vm.get_transport()
dest_addr = ('target_machine_ip', 22)
local_addr = ('my_pc_ip', 22)
vmchannel = vmtransport.open_channel("direct-tcpip", dest_addr, local_addr)
#
jhost=paramiko.SSHClient()
jhost.set_missing_host_key_policy(paramiko.AutoAddPolicy())
jhost.connect('target_machine_ip',username='target_machine_username',password='target_machine_password', sock=vmchannel)
#
stdin, stdout, stderr = jhost.exec_command("sh clock")
#
data = stdout.read()
print(data.decode("utf-8"))
#
jhost.close()
vm.close()
# End
但会出现错误
Secsh channel 0 open FAILED: User does not have permission: Administratively prohibited
Traceback (most recent call last):
File "C:\Users\omc\Desktop\try_jump_host.py", line 16, in <module>
vmchannel = vmtransport.open_channel("direct-tcpip", dest_addr, local_addr)
File”C:\Users\omc\AppData\Local\Programs\Python\Python36-32\lib\site-packages\paramiko-2.4.1-py3.6.egg\paramiko\transport.py", line 902, in open_channel
raise e
paramiko.ssh_exception.ChannelException: (1, 'Administratively prohibited')
请问这样是程式码有问题,还是说目前的网络环境不允许这样连线呢? 恳请各位先进指点一二
作者: s860134 (s860134)   2018-09-29 03:31:00
因为你搞错目标了("direct-tcpip",(jump_host_ip,22),('127.0.0.1',22))你的 vm channel 是建立在 my pc -> jump host而不是你上面写的直接建在 jump_host -> target_mechine就我所知你要做第二种操作 jump_host 的 sshd config要开AllowTcpForwarding yes好像是叫做 reverse proxy忽略我上面所说,你把 jhost.commect 的 target_machine_换成 local_addr 就可以了刚试了一下,你有权限看到跳板的 /etc/ssh/sshd_config拉到最下面 是否有 Match User jump_host_usernameAllowTcpForwarding yes应该是要打开这个设定才能做TCP forwording

Links booklink

Contact Us: admin [ a t ] ucptt.com