我正在使用paramiko todo远程SSH
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username='root', key_filename='/root/.ssh/mykey.priv')
stdin, stdout, stderr = ssh.exec_command('firewall-cmd --state')
print stdout.readlines()
我得到的输出为
[u'running\n']
如何解析它只打印为“正在运行”?
答案 0 :(得分:0)
我现在正在尝试paramiko并使用recv,根据此处的文档http://docs.paramiko.org/en/2.1/api/channel.html它将返回一个字节。所以我所做的就是将它转换为字符串,见下文
myconn = paramiko.SSHClient()
myconn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
myconn.connect('192.168.15.254', port=22, username='test',
password='test', look_for_keys=False,
allow_agent=False)
myshell = myconn.invoke_shell()
myshell.send('show int ATM 0/0/0 | include put rate \n')
time.sleep(1)
output = myshell.recv(65535)
myconn.close()
print(output)
mystr = output.decode(encoding='UTF-8')
print(mystr)
打印输出会给你这个;
b'\r\nROUTER#show int ATM 0/0/0 | include put rate \r\n 30 second input rate 47000 bits/sec, 37 packets/sec\r\n 30 second output rate 38000 bits/sec, 33 packets/sec\r\nROUTER#'
打印mystr会给你这个;
ROUTER#show int ATM 0/0/0 | include put rate
30 second input rate 18000 bits/sec, 16 packets/sec
30 second output rate 15000 bits/sec, 13 packets/sec
ROUTER#
我希望这至少会让你走上正确的道路,如果还不晚。
答案 1 :(得分:0)
尝试以下代码:
print(stdout.read().decode('ascii'))
我相信这会解决您的问题。