【问题标题】:Passing local variable in python sshClient command在 python sshClient 命令中传递局部变量
【发布时间】:2014-04-04 06:07:58
【问题描述】:

我正在使用 python 2.7.6 编写脚本

问题陈述:- 在我的脚本中,我在运行时将我的 applicationId 提取到变量 say 'appid' 中 现在我必须在 SSHClient 命令中将其作为变量传递,以便在远程服务器中执行,在下面的 cmd 中

ssh_idb = SSHClient(cfg_cluster_ip)
ssh_idb.login_with_password(username=idb_ssh_user,password=idb_ssh_password)
cmd ='ps -ef | grep "Admin|$appid"'
out, err, code = ssh_idb.execute_sudo_command(["%s" % cmd])
print out

它返回空白输出[预期值未在 cmd 中传递,shell 不返回任何数据] 有没有办法在cmd中动态传递局部变量值?

【问题讨论】:

    标签: python python-2.7 command-line-arguments


    【解决方案1】:

    这个问题很不清楚。你的意思是appid 是 Python 脚本中的局部变量吗?如果是这样,您需要将其传递给命令字符串:

    cmd ='ps -ef | grep "Admin|%s"' % appid
    

    【讨论】:

    • 是的,这解决了我的问题!谢谢。此外,我们可以使用 cmd = "ps -ef | grep 'CA|" 找出另一种方法+ str(appid) + "'"
    猜你喜欢
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    相关资源
    最近更新 更多