0

I need to run a Python script which will get the password from the user and login into the specific box using SSH.

password = getpass.getpass('password:')
...
...
command='ssh' + ' ' + "IPname"

p = subprocess.Popen(command,shell=True,stdout=PIPE).communicate(password)[0]

But the above script is not working and still it is looking for password.

4
  • Is IPname a variable that you've accidentally quoted? Commented Aug 11, 2015 at 14:27
  • you can use pyexpect to send the password when the process expects it. You could also just avoid using passwords and use public key authentification. Commented Aug 11, 2015 at 14:28
  • 1
    Don't do this. Use public keys to authenticate. It's a bad idea to hard-code passwords in scripts. Commented Aug 11, 2015 at 14:29
  • yes but i'm getting password from user and the script for just internal purpose. Commented Aug 25, 2015 at 7:16

1 Answer 1

4

Use paramiko like so, because you cannot use password authentication with subprocess, you would have to create and alias and do ssh somealias. Paramiko makes it easy to do the authentication without setting up an alias:

password = getpass.getpass()

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('some.host.com', username=username, password=password)

Or you could use a public key with paramiko as well:

pathToKey = "/some/path/here"

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('some.host.com', username=username, key_filename=pathToKey)
Sign up to request clarification or add additional context in comments.

4 Comments

You can ssh with subprocess, but I haven't tried/wouldn't want to try to do an interactive shell that way, but if you just want to execute a command on the remote system you can definitely use ssh with subprocess
@EricRenouf you can't use it if its a password authentication, you would have to set up an ssh alias and do ssh somealias
Or use something like sshpass around it, but I just wanted to clarify your statement "you cannot ssh using subprocess"
i need alternative of paramiko. could you pls more specific.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.