我有一个 Python 脚本,它随机选择一个端口并将其传递给 bash 命令:
#!/usr/bin/env python
import random
import os
# Select a port at random
port = ['22', '23', '24']
selected_port = (random.choice(port))
# print 'selected_port
bashCommand = "ssh -p '${selected_port}' -i pi.rsa [email protected]"
os.system(bashCommand)
将selected_port
变量传递给 my的正确方法是什么bashCommand
?目前我得到一个SyntaxError: EOL while scanning string literal
使用 Python 的字符串插值机制之一:
bashCommand = "ssh -p '%s' -i pi.rsa [email protected]" % selected_port
或者
bashCommand = "ssh -p '{}' -i pi.rsa [email protected]".format(selected_port)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句