简体   繁体   English

当对期望脚本进行子流程调用时,Python避免使用花括号

[英]Python avoid curly braces when subprocess call is done to an expect script

I have the following expect script named autossh.exp 我有以下期望脚本名为autossh.exp

#!/usr/bin/expect -f
set user [lrange $argv 0 0] 
set password [lrange $argv 1 1] 
set ipaddr [lrange $argv 2 2]
set command [lrange $argv 3 3]
set timeout 10
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh -o StrictHostKeyChecking=no $user@$ipaddr 'bash -s' < $command
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
interact
# send blank line (\r) to make sure we get back to gui
#send -- "\r"

I am calling this expect script from a python file which is like below 我正在从下面的python文件中调用此期望脚本

import subprocess
user = "abc"
operation = "l"
password = "xyz"
brokerip = "10.XX.XX.XXX"
command = ' topics.sh '+operation+' '+user
subprocess.call(['autossh.exp', user, password, brokerip, command])

I want to pass the topics.sh (which is locally) found to ssh. 我想将找到的topic.sh(在本地)传递给ssh。

When i run the python file i get the below error. 当我运行python文件时,出现以下错误。

spawn ssh -o StrictHostKeyChecking=no abc@10.xx.xx.xxx 'bash -s' < { topics.sh l abc}
tmhaskar@xx.xx.xx.xxx's password: 
bash: {: No such file or directory

Can some one tell me is there any work around to avoid the curly braces. 有人可以告诉我是否有任何避免花括号的方法。

Thanks in advance. 提前致谢。

Try using lindex rather than lrange . 尝试使用lindex而不是lrange lrange attempts to return a well formed list, so if it finds certain characters in the data it will add braces or backslashes to guarantee you get a valid list even if it's a list with a single element. lrange尝试返回格式正确的列表,因此,如果它在数据中找到某些字符,它将添加大括号或反斜杠以确保您获得有效的列表,即使该列表只有一个元素也是如此。 lindex, on the other hand, returns a single element from a list. 另一方面,lindex从列表中返回单个元素。

% set argv [list abc xyz 10.xx.xx.xxx "topics.sh 1 abc"]
abc xyz 10.xx.xx.xxx {topics.sh 1 abc}
% lrange $argv 3 3
{topics.sh 1 abc}
% lindex $argv 3
topics.sh 1 abc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM