4

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", "user1@192.168.10.1"])

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

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
Manoj
  • 41
  • 1
  • 1
  • 2

3 Answers3

3

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

2

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

usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • There is nothing to do with python.It's problem with sshpass.Throwing error bash: !@#": event not found – Manoj Sep 30 '14 at 04:27
  • And why do you not add this piece of information to your question? How should we know? – usr1234567 Sep 30 '14 at 06:25
  • Sorry for that.When I posted this question I don't have that error statement.Now the issue is solved by adding +'\r' to the trail of password field.Thanks... – Manoj Sep 30 '14 at 07:07
  • Ok, create an answer how you solved your problem, than accept it. This way others can profit from what you have found and the question will be marked as solved. – usr1234567 Sep 30 '14 at 09:42
2

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:

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820