很久以前有用 Paramiko 套件測試過連線遠端樹莓派, 參考 :
簡單測試成功後就收兵, 但今天為了工作上的自動化抽空使用 Paramiko 套件連線遠端主機卻時遇到 Error reading SSH protocol banner'utf-8' 問題 :
>>> import paramiko
>>> client=paramiko.SSHClient()
>>> client.load_system_host_keys()
>>> client.connect('192.168.70.11', '2361', 'iamyesman', '1234567')
Exception: Error reading SSH protocol banner'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\transport.py", line 2211, in _check_banner
try:
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\packet.py", line 386, in readline
return u(buf)
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\py3compat.py", line 147, in u
return s.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\transport.py", line 2039, in run
self._check_banner()
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\transport.py", line 2216, in _check_banner
raise SSHException(
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\transport.py", line 2211, in _check_banner
try:
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\packet.py", line 386, in readline
return u(buf)
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\py3compat.py", line 147, in u
return s.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\client.py", line 406, in connect
t.start_client(timeout=timeout)
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\transport.py", line 660, in start_client
raise e
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\transport.py", line 2039, in run
self._check_banner()
File "C:\Users\User\AppData\Local\Programs\Thonny\lib\site-packages\paramiko\transport.py", line 2216, in _check_banner
raise SSHException(
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
我在 Stackoverflow 找到下面這兩篇 :
照底下解答似乎是 Paramiko 底下有一個 Transport.py 檔案裡面的 timeout 設太小 (預設值是 self.banner_timeout=15), 答者建議直接加一列指令 timeout=200 固定此值即可, 首先用 __file__ 屬性找出 paramiko 位置 :
>>> paramiko.__file__
'C:\\Users\\User\\AppData\\Local\\Programs\\Thonny\\lib\\site-packages\\paramiko\\__init__.py'
到 Thonny 底下的 lib\site-packages\paramiko 即可找到 Transport.py :
但是我修改 Transport.py 後執行 SSH 連線錯誤依舊, 無效. 找了很多文章都無法解決, 所以先做個紀錄暫時擱下, 也許明天可以考慮改用 netmiko 看看.
參考 :
2023-06-14 補充 :
最近突然發現, 搞了老半天是我搞錯了, 原來我要連線的主機不是使用 ssh 協定, 而是使用 telnet, 難怪 Paramiko 會出現這個怪問題, 收隊.
沒有留言:
張貼留言