python: how to run scripts over ssh remotely [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Execute arbitrary python code remotely - can it be done?
I wrote a memory usage check function and it runs fine on one server (say 10.100.110.10).
But I need to run the same script remotely on 10.100.110.11 from 10.100.110.10. I can ssh to 10.100.110.11 from 10.100.110.10. Is there any way to implement that using python built in modules?
I can't use any new modules like Paramico and Unix command
ssh -n user#10.100.110.11 "df -m"
works fine.
If it is not possible, how can I ssh to 10.100.110.11 using a built in Python module?

You cannot remotely run Python code if you're not accessing the Python interpreter on the other side. You could sent the code the the Python's interpreter standard input, but you still have to send the code.
The other solution would be to make your code remote-compatible, and replace all systems calls by their equivalents over SSH (using Paramiko for example).

Related

I need to write a python script which will make a passwordless connection from one server (server1) to another remote server(server2) [duplicate]

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed last year.
I have successfully created public private keys to make passwordless ssh connection.
I need to make connection with remote server, go to a particular path, and append some input to sample.log file.
Currently I am using os.system("ssh xxx#aaa.vvv.com") and then the steps to append the input to sample.log file. But my script stops only aftwr making connection with using os.system("ssh xxx#aaa.vvv.com").
Paramiko Module is best suited for it.
I use it personally to make connections and execute comand from one device to another.
You can find more detailed information of paramiko on https://www.paramiko.org

How can I make a Python script keep running after I close my shell? [duplicate]

This question already has answers here:
How to make a program continue to run after log out from ssh? [duplicate]
(6 answers)
Closed 2 years ago.
I've coded a Discord bot in Python. I have it hosted on a server that I use PuTTy to SSH into. Closing that terminal will obviously result in the bot ceasing to work. Does Python have a process management system that will allow me to keep a Python script running?
I'm running centOS.
It depends about how much experience you have programming in python.
For example you could use daemonize (which I personally prefer).
And this is the simplest example available (from the daemonize documentation)
from time import sleep
from daemonize import Daemonize
pid = "/tmp/test.pid"
def main():
while True:
sleep(5)
daemon = Daemonize(app="test_app", pid=pid, action=main)
daemon.start()
Another way to keep your script running, it could be install screen.
Execute screen before executing your script and then detach the session using "Ctrl+a"+"d"

Is their some means of coding and running Python on a Raspberry PI from a PC? [duplicate]

This question already has answers here:
Run local python script on remote server
(5 answers)
Closed 6 years ago.
So far, SSH is all I can think of. Send a command line like python <dosometing., and interpret the response. I am, however, hoping for a lower level library, where I can enter the Python command in my interpreter's REPL, and have that command execute on the Pi, and return the result to my REPL.
Please no questions why don't I just SSH straight into the Pi and use its Python directly. I don't want to use an SSH session to write Python on my headless Pi. I want a Windows Python REPL that talks to the Raspbian Python transpiler. This is for an experimental task that is part of a much larger project about .NET and Pi communication.
First thing that comes to my mind is to create a TCP server-client app. Server would reside on RPi, and waiting on connections. When the command arrives from client, server will execute it (using subprocess.Popen or even maybe os.system). Client can get whole output back (in the first case), or just exit status (in the second case).

Connecting to SFTP server via Windows' Command Prompt [duplicate]

This question already has answers here:
Secure FTP using Windows batch script
(3 answers)
Closed 7 years ago.
I'm wondering if there's any way to connect SFTP server with Windows' Command Prompt, by only executing batch file.
Do I need to install additional software? which software?
The purpose is to do pretty basic file operations (upload, delete, rename) on remote SFTP server by executing a batch file.
And by the way, I have heard about python's Fabric library, and I wonder whether it's better solution than the batch script for the mentioned basic file operations?
Thanks a lot!
The built in FTP command doesn't have a facility for security. You can use winscp, an open source free SFTP client and FTP client for Windows.
Try using cURL. It allows to set up file transfers from the command line and supports SSL.
See http://www.unixlore.net/articles/using-curl-for-ftp-over-ssl-file.html

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