如何 ssh.invoke_shell() 在 Linux 下工作
How to ssh.invoke_shell() work under Linux
import paramiko
import os
import sys
#Data conection
ssh_servidor = 'xxx.xx.xx.xxx'
ssh_usuario = 'yyy'
ssh_clave = 'pws'
ssh_puerto = yy
# Conect to the server
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Open a server session
ssh.connect(ssh_servidor, ssh_puerto, ssh_usuario, ssh_clave)
channel = ssh.invoke_shell()
channel.send('clear')
channel.send('\n')
channel_data = str()
while True:
if channel.recv_ready():
enter code herechannel_data += channel.recv(9999)
print '############ Device Ouput ##############'
print channel_data
print '########################################'
else:
continue
if channel_data.endswith('xxxx@yyyyy~$'):
channel.send('ls | grep mmmm')
channel.send('\n')
问题是理论上 var channel_data 必须有并打印
这个:
############ Device Ouput ##############
Linux tito 2.6.32-5-amd64 #1 SMP Fri May 10 08:43:19 UTC 2013 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Dec 14 10:01:15 2017 from .xx.xx.xxx
xxxx@yyyyy~$
########################################
除此之外,打印如下:
############ Device Ouput ##############
Linux tito 2.6.32-5-amd64 #1 SMP Fri May 10 08:43:19 UTC 2013 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Dec 14 10:01:15 2017 from .xx.xx.xxx
########################################
主机"xxxx@yyyyy~$"一直没有出现,所以我查询时的部分代码:
if channel_data.endswith('xxxx@yyyyy~$'):
从来没有发生过。
与我看到的 paramiko 库示例的唯一区别是代码在 Windows 上运行。
所以问题是,此时 win 和 Linux 有什么区别?
为什么 S.O。不显示主机 xxxx@yyyyy~$
?
有什么建议吗?
我找到了答案:
channel_bytes = channel.recv(9999)
channel_data = channel_bytes.decode("utf-8")
否则,人们会运行使用字节(来自缓冲区)作为字符串。
谢谢大家!!
import paramiko
import os
import sys
#Data conection
ssh_servidor = 'xxx.xx.xx.xxx'
ssh_usuario = 'yyy'
ssh_clave = 'pws'
ssh_puerto = yy
# Conect to the server
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Open a server session
ssh.connect(ssh_servidor, ssh_puerto, ssh_usuario, ssh_clave)
channel = ssh.invoke_shell()
channel.send('clear')
channel.send('\n')
channel_data = str()
while True:
if channel.recv_ready():
enter code herechannel_data += channel.recv(9999)
print '############ Device Ouput ##############'
print channel_data
print '########################################'
else:
continue
if channel_data.endswith('xxxx@yyyyy~$'):
channel.send('ls | grep mmmm')
channel.send('\n')
问题是理论上 var channel_data 必须有并打印 这个:
############ Device Ouput ##############
Linux tito 2.6.32-5-amd64 #1 SMP Fri May 10 08:43:19 UTC 2013 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Dec 14 10:01:15 2017 from .xx.xx.xxx
xxxx@yyyyy~$
########################################
除此之外,打印如下:
############ Device Ouput ##############
Linux tito 2.6.32-5-amd64 #1 SMP Fri May 10 08:43:19 UTC 2013 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Dec 14 10:01:15 2017 from .xx.xx.xxx
########################################
主机"xxxx@yyyyy~$"一直没有出现,所以我查询时的部分代码:
if channel_data.endswith('xxxx@yyyyy~$'):
从来没有发生过。
与我看到的 paramiko 库示例的唯一区别是代码在 Windows 上运行。
所以问题是,此时 win 和 Linux 有什么区别?
为什么 S.O。不显示主机 xxxx@yyyyy~$
?
有什么建议吗?
我找到了答案:
channel_bytes = channel.recv(9999)
channel_data = channel_bytes.decode("utf-8")
否则,人们会运行使用字节(来自缓冲区)作为字符串。
谢谢大家!!