-2

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

liborm
  • 2,454
  • 18
  • 30

1 Answers1

0

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)
Thomas
  • 162,537
  • 44
  • 333
  • 446