1

I want to SSH using a user and password. The format of SSH that works is :-

ssh admin@IP Are you sure you want to continue connecting (yes/no)? yes Password:

And the password is required.. so how should i design the script so that it could take user Input as the IP.?

2
  • There's no reason to use the external ssh command when native-Python SSH libraries exist (and give you full control over the process, including authentication); see f/e paramiko. Commented Feb 15, 2019 at 5:53
  • Beyond that, this doesn't tell us what you tried, and doesn't tell us what specific problem you encountered. See How to Ask in the Help Center. Commented Feb 15, 2019 at 5:54

2 Answers 2

3

I hope this will help you achieve what you are looking for using Paramiko.

import paramiko
import time

ip = input("Please enter IP")
name = input("Please enter UserName")
password = input("Please enter Password")
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip,username=name,password=password)   #This is used to establish a connection

remote_connection = ssh_client.invoke_shell() #This helps you invoke the shell of the client machine

remote_connection.send("cli\n")           #These commands are used to send command over
remote_connection.send("configure\n")     #to the remote machine that you are trying to connect with


time.sleep(5) 
output = remote_connection.recv(10240)  #This is to recieve any output that you get on the after SSH
                                        #connection is established

ssh_client.close                        #This closes your active SSH connection

For official documentation please read here.

Sign up to request clarification or add additional context in comments.

Comments

2

Do pip install fabric

See fabric

Example

from fabric import Connection
result = Connection('web1.example.com').run('uname -s', hide=True)
msg = "Ran {0.command!r} on {0.connection.host}, got stdout:\n{0.stdout}"
print(msg.format(result))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.