【问题标题】:The right way to send cqlsh command via ssh using PYTHON使用 PYTHON 通过 ssh 发送 cqlsh 命令的正确方法
【发布时间】:2021-12-20 08:30:45
【问题描述】:

我尝试使用 PYTHON 通过 ssh 发送 cqlsh 命令并不断收到类似的错误

  1. 输入 ':' 没有可行的替代方案

  2. dsecqlsh.py 端口无效

...

我搜索了包括堆栈溢出在内的互联网,没有给出答案。

def sshRemoteCmd(user,host,cmd):
    import subprocess
    import re
    x = subprocess.Popen("ssh {user}@{host} {cmd}".format(user=user, host=host, cmd=cmd), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
    result=''

    if not x:
        result = 'error'
    else:
        for item in x:
            result += item.decode('ascii')
    return result

cmd = f'''cqlsh -e "select * from MYTABLE where userid='12345';"'''
print(cmd)
result = sshRemoteCmd('root','hosts', cmd)
print(result)

【问题讨论】:

    标签: python ssh cqlsh


    【解决方案1】:

    在我尝试了所有技巧之后,这里是正确的答案

    1. 在 cqlsh cmd 中添加本地主机和端口号
    cqlsh localhosts 9042
    
    1. 添加双引号来包裹整个命令

    2. 用 \" 双重转义引号

    这是最后的正确代码

    def sshRemoteCmd(user,host,cmd):
        import subprocess
        import re
        x = subprocess.Popen("ssh {user}@{host} {cmd}".format(user=user, host=host, cmd=cmd), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
        result=''
    
        if not x:
            result = 'error'
        else:
            for item in x:
                result += item.decode('ascii')
        return result
    
    cmd = f'''"cqlsh localhost 9042 -e \\"select * from MYTABLE where userid='1234';\\""'''
    print(cmd)
    result = sshRemoteCmd('root','hosts', cmd)
    print(result)
    

    【讨论】:

      猜你喜欢
      • 2015-08-08
      • 2016-12-21
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多