小弟是學習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')
請問這樣是程式碼有問題,還是說目前的網路環境不允許這樣連線呢? 懇請各位先進指點一二