I am using a robot that is connected to a remote pc using localhost. I have used the Paramiko library to send commands from remote pc to my robot. I want to publish a number on /activate topic like this:
rostopic pub /activate std_msgs/Int16 "data: 1"
The problem is not my connection because the above command works using PuTTY or SSH.
I have written the following code:
import paramiko
host = "xxx.xxx.x.x"
port = 22
username = "robot"
password = "xxx"
command = "rostopic pub /activate std_msgs/Int16 'data: 1'"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password)
stdin, stdout, stderr = ssh.exec_command(command)
But using this code, I see no results in my robot. Can anyone help?
If you have problems with executing commands, you need to read their (error) output to see any error messages.
Also your current code does not even complete the command. And reading the command output is the actually easiest way in Paramiko to actually get your command completed.
See Wait to finish command executed with Python Paramiko
For another common problem, see Some Unix commands fail with "<command> not found", when executed using Python Paramiko exec_command
Related
I am trying to use Paramiko to SSH into a Brocade switch and carry out remote commands. The code is as given below:
def ssh_connector(ip, userName, passWord, command):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=userName, password=passWord, port=22)
stdin, stdout, stderr = ssh.exec_command(command)
print stdout.readlines()
ssh_connector(ip, userName, passWord, 'show running-config')
While trying to run the code, I encounter a strange error which is as given below.
Protocol error, doesn't start with scp!
I do not know the cause of the error or whether the SSH connection was successful. Could you please help me with this?
If the SSHClient.exec_command does not work, the first thing to test is to try (on one line):
ssh user#host command
That will use the same SSH API (the "exec" channel) as SSHClient.exec_command. If you are on Windows, you can use plink (from PuTTY packages) instead of ssh. If ssh/plink fails too, it indicates that your device does not support the SSH "exec" channel.
In your case, it seems that the "exec" channel on Brocade SSH server is implemented to support the scp command only.
As you claim to be able to "SSH" to the switch, it seems that the "shell" channel is fully working.
While it is generally not recommended to use the "shell" channel for command automation, with your server you won't have other option. Use the SSHClient.invoke_shell and write the commands to the channel (= to the shell) using the Channel.send.
channel = ssh.invoke_shell()
channel.send('ls\n')
channel.send('exit\n')
See also What is the difference between exec_command and send with invoke_shell() on Paramiko?
A similar question on C#/SSH.NET: SSH.NET is not executing command on device.
Obligatory warning: Do not use AutoAddPolicy – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".
I am trying to use Paramiko to SSH into a Brocade switch and carry out remote commands. The code is as given below:
def ssh_connector(ip, userName, passWord, command):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=userName, password=passWord, port=22)
stdin, stdout, stderr = ssh.exec_command(command)
print stdout.readlines()
ssh_connector(ip, userName, passWord, 'show running-config')
While trying to run the code, I encounter a strange error which is as given below.
Protocol error, doesn't start with scp!
I do not know the cause of the error or whether the SSH connection was successful. Could you please help me with this?
If the SSHClient.exec_command does not work, the first thing to test is to try (on one line):
ssh user#host command
That will use the same SSH API (the "exec" channel) as SSHClient.exec_command. If you are on Windows, you can use plink (from PuTTY packages) instead of ssh. If ssh/plink fails too, it indicates that your device does not support the SSH "exec" channel.
In your case, it seems that the "exec" channel on Brocade SSH server is implemented to support the scp command only.
As you claim to be able to "SSH" to the switch, it seems that the "shell" channel is fully working.
While it is generally not recommended to use the "shell" channel for command automation, with your server you won't have other option. Use the SSHClient.invoke_shell and write the commands to the channel (= to the shell) using the Channel.send.
channel = ssh.invoke_shell()
channel.send('ls\n')
channel.send('exit\n')
See also What is the difference between exec_command and send with invoke_shell() on Paramiko?
A similar question on C#/SSH.NET: SSH.NET is not executing command on device.
Obligatory warning: Do not use AutoAddPolicy – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".
I'm currently trying to write an airflow job that will allow me to ssh into an EC2 instance and then start an sftp session with another host from within this EC2 box. My current code that I have is as follows:
def run_ssh():
hook = SSHHook(ssh_conn_id='xyz').get_conn() #returns an ssh client
stdin, stdout, stderr = hook.exec_command('sftp user#host.com;')
# This next step prompts me for password so i provide it
stdin.write('password')
logging.info(stdout.readlines())
stdin, stdout, stderr = hook.exec_command('ls')
logging.info(stdout.readlines())
When i print the final line i should be seeing some folders but instead just see ['a\n']... so it seems I'm not actually able to sftp. Are there better ways to sftp from a remote host through a python script running locally.
Any help with this is appreciated. The answer can be geared towards a simple python script as opposed to airflow.
For your literal question, see:
Pass input/variables to command/script over SSH using Python Paramiko
Though implementing an SFTP over jump host this way is not a good solution.
Use port forwarding instead:
Nested SSH using Python Paramiko
Port forwarding and the open SFTP using Python Paramiko
This is my first time using paramiko. I'm trying to establish an SSH session to a test Amazon Linux 2 instance where I've enabled password authentication, since that doesn't come enabled by default and restarted the SSH daemon on the box. I also made sure that I could connect with SSH via the normal SSH program using the username / password I put in the Python program.
When I run the Python code below, everything looks good and it waits for input and keeps the program running, but when I'm logged into the Amazon instance, I don't see the paramiko user logged in (I did a "w" and a "who" command). In fact, I have no evidence server-side that Paramiko ever connects successfully to begin with.
#!/usr/bin/env python3
import pprint
import boto3
import os
import paramiko
os.system('clear')
pp = pprint.PrettyPrinter(indent=4)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('X.X.X.X',username='the_username',password='the_password',port=22)
get_input = input("Preventing program from closing and keeping SSH connectiion alive...")
who shows interactive shell sessions only.
Your code only connects. It does not start a shell, let alone an interactive shell.
See List all connected SSH sessions?
Use case
On a unix server , when login manually ,opens a command shell of its own to run the command.
I am trying to automate this by using paramiko , however , somehow i am not able to execute the command on command shell using paramiko
What i have done ?
I created a simple script which is able to make connection, but its not executing command on Vshell as the ouput is always coming empty.
import paramiko
import sys
ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=sys.argv[1],port=sys.argv[2],username=sys.argv[3],password=sys.argv[4])
command="show hwid"
stdin,stdout,stderr=ssh_client.exec_command(command)
out=stdout.read()
print out
err=stderr.read()
print err
ssh_client.close()
The same script runs perfectly fine , when its used on server where vshell is not being used
Anyhelp or suggestion on this?
stdin,stdout,stderr=ssh_client.exec_command(command)
Regarding this line of code, I suspect that the SSH server is not properly configured to allow commands to be executed in this way (this is the equivalent of ssh myserver show hwid, rather than typing it into the terminal after login).
You might want to imitate the behaviour of typing the command in after logging into the server, and for that I think this is appropriate:
shell = ssh_client.invoke_shell()
stdin, stdout, stderr = shell.exec_command(command)