I have a problem for several days. On my Raspberry Pi I go to the terminal for executing the following command:
scp user#subdomain.server.com:/home/path/to/file/test.py /home/pi/update/test.py
It works fine and copies the file from the server without password (because of ssh-keys) to my local machine.
BUT: I have to do exactly the same within a python script:
import os
cmd = 'scp user#subdomain.server.com:/home/path/to/file/test.py /home/pi/update/test.py'
os.system(cmd)
This doesn't work giving an error
ssh: Could not resolve hostname subdomain.server.com: Temporary failure in name resolution
Why do I get this message and especially how can I solve it? Does anyone give an advice, please? I don't understand it ...
Related
I'm trying to use fabric in python to send commands to a program on another machine.
This code works fine:
from fabric.api import env, run
env.host_string = 'xxx.xxx.xxx.xxx'
env.user = 'username'
env.password = 'password'
run('ls')
But when running
run('rostopic list')
I get
Warning: run() received nonzero return code 127 while executing 'rostopic list'!
'/bin/bash: rostopic: command not found'
However on the machine itself if I run
rostopic list
it runs as it's supposed to
I'm not sure how to proceed and I don't understand why it's not working with fabric. FYI I tried implementing this with paramiko but I also run into problems, however it works fine with pxssh. The issue is that I need this to work on Windows and pxssh isn't compatible. How can I make this command work with fabric?
From the comments you've made about the pathing issues, it sounds like you will need to use some combination of the path, cd, and/or prefix context managers in order to run ROS (Robot Operating System) commands over an SSH connection. You might also wish to troubleshoot by getting Fabric completely out of the picture, and instead work on getting the command to work via ssh -t, like so:
$ ssh user#machine -t "cd /my/home/directory && /opt/ros/indigo/bin/rostopic list"
An example using context managers with Fabric would look like this:
with path('/opt/ros/indigo/bin/'):
with prefix('always run this command'):
with cd('/my/special/directory'):
run('rostopic list')
That's pretty contrived, but hopefully illustrates the point. Regardless, I would first make sure that you can run the command via ssh -t. Solving that problem will likely lead you to the correct way to make this happen with Fabric.
As a side/related consideration: is this a virtual environment on your remote machine? You could use the prefix context manager to activate it, like so:
with prefix('/opt/ros/indigo/bin/activate'):
run('rostopic list')
Or using ssh -t, you could execute:
$ ssh user#machine -t "/opt/ros/indigo/bin/activate && rostopic list"
I am writing a tkinter script which accepts a linux command from the UI. Once the submit button is clicked,I want to run this command that I got from the UI on a remote linux server and collect the output and display on the Tkinter window.
Regarding the tkinter parts, i am doing fine. the problem is when I wanna execute this command on the remote server and collect the output.
I will be running my program in a Virtual Environment of Conda
on a small research on google the most common solution I saw was the usage of fabric as shown here in this link
other solutions which I saw was the usage of plink and popen.
But I am not clear as to where I will mention the host name and password to connect to the remote server with any of the solutions stated above. How I will send my command and receive the output back to my tkinter flow. Is there way that I can understand if the command I executed failed because of some reason?
Any help on this will be appreciated! thanks in advance
So i found the solution i was looking for.. may be i did not read the docs which i surfed before properly..
here its
def connect():
print "starting to connect"
env.host_string = 'vpnuser#10.0.0.70'
env.password = "vpnuser "
run("ls -lrth")
put this in a fabfile.py file and run it like this
fab -a connect
That is it !
You want to use host_string to dynamically specify what the destination is. Change it before executing commands.
An example would be:
from fabric.api import run, env
env.host_string = 'user#host.com'
def ls():
run("ls")
I'm connected to a Raspberry Pi via ssh. I'm trying to run a script on the RPi but rather than using a nano editor on the terminal I want to execute a python script based on my mac (the server of the SSH).
However when typing
'sudo python /Users/User/Pythonscript.py'
terminal returns
'python: can't open file '/Users/User/Pythonscript.py': [Errno 2] No such file or directory'
And yet this file does exist under that directory.
Any ideas?
Ok, if I have properly understood, you have script on your client and want to execute it on Pi from ssh.
scp /path/to/script.py user#hostname:/path/to/
then add your rsa key to your server. This perfect guide tells you how.
Then just write .sh, which get access to your server ssh user#hostname, then cd /path/to and, finally python script.py
You have a file on your Mac and want to execute it on your Pi. Two things need to be done: First, get the file to the Pi. Second log in to the Pi and run it. Apparently, you managed step two, so I'll address step 1.
The simple solution: scp, e.g. scp /Users/User/Pythonscript.py <user>#<ip_of_pi>:<target_dir>
The solution that might be longer-term more feasible, if you want to develop locally: sshfs. It's available via Homebrew. You mount a directory locally, and anything you change will automatically be reflected on the respective directory on the Pi. Here's a tutorial how to install and use sshfs. On first glance, it seems reasonable.
No matter how you get your script to the Pi, you need to find it on the Pi and execute it there.
I need to run the windows command "XCOPY" on remote machine.
I am using paramiko module to connect to remote machine and run, but I am getting error like invalid path even though the path is valid and same command works if I run on remote command prompt manually. But not possible through paramiko.
Can anybody help?
Regards,
Arun
May be you can get help from:
http://support.microsoft.com/kb/192808
hava a look.
I am trying to open an interactive ssh shell through fabric.
Requirements:
Use fabrics hosts in the connection string to remote
Open fully interactive shell in current terminal
Works on osx and ubuntu
No need for data transfer between fabric/python and remote. So fabric task can end in background.
So far:
fabfile.py:
def test_ssh():
from subprocess import Popen
Popen('ssh user#1.2.3.4 -i "bla.pem"', shell=True)
In terminal:
localprompt$ fab test_ssh
localprompt$ tcsetattr: Input/output error
[remote ubuntu welcome here]
remoteprompt$ |
Then if I try to input a command on the remote prompt it is executed locally and I drop back to the local prompt.
Does anyone know a solution?
Note: I am aware of fabrics open_shell, but this does not work for me since the stdout lags behind, rendering this unusable.
A slight modification does the trick:
def test_ssh():
from subprocess import call
call('ssh user#1.2.3.4 -i "bla.pem"', shell=True)
As the answer to this question suggests the error suggests the error comes from the inability of ssh to connect to the stdin/out of a process in the background.
With call the fabric task does not end in the background, but I am fine with that as long as as it is not interfering with my stdin/out.