能够使用带 python 和 Linux 的 SSH,但不能使用带 python 和 Sonicwall 的 SSH
Able to use SSH w/ python and Linux but not able to use SSH w/ python and Sonicwall
我正在 python 中使用 Paramiko 来自动执行各种 SSH 任务。我想做的一件事是 运行 sonicwall 上的一些命令。我有一些代码可以让我在 Ubuntu VM 上执行 运行 命令,但相同的代码不适用于 Sonicwall。唯一改变的是变量(用户名、密码、IP 地址等) 下面是我收到的代码和错误。任何人都可以帮助启发我为什么这没有按预期工作吗?
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
host = '192.168.16.75'
port, user, password = 22, 'scott', 'scott'
ssh.connect(host, port, user, password)
stdin, stdout, stderr = ssh.exec_command("ls -l")
for line in stdout.readlines():
print(line)
ssh.close()
#ABOVE WORKS W/ UBUNTU
#BELOW DOES NOT WORK WITH SONICWALL
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
host = '192.168.16.63'
port, user, password = 22, 'admin', 'password'
ssh.connect(host, port, user, password)
stdin, stdout, stderr = ssh.exec_command("show arp")
for line in stdout.readlines():
print(line)
#BELOW IS THE ERRORS PYTHON THROWS
C:\Users\scott\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/scott/.PyCharmCE2019.2/config/scratches/scratch_6.py
Traceback (most recent call last):
File "C:/Users/scott/.PyCharmCE2019.2/config/scratches/scratch_6.py", line 8, in <module>
stdin, stdout, stderr = ssh.exec_command("show arp")
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\client.py", line 514, in exec_command
chan.exec_command(command)
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\channel.py", line 72, in _check
return func(self, *args, **kwds)
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\channel.py", line 257, in exec_command
self._wait_for_event()
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\channel.py", line 1226, in _wait_for_event
raise e
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\transport.py", line 2055, in run
ptype, m = self.packetizer.read_message()
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\packet.py", line 459, in read_message
header = self.read_all(self.__block_size_in, check_rekey=True)
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\packet.py", line 303, in read_all
raise EOFError()
EOFError
Process finished with exit code 1 ```
您的 SSH 服务器可能不允许 exec_command()
。
您可以通过执行以下 ssh 命令来验证这一点:
ssh user@host c i eth i
如果你得到
"Connection to x.x.x. closed by remote host."
则不支持
您应该改为在 paramiko 中启动交互式 shell:
ssh = paramiko.SSHClient()
ssh.connect('credentials here')
chan = ssh.invoke_shell()
chan.send('command here\n')
output = chan.recv()
我正在 python 中使用 Paramiko 来自动执行各种 SSH 任务。我想做的一件事是 运行 sonicwall 上的一些命令。我有一些代码可以让我在 Ubuntu VM 上执行 运行 命令,但相同的代码不适用于 Sonicwall。唯一改变的是变量(用户名、密码、IP 地址等) 下面是我收到的代码和错误。任何人都可以帮助启发我为什么这没有按预期工作吗?
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
host = '192.168.16.75'
port, user, password = 22, 'scott', 'scott'
ssh.connect(host, port, user, password)
stdin, stdout, stderr = ssh.exec_command("ls -l")
for line in stdout.readlines():
print(line)
ssh.close()
#ABOVE WORKS W/ UBUNTU
#BELOW DOES NOT WORK WITH SONICWALL
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
host = '192.168.16.63'
port, user, password = 22, 'admin', 'password'
ssh.connect(host, port, user, password)
stdin, stdout, stderr = ssh.exec_command("show arp")
for line in stdout.readlines():
print(line)
#BELOW IS THE ERRORS PYTHON THROWS
C:\Users\scott\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/scott/.PyCharmCE2019.2/config/scratches/scratch_6.py
Traceback (most recent call last):
File "C:/Users/scott/.PyCharmCE2019.2/config/scratches/scratch_6.py", line 8, in <module>
stdin, stdout, stderr = ssh.exec_command("show arp")
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\client.py", line 514, in exec_command
chan.exec_command(command)
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\channel.py", line 72, in _check
return func(self, *args, **kwds)
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\channel.py", line 257, in exec_command
self._wait_for_event()
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\channel.py", line 1226, in _wait_for_event
raise e
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\transport.py", line 2055, in run
ptype, m = self.packetizer.read_message()
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\packet.py", line 459, in read_message
header = self.read_all(self.__block_size_in, check_rekey=True)
File "C:\Users\scott\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\packet.py", line 303, in read_all
raise EOFError()
EOFError
Process finished with exit code 1 ```
您的 SSH 服务器可能不允许 exec_command()
。
您可以通过执行以下 ssh 命令来验证这一点:
ssh user@host c i eth i
如果你得到
"Connection to x.x.x. closed by remote host."
则不支持
您应该改为在 paramiko 中启动交互式 shell:
ssh = paramiko.SSHClient()
ssh.connect('credentials here')
chan = ssh.invoke_shell()
chan.send('command here\n')
output = chan.recv()