How to run remote shell command using python script - python

I tried to run remote command to back up my server db, after dumbing it I can't figure out how to remotely get the datas.
https://docs.python.org/2/library/subprocess.html
Found some documentation, but that didn't really helped me.

Most databases have some remote-backup service available, so I'd look into that first.
That said, you could use a library that simplifies secure-shell operations. One of those is Fabric which was based on paramiko.
Fabric was designed for things like remote backups (or deployments). You want to look specifically at it's get operation capabilities.

I advice you to read this documentation.
=> https://docs.python.org/2/library/ssl.html#socket-creation
Best regards,
Greg

Related

Forbid Python from writing anything to disk

Are there any command-line options or configurations that forbids Python from writing to disk?
I know I can hack open but it doesn't sound very safe.
I've hosted some Python tutorials I wrote myself on my website for friends who want to learn Python, and I want them to have access to a Python console so they can try as they learn. This is done by creating a Python subprocess from the http server.
However, I do not want them to accidentally or intentionally damage my server, so I need to forbid the Python process from writing anything to disk.
Also I'm running the server on Ubuntu Linux so doing it Python-wise or system-wise are both OK.
I doubt there's a way to do this in the interpreter itself: there are way too many things to patch (open, subprocess, os.system, file, and probably others). I'd suggest looking into a way of containerizing the python runtime via something like Docker. The containerization gives some guarantees restricting access, though not as much as virtualization. See here for more discussion about the security implications.
Running a jupyter/ipython notebook in the docker container would probably be the easiest way to expose a web-frontend. jupyter provides a collection of docker containers for this purpose: see https://github.com/jupyter/tmpnb and https://github.com/jupyter/docker-stacks

Access Hadoop on server from python

First of all, I need to say that I am a girl who knows very little about remote server. A lot of similar questions asked here is very difficult for me to understand. So I come to ask.
My task is to generate a script which helps me fetch some data from a server.
The data is stored in Hadoop. Usually I log in the server with a user name and a temporary password. I run 'hive' clauses on the server. After I get all the data on the server, I download it. Then on my computer, I manipulate the data on my own computer with Python.
Now I hope to do this with one Python script.
I find thrift package, but don't know how to begin to understand.
I wonder should I install hive on my computer, then use sys to run hive. Or should I log in the server and run hive on the server in my script?
In any case, can thrift help me log in the server?
Thanks very much!
Although Thrift can surely help you, it is recommended to use a more higher-level client. They are usually well-tested and will keep all (or most of) the low level stuff away from you. In particular, HBase looks promising in your case. I'd recommend to have a look at that one, and to compare it with the Hadoop Python Thrift Client described in this tutorial.

Running Nginx with a database-backed config file

I know there exists a plugin for nginx to load the config through perl. I was wondering, does anyone have any experience doing this without using a plugin? Possibly a fuse-backed Python script that queries a DB?
I would really like to not use the perl plugin, as it doesn't seem that stable.
I haven't seen any working solution to solve your task, a quick google search doesn't give any useful information either (it doesn't look like HttpPerlModule could help with DB stored configuration).
It sounds like it's a good task to develop and contribute to Nginx project !

How can we call the CLI executables commands using Python

How can we call the CLI executables commands using Python
For example i have 3 linux servers which are at the remote location and i want to execute some commands on those servers like finding the version of the operating system or executing any other commands. So how can we do this in Python. I know this is done through some sort of web service (SOAP or REST) or API but i am not sure....... So could you all please guide me.
Depends on how you want to design your software.
You could do stand-alone scripts as servers listening for requests on specific ports,
or you could use a webserver which runs python scripts so you just have to access a URL.
REST is one option to implement the latter.
You should then look for frameworks for REST development with python, or if it’s simple logic with not so many possible requests can do it on your own as a web-script.
Maybe you should take a look at Pushy, which allows to connect to remote machines through SSH and make them execute various Python functions. I like using it because there are no server-side dependencies except the SSH server and a Python interpreter, and is therefore really easy to deploy.
Edit: But if you wish to code this by yourself, i think SOAP is a nice solution, the SOAPpy module is great and very easy to use.
You can use Twisted,
It is easy create ssh clients or servers.
Examples:
http://twistedmatrix.com/documents/current/conch/examples/

Running Python code from a server?

Problem: to run one.py from a server.
Error
When I try to do it in Mac, I get errors:
$python http://cs.edu.com/u/user/TEST/one.py ~
/Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python: can't open file 'http://cs.edu.com/u/user/TEST/one.py': [Errno 2] No such file or directory
one.py is like:
print 1
When I do it in Ubuntu, I get "file is not found".
Question: How can I run Python code from a server?
So far as I know, the standard Python shell doesn't know how to execute remote scripts. Try using curl or wget to retrieve the script and run it from the local copy.
$ wget http://cs.edu.com/u/user/TEST/one.py
$ python one.py
UPDATE: Based on the question referenced in the comment to this answer, you need to execute one.py based on incoming HTTP requests from end users. The simplest solution is probably CGI, but depending on what else you need to do, a more robust solution might involve a framework of some sort. They each have there strengths and weaknesses, so you should probably consider your requirements carefully before jumping in.
You can't do this. If you have SSH access to the server you can then run the python script located on the server using your SSH connection. If you want to write websites in python google python web frameworks for examples of how to set up and run websites with Python.
wget http://cs.edu.com/u/user/TEST/one.py
python one.py
You can mount the remote servers directory with some sort of file networking, like NFS or something. That way it becomes local.
But a better idea is that you explain why you are trying to do this, so we can solve the real usecase. There is most likely tons of better solutions, depending on what the real problem is.
The python interpreter doesn't know how to read from a URL. The file needs to be local.
However, if you are trying to get the server to execute the python code, you can use mod_python or various kinds of CGI.
You can't do what you are trying to do the way you are trying to do it.
Maybe something like this?
python -c "import urllib; eval(urllib.urlopen(\"http://cs.edu.com/u/user/TEST/one.py").read())"
OK, now when you explained, here is a new answer.
You run that script with
python one.py
It's a server side-script. It's run on the server. It's also located on the server. Why you try to access it via http is beyond me. Run it from the file system.
Although, you should probably look into running Grok or Django or something. This way you'll just end up writing your own Python web framework, you may just as well use one that exists instead. ;)

Categories

Resources