我有这个脚本:
#!/usr/bin/python
import subprocess
import sys
HOST="cacamaca.caca"
COMMAND="display mac-address 0123-4567-8910"
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if result == []:
error = ssh.stderr.readlines()
print >>sys.stderr, "ERROR: %s" % error
else:
print result
由于输出中存在空格和不同的行,因此它还会打印回车和换行符,因此结果并不干净:
['\r\n', 'cacamaca.caca\r\n', 'Info: The max number of VTY users is 10, and the number\r\n', ' of current VTY users on line is 2.\r\n', ' The current login time is 2017-07-20 20:10:54+03:00 DST.\r\n', '-------------------------------------------------------------------------------\r\n', 'MAC Address VLAN/VSI Learned-From Type \r\n', '-------------------------------------------------------------------------------\r\n', '0123-4567-8910 1234/- Eth-Trunk9 dynamic \r\n', '\r\n', '-------------------------------------------------------------------------------\r\n', 'Total items displayed = 1 \n', '\r\n', '']
我该如何删除'\ n'和'\ r \ n'或至少用空格替换它们,以使结果看起来像原始结果? 我确实阅读了很多关于这个问题的答案,但没有一个能帮助。