I have a Python script which selects a port at random and passes it to the bash command:
#!/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 pi@192.168.1.xx"
os.system(bashCommand)
What is the correct way to pass selected_port
variable to my bashCommand
? Currently I'm getting a SyntaxError: EOL while scanning string literal
Use one of Python's string interpolation mechanisms:
bashCommand = "ssh -p '%s' -i pi.rsa pi@192.168.1.xx" % selected_port
or
bashCommand = "ssh -p '{}' -i pi.rsa pi@192.168.1.xx".format(selected_port)