前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >记一次ssh.exec_command(cmd)执行后读取结果为空

记一次ssh.exec_command(cmd)执行后读取结果为空

作者头像
未来sky
发布2019-09-19 11:24:00
1.9K0
发布2019-09-19 11:24:00
举报
文章被收录于专栏:好好学习吧
代码语言:javascript
复制
# 连接跳板机,执行插标签
def con_tmp_machine(mobile_phoneno, myguid):
    keyfile = os.path.expanduser('/Users/kusy/.ssh/id_rsa')
    password = keyring.get_password('SSH', keyfile)
    key = paramiko.RSAKey.from_private_key_file(keyfile, '')

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname='88.88.888.88', port=10022, username='kusy', password="kusy@cys", pkey=key)
    mobile_phoneno = mobile_phoneno
    mobile_phone_tail = mobile_phoneno[-1]
    user_guid = myguid
    sex = "0" if int(mobile_phone_tail) % 2 == 0 else "1"
    add_tag_raw = r'''
        curl -XPOST -H 'Content-Type:application/json' 'xxx.cn:9200/xx_label/job' -d '{
      "user_guid": "$user_guid",
      "mobile_phone_tail": "$mobile_phone_tail",
      "mobile_phone": "$mobile_phoneno",
      "sex":"$sex"
    }'
        '''
    add_tag = add_tag_raw.replace('$mobile_phoneno', mobile_phoneno)\
        .replace('$mobile_phone_tail', mobile_phone_tail)\
        .replace('$user_guid', user_guid)\
        .replace('$sex', sex)

    search_tag_raw = '''curl -XPOST -H "Content-Type:application/json" "xxx.cn:9200/xx_label/job/_search" -d '{"query":{"query_string":{"query":"$mobile_phoneno"}}}'
    '''
    search_tag = search_tag_raw.replace('$mobile_phoneno', mobile_phoneno)

    # 查询标签、加标签流程控制
    result = ''
    err_flag = False
    if user_guid == '':
        cmd = ssh.exec_command(search_tag)
    else:
        cmd = ssh.exec_command(add_tag)
        # Alias [xx_label] has more than one indices
        stdin, stdout, stderr = cmd
        result = stdout.read().decode()
        # print('result', result)
        if result.__contains__('Alias [xx_label] has more than one indices'):
            xx_label = re.compile('(xx-\d{8})').findall(result)
            new_xx_label = sorted(xx_label)[-1]
            print('**new_xx_label**', new_xx_label)
            cmd = ssh.exec_command(add_tag.replace('xx_label', new_xx_label))
        else:
            err_flag = True
            print('add normaly..')
            pass
    if result != '' and err_flag:
        return result
    else:
        stdin, stdout, stderr = cmd
        return stdout.read().decode()

    print("\nssh execute over")

    # 关闭连接
    ssh.close()

绿色背景的代码是修改后的逻辑,原先出问题的代码就是去掉这部分的。数据走的是黄色else的逻辑,每次走完return的结果都是'',调试的时候打印的result内容也正常,百思不得其解,debug的时候发现cmd对象也正常,但是内容变为空了。想着难道是因为cmd的stdout已经被读取一次(粉色背景的cmd对象),再次读取其实是继续读文件,所以是空。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-09-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档