Handling special characters in password field of sshpass

11,144

Solution 1

Escape character worked in my case. Simply use \ character before '!' or other special characters.

Solution 2

Python does not like the special characters. Either escape the symbols by a trailing \ or use a raw string like in r"test!@#".

Solution 3

First of all, using sshpass with the option -p means that you publish your password to anyone on the same machine. It's like putting your house key under your rug.

The error message comes from the shell which is being used to execute the command above (probably BASH). The character ! is interpreted as special character and means "look in the history for the text after me".

The problem is most likely inside of the sshpass script (since you didn't specify shell=True in Popen()). You can try to fix the script by making sure that it uses proper quoting and escaping.

The other solution is to pass env={'SSHPASS': 'test!@#'} to Popen() to set the environment for sshpass as explained in the manpage:

cmd = ['sshpass', '-e', 'ssh', '-o', 'UserKnownHostsFile=/dev/null', ...]
env = os.environ.copy()
env['SSHPASS'] = 'test!@#'
Popen(cmd, env=end)

Note: You should split the "-o key=value" into "-o", "key=value".

Related:

Share:
11,144
Admin
Author by

Admin

Updated on June 17, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a Python script which uses sshpass for ssh access to a machine

    Popen(["sshpass","-p", "test!@#", "ssh", "-o UserKnownHostsFile=/dev/null", "-o StrictHostKeyChecking=no", "[email protected]"])
    

    But due to the presence of special characters in password field this command is throwing some error. Is there a way that I can use password with special characters in sshpass or anything else which can be called from Python?

    The error is: bash: !@#": event not found