我使用SSH登录到远程节点,获取服务的状态并希望打印它。Python - 摆脱不必要的输出
在我的远程节点上运行bash命令yield。
[[email protected] ~]# redis-cli -a '!t3bmjEJss' info replication | grep role | cut -d':' -f2
slave
是香港专业教育学院编写的Python代码是
def serviceDetails(ip,svc):
if svc == 'redis-server':
ssh = subprocess.Popen(["ssh", "%s" % ip, "redis-cli -a '!t3Z9LJt2_wmUDbmjEJss' info replication | grep role | cut -d':' -f2"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
print (result)
else:
print ("Redis service is not running on this node")
,我从result
变量中获得的输出为:
[b'slave\r\n']
为什么所有这些多余的字符出现?我该如何摆脱它们?
'result = result.encode('utf-8')。strip()' – zondo
@zondo这是一个列表吗?由于现在即时得到 回溯(最近通话最后一个): 文件“”,1号线,在 AttributeError的:“名单”对象有没有属性“编码” 。结果是一个列表。 –
对不起,不。这是'stdout'中的每一行。你会希望'result = [line.encode('utf-8')。strip()for line]' – zondo