3

I am trying to pass dict arguments using the ssh command via the os module:

os.system(f'ssh remote_host python -u - {dict1} {dict2} < local_script.py')

I am getting an error:

sh:line:0 syntax error near unexpected token ('

What is the right syntax to pass dict as an argument?

If I pass string instead of dict, it works fine.

Any suggestions?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Jagadeesh
  • 33
  • 4

1 Answers1

3

Use json and urlencode.

import urllib.parse
import json
dict1_j = urllib.parse.quote(json.dumps(dict1))
dict2_j = urllib.parse.quote(json.dumps(dict2))
os.system(f'ssh remote_host python -u - {dict1_j} {dict2_j} < local_script.py')

And you can use urldecode and json pharse to decode this in local_script.py

import json 
import urllib.parse 
dict1 = json.loads(urllib.parse.unquote(sys.argv[1])) 
dict2 = json.loads(urllib.parse.unquote(sys.argv[2]))