我正在创建一个小脚本,我想使用 python 编程语言将一个变量传递给 bash 命令,例如:
number = raw_input("digit: ")
然后我想获取数字变量并将其放入 bash 命令中,例如:
ssh 'foo%s.bar'(number) <- where the %s is located id like it be replaced with the input
最后,我想采用它并在 python 脚本中的 bash 命令中运行它:
ssh foo45.bar
我怎样才能让它工作?
import subprocess
number = raw_input("digit: ")
subprocess.call(('ssh', 'foo{}.bar'.format(number)))
要获得一些好的阅读,请尝试 string formatting 的 python 文档和 subprocess .
如果要将上面的内容与 sshpass 集成,请将子进程调用替换为:
subprocess.call(('sshpass', '-p', 'YOUR_PASSWORD', 'ssh', '-o', 'StrictHostKeyChecking=no', 'foo{}.bar'.format(number)))