citizenshell.SecureShell()方法返回值类型转换

关于citizenshell.SecureShell()方法返回值类型转换

背景
使用citizenshell.SecureShell()连接远程的一台Linux机器,然后在shell里去读一个文件并且统计该文档中某个关键字出现的次数。后面需要使用这个次数进行算数运算。

def Count_words(count_word):
    shell = citizenshell.SecureShell(hostname=ip, username='xxxxx', password='xxxxx')
    out = shell("grep '%s' %s | wc -l"%(count_word,logfile))
    return out

num=Count_words(count_word1)-Count_words(count_word2)

运行以上代码报错如下:TypeError: unsupported operand type(s) for -: 'ShellResult' and 'ShellResult',然后尝试把shell返回值转int,修改如下代码:

def Count_words(count_word):
    shell = citizenshell.SecureShell(hostname=ip, username='xxxxx', password='xxxxx')
    out = shell("grep '%s' %s | wc -l"%(count_word,logfile))
    out = int(out)
    return out

运行以上代码报错如下:AttributeError: ShellResult instance has no attribute '__trunc__'查了一下shell返回值的类型<class 'citizenshell.shellresult.ShellResult'>,看着是个特殊的类型,反正不能转int
之前只用过strint,遇到这种情况毫无头绪啊~~
灵光一闪,是不是可以将shell返回值先转str再转int呢~ 尝试一下~

def Count_words(count_word):
    shell = citizenshell.SecureShell(hostname=ip, username='xxxxx', password='xxxxx')
    out = shell("grep '%s' %s | wc -l"%(count_word,logfile))
    out = int(str(out))
    return out
num=Count_words(count_word1)-Count_words(count_word2)

完美解决~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值