-1

I have written a simple code wherein I invoked ssh to one of my lab Device ip through os.system. Now the problem for me is how do I supply a password? Can I do it via python?

Below is the code that I have been using:

import os
os.system("ssh 192.168.1.100")

I tried to understand how the os module works but so far am not able to supply password argument - how do I supply a password via python to this program?

My environment is Bash.

Ajean
  • 5,528
  • 14
  • 46
  • 69
Rakesh M
  • 1
  • 3
  • Possible duplicate of [Command Line Arguments In Python](http://stackoverflow.com/questions/1009860/command-line-arguments-in-python) – Ajean May 08 '17 at 15:51

1 Answers1

0

Install sshpass, then launch the command with os.system like that:

os.system("sshpass -p \'yourpassword\' ssh -o StrictHostKeyChecking=no yourusername@hostname")

Or you can use a fabric script to copy your public key into the system then you will not need a password for connection

from fabric.api import env, sudo, run
def copy_pub_key(ip, user, password, pub_key):

    env.host_string = ip
    env.user = user
    env.password = password

    if user == "root":
        sudo('echo \'{pub_key}\' >> /root/.ssh/authorized_keys'.format(
            pub_key=pub_key,
            user=user
        ))
    else:
        sudo('echo \'{pub_key}\' >> /home/{user}/.ssh/authorized_keys'.format(
            pub_key=pub_key,
            user=user
        ))
Ch.Hedi
  • 128
  • 1
  • 7
  • Thank you for the response. ultimate goal is to automate a login and in a kind of official environment i cannot add any packages apart from what system give me so. – Rakesh M May 08 '17 at 15:54
  • Then you should use fabric script to copy the public key into the system then you can connect without password. – Ch.Hedi May 08 '17 at 16:14