I have a problem with a test suite. I use robot framework and python.I created a function in python which executes a console command in a remote Linux client.
def purge_default_dns(device_ip):
ssh_val = "usr1@" + device_ip
command = "ctool dns | grep -v \"grep\" | awk \'{print $2}\'"
test = check_output(["ssh", ssh_val, "-p", "6022", command])
The check_output()
function connects with device_ip
and executes command. If I try to connect with a fully qualified domain name (ex. my.domain.io
), then I get a prompt for password (which is empty). I press enter and command executes regular. Is there any parameter that passes for example Enter when password prompt comes up?
I tried ssh -e
switch , I don't want to change ssh client , I just need a generic solution.
For example using paramiko library in the code below , I can create an paramiko SSHClient
, which has a parameter for password and doesn't prompt anything. While I can't use paramiko right now , I need something else with SSHLirary to go around the problem.
def send_ssh_command(device_ip , command):
hostname = device_ip
password = ""
username = "usr1"
port = 6022
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
client.connect(hostname, port=port, username=username, password=password)
stdin , stdout , stderr = client.exec_command(command)
command_return_val = stdout.read()
finally:
client.close()
return command_return_val
Thank you.
To get this straight, the only solution you look for is to pass the password on the command line to the default OS ssh
client, and do not/cannot install any libraries (paramiko, etc) that can help you achieve the same result through other means?
I'm asking this, because the robot framework's SSHLibrary provides this out of the box; you already have the python's solution with paramiko
; and the general linux solution is to install the sshpass
package, and use it to pass the value:
sshpass -p "YOUR_PASS" ssh [email protected]:6022
So if all of these are not an option, you are left with two alternatives - either hack something around SSH_ASKPASS
- here's an article with a sample, or use expect
to pass it - this one is what I'd prefer out of the two.
Here's a very good SO answer with an expect
script wrapper around ssh
. In your method, you will have to first create a file with its content, set an executable flag on it, and then call that file in check_output()
, passing as arguments the password, 'ssh' and all its arguments.