LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-11-2005, 11:29 AM   #1
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Rep: Reputation: 15
Python commands over ssh


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.
 
Old 10-11-2005, 02:17 PM   #2
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
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.)
 
Old 10-11-2005, 02:57 PM   #3
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Original Poster
Rep: Reputation: 15
Thanks. I will have to try that.
 
Old 10-17-2005, 05:45 AM   #4
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Original Poster
Rep: Reputation: 15
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.

Thanks,

Kedelfor
 
Old 10-17-2005, 10:31 AM   #5
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
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.
 
Old 10-18-2005, 01:03 AM   #6
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Original Poster
Rep: Reputation: 15
Can you push an entire script using the python -c without copying the script across. Liike cat it into the intepeter remotely or soemthing?
 
Old 10-18-2005, 03:26 AM   #7
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Original Poster
Rep: Reputation: 15
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.

Thanks,

Kedelfor
 
Old 10-18-2005, 04:07 AM   #8
Kedelfor
Member
 
Registered: Jan 2004
Posts: 49

Original Poster
Rep: Reputation: 15
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh changes the way python script behaves shanenin Programming 4 11-16-2005 03:22 PM
Passing commands via ssh Comatose51 Linux - Security 1 04-13-2005 04:31 PM
Running commands over ssh iago Programming 8 11-28-2004 12:13 PM
does ssh have commands similar to ftp? xviddivxoggmp3 Linux - Software 2 07-14-2004 06:04 PM
python, executing regular linux commands Robert0380 Programming 3 06-26-2003 03:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration