ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Well I have moved on from some shell scripting to python. I like it a lot feels more like programming. Any way I need some help with python now. I looked on the net and wasn't able to find it so step 2 is linuxquestions.org.
Heres my problem:
On a remote machine that I am able to log into remotely through ssh and shared keys I want to remove a lot of files. using the command line rm gives the error about there being too many arguments. I believe the actual error is argument list too long. A wc showed me there was 22650 files. What i want to be able to do is supply the script with the remote machine and some other things. That is not the problem i just use argv for that.
The problem is this. I use a popen to get to the desired location, but I wan to use os.remove(filename) in a for loop. Here is a sample of the basic thing.
import os
import sys
directory = /home/user/log #this is just for example purposes
host = sys.argv[1]
os.popen("ssh " + host)
for file in os.listdir(directory):
os.remove(file)
now the syntax is close enough that is not the problem, the problem seems to be passing python commands over ssh. Is there a way to do this? I don't want to have to rely on the os commands for compatibility, but use the local python modules.
Also not sure if this is relevant, but the remote machine does have python on it. I thought maybe it needed to be able to run python itself. I tried the above script locally and it worked, its just sending the command over ssh that I am having problems. Any help would be appreciated. Thanks.
Use xargs to get around the too many arguments error:
Code:
echo /home/user/log/* | xargs rm
This does the same as
Code:
rm /home/usr/log/*
except it won't give you an error when there are many files.
The spirit of your Python script would remove everything, including files whose name starts with "." (the above would leave those alone). To achieve that effect, you could use "ls -A /home/usr/log | xargs rm" or do two steps:
Code:
rm -r /home/user/log
mkdir /home/user/log
Of course that's a bit more dangerous.
This part of the python script won't fly:
Code:
os.popen("ssh " + host)
for file in os.listdir(directory):
os.remove(file)
Popen opens a pipe. If Python is anything like another script language, you could print commands (e.g., "ls\n") to it, which should get executed on the other host in the shell (not Python) of the ssh session. It looks like listdir would be executed locally on the machine where the script runs. (Then again, I don't know much about Python.)
Is there any way to be able to run python commands over ssh. I am just wondering, becasue I do not want to rely on the commands of the OS. There will also be otehr things I will have to do like copying or opeing remote files in python. I know this is a little over my head, but once I can get the connection part down, I should be able to get the rest working.
You could install Python on the remote side too (if it's not there already), and start it up in the ssh session. You still have to push the python commands across. There are several ways, e.g., transferring a script first, or using "python -c command", to name just two.
OK I have an idea. By using ssh you can open up a command line python interpreter. Like use ssh hostname python. From there you can just start typing away. But I seem to run into some problems. First off nothing is computed until i exit via ^D. Second I am not sure if I could make a sub process to open the connection, then feed the commands directly to that process without it closing.
I think this is possible. Maybe I should use a python -i for interactive and just feed commands over the line.
Basically I want a python command line server that I can send commands to over ssh, but I do not want to have to install a server on all the machines. I just need a way to open a ssh session, get the interpreter, and send a few python commands. Is this possible, or am I wasting time looking for a solution. I am just wondering if this is too involved for a beginner or if it is something easy that I am missing.
Sorry to keep posting back to myself, but I am making some progress. I figured out that I can use:
t = os.popen('ssh hostname python', 'w')
this creates the connection to the interpreter
the only problem i have is now i have to do:
print >> t, "command and parameters"
to actually pass what i want the remote machine to do.
I also have to do another import of any modules i need becasue it is a remote machine.
This is kind of neat especially for a newb like myself. I also figured I would post it here so others could learn from my experience. I am sure there is a better way to handle this, but thats learning.
Question is can I return variables back and forth and run functions, or would i essentially have to print everything into the remote machine and then have it print something back.
Any better ideas on how to handle this owuld be appreciated.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.