[問題]
小弟是學習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')
請問這樣是程式碼有問題,還是說目前的網路環境不允許這樣連線呢? 懇請各位先進指點一二
-----
Sent from JPTT on my Asus ASUS_Z017DA.
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.76.172.211
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1538041543.A.625.html
→
09/29 03:31,
5年前
, 1F
09/29 03:31, 1F
→
09/29 03:33,
5年前
, 2F
09/29 03:33, 2F
→
09/29 03:34,
5年前
, 3F
09/29 03:34, 3F
好的,十分感謝s大,沒想到真的有人回覆
→
09/29 03:35,
5年前
, 4F
09/29 03:35, 4F
→
09/29 03:35,
5年前
, 5F
09/29 03:35, 5F
→
09/29 03:38,
5年前
, 6F
09/29 03:38, 6F
→
09/29 03:38,
5年前
, 7F
09/29 03:38, 7F
→
09/29 03:48,
5年前
, 8F
09/29 03:48, 8F
→
09/29 03:49,
5年前
, 9F
09/29 03:49, 9F
→
09/29 11:41,
5年前
, 10F
09/29 11:41, 10F
→
09/29 11:42,
5年前
, 11F
09/29 11:42, 11F
→
09/29 11:43,
5年前
, 12F
09/29 11:43, 12F
→
09/29 11:43,
5年前
, 13F
09/29 11:43, 13F
※ 編輯: cc0827 (42.76.172.211), 09/30/2018 00:06:30
討論串 (同標題文章)