a way to execute commands over ssh with python with standard packages - python

I'm trying to find a way to execute commands with python over ssh. I did find this answer, however I'm working on an embedded platform and can't install new packages. In other words I can't install paramiko. Is there a way to do what paramiko does but with standard packages?

Is it a Unix based system with an ssh client? If so, could you use subprocess, https://docs.python.org/3/library/subprocess.html, to spawn another process and run ssh commands?
If not, have you tried to see if you can package paramiko itself with your code and deploy everything together to the target system? I'm not sure if this works, but it may be worth a shot.

Related

Batch SFTP commands with Python and Paramiko

I am porting some batch jobs from Window to Linux. These are .bat scripts on Windows and I am rewriting them in Python to run on Linux
On Windows, we currently use putty to SFTP files and run commands like this:
psftp user#host -i privatekey.ppk -batch -b script.txt
This executes the putty SFTP commands found in script.txt in batch. Example below:
SCRIPT.TXT example
cd mydir
lcd outbox
get myremotefile.txt mylocalfile.txt
get myotherremotefile.txt myotherlocalfile.txt
bye
I am trying to find a way to use the same batch script (script.txt) in my python script to minimise the amount of changes overall. I've been looking into paramiko but so far I have not been able to find a way to execute the sftp commands in bulk via a script file.
An alternative would be to spawn a sub-process to execute the sftp command with the -b option but would prefer a python native solution if possible.
What do you think? Are there other options out there to solve this?
The commands in you script are psftp commands. Not "SFTP commands" (there are no SFTP commands). In general, no other application nor library understands them. Except for OpenSSH sftp, as PuTTY psftp was made to be somewhat compatible.
If you want to use a native Python SFTP library, like Paramiko or pysftp, you will need to use its API. And that means rewriting your code. It's not difficult.

How to use fabric with ssh keys?

Using fabric 2.4 and trying to set up ssh keys to remotely connect to various linux servers?
Python is new to me, I've followed example on this site and Read python doc but still unclear to me.
Currently running python on windows and my script is able to connect to remote linux servers because i have connection string defined as follows:
ssh_connect = Connection(host='servername', user='user1', connect_kwargs={'password': 'blahblah'})
I am running python script from my window server and instead of defining the connection string, I would like it to use ssh key. I have the id_rsa.pub file from the linux server. I would like to setup up on my windows box and have the script use that for connections?
You can always just pass a key via Fabric through it's command line arguments. This is definitely kind of a vague question, especially since it's running fabric on Windows, which to my knowledge is not technically supported by them, though it does work.
fab -i /Path/to/key
Source: fab --help and the official documentation here

Run Spyder /Python on remote server

So there are variants of this question - but none quite hit the nail on the head.
I want to run spyder and do interactive analysis on a server. I have two servers , neither have spyder. They both have python (linux server) but I dont have sudo rights to install packages I need.
In short the use case is: open spyder on local machine. Do something (need help here) to use the servers computation power , and then return results to local machine.
Update:
I have updated python with my packages on one server. Now to figure out the kernel name and link to spyder.
Leaving previous version of question up, as that is still useful.
The docker process is a little intimidating as does paramiko. What are my options?
(Spyder maintainer here) What you need to do is to create an Spyder kernel in your remote server and connect through SSH to it. That's the only facility we provide to do what you want.
You can find the precise instructions to do that in our docs.
I did a long search for something like this in my past job, when we wanted to quickly iterate on code which had to run across many workers in a cluster. All the commercial and open source task-queue projects that I found were based on running fixed code with arbitrary inputs, rather than running arbitrary code.
I'd also be interested to see if there's something out there that I missed. But in my case, I ended up building my own solution (unfortunately not open source).
My solution was:
1) I made a Redis queue where each task consisted of a zip file with a bash setup script (for pip installs, etc), a "payload" Python script to run, and a pickle file with input data.
2) The "payload" Python script would read in the pickle file or other files contained in the zip file. It would output a file named output.zip.
3) The task worker was a Python script (running on the remote machine, listening to the Redis queue) that would would unzip the file, run the bash setup script, then run the Python script. When the script exited, the worker would upload output.zip.
There were various optimizations, like the worker wouldn't run the same bash setup script twice in a row (it remembered the SHA1 hash of the most recent setup script). So, anyway, in the worst case you could do that. It was a week or two of work to setup.
Edit:
A second (much more manual) option, if you just need to run on one remote machine, is to use sshfs to mount the remote filesystem locally, so you can quickly edit the files in Spyder. Then keep an ssh window open to the remote machine, and run Python from the command line to test-run the scripts on that machine. (That's my standard setup for developing Raspberry Pi programs.)

How to use ssh with python on windows without paramiko?

On machine A I want to have a small python script to execute a command on machine B. Machine A is Windows with freesshd running on it, and machine B can be either Windows or Linux. I only have a username and a password to log in to machine B, no other way of authentication.
How can I create a python script to run a command on machine B? ssh would be fine, but where to find that command on Windows? And it has to be done without paramiko, since this module does not wok for unknown reasons.
If you can't get paramiko working, just do it manually with Popen: http://python-for-system-administrators.readthedocs.org/en/latest/ssh.html
I haven't tried this on Windows but to handle input and output of a command pexpect is a good candidate. It also has a ssh part http://pexpect.readthedocs.org/en/latest/api/pxssh.html.
Did some more digging. There is a windows port of pexpect: https://gist.github.com/anthonyeden/8488763
Use that along with putty on the command line in windows.
Example of Putty on command line: http://etherealmind.com/putty-command-line/

using python commands within paramiko

I've been using Paramiko today to work with a Python SSH connection, and it is useful.
However one thing I'd really like to be able to do over the SSH is to utilise some Pythonic sugar. As far as I can tell I can only use the inbuilt Paramiko functions, and if I want to anything using Python on the remote side I would need to use a script which I have placed on there, and call it.
Is there a way I can send Python commands over the SSH connection rather than having to make do only with the limitations of the Paramiko SSH connection? Since I am running the SSH connection through Paramiko within a Python script, it would only seem right that I could, but I can't see a way to do so.
RPyC could be what you're looking for. It gives you access to another machine's Python environment directly from the local script.
>>> import rpyc
>>> conn = rpyc.classic.connect("someremotehost.com")
>>> conn.modules.sys.path
['D:\\projects\\rpyc\\servers', 'd:\\projects', .....]
To establish a connection over SSL or SSH, see:
http://rpyc.sourceforge.net/docs/secure-connection.html#ssl
Well, that is what SSH created for - to be a secure shell, and the commands are executed on the remote machine (you can think of it as if you were sitting at a remote computer itself, and that either doesn't mean you can execute Python commands in a shell, though you're physically interact with a machine).
You can't send Python commands simply because Python do not have commands, it executes Python scripts.
So everything you can do is a "thing" that will make next steps:
Wrap a piece of Python code into file.
scp it to the remote machine.
Execute it there.
Remove the script (or cache it for further execution).
Basically shell commands are remote machine's programs themselves, so you can think of those scripts like shell extensions (python programs with command-line parameters, e.g.).

Categories

Resources