I want to call a python script, script1.py, which takes a regex string as argument. This script then connects to another server and calls a script2.py.
script1.py:
regex = sys.argv[1]
log = os.popen('ssh otherserver python /home/log/scripts/script2.py \"%s\"' % regex)
for line in log:
print line.strip()
log.close()
script2.py:
regex = re.compile(sys.argv[1])
for line in ['[1','[ 2]',' a ']:
if regex.search(line):
print line.strip()
The problem I’m running into is that parentheses are breaking the script when run through SSH.
This works:
[foo@bar scripts]$ python script1.py “.*”
[1
[ 2]
a
This doesn’t:
[foo@bar scripts]$ python script1.py “(.*)”
bash: -c: line 0: syntax error near unexpected token('python /home/log/scripts/script2.py (.*)’
bash: -c: line 0:
Why isn’t the argument in the second example escaped? This does not occur if I call script2.py locally without SSH.
Update with solution:
Turns out the solution was using subprocess, but passing arguments didn’t work, so I had to format the last argument to include the escaped regex:
log = subprocess.check_output(['ssh', 'eu1', 'nice', 'python', '/home/log/scripts/script2.py \"%s\"' % regex])
print log.strip()
Try this: