我有一个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
变量传递给我的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)