Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Interacting with shell command via Python?

Interacting with shell command via Python?
Thread Tools
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
Mar 1, 2004, 01:14 PM
 
I'm working on a project in Python that requires the mounting of a sambashare. I have no problem running the command to mount ("os.system('smbmount server mountpoint')"), but it then prompts for a password. At the moment I just have it set up so that the user runs my program in the terminal and then enters the password when smbmount asks for it, interacting directly with smbmount. I'd rather be able to ask the user for the password myself and then pass it to the command so that I can stick a nicer interface onto the program and not be bound to the terminal. For whatever reason, even though smbmount has a commandline argument for passing in the password, it won't let me pass the password in as an argument. So, I need to somehow open a bi-directional pipe to the command. I've been messing around with popen(), but I've only been able to figure out how to get information back from the command, and not how to pass information along to it. Does anyone know a way to to run a command in Python that would let me actually interact with it? Or at least a website that would help? Google is being surprisingly unhelpful with this.
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Mar 1, 2004, 01:55 PM
 
Originally posted by nonhuman:
I'd rather be able to ask the user for the password myself and then pass it to the command so that I can stick a nicer interface onto the program and not be bound to the terminal.
Look at os.popen . I think it may support what you want. Something like:

Code:
import os lf = chr(10) f = os.popen("telnet myserver", 'w') f.write("username" + lf) f.flush() f.write("password" + lf) f.flush()
A simple example, but it might fit your needs.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
Mar 1, 2004, 02:13 PM
 
Ah, maybe it was the linefeeds that I was missing. That could explain it.
     
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
Mar 1, 2004, 02:30 PM
 
Apparently I was just confused by the poorly written smbmount manpage and was passing the command-line arguments wrong. So I got it working without the two-way pipe. But I'd still like to figure out how to do this, it would be useful.

I don't seem to be able to get popen to give me a write-enabled file though. It always returns a read-only file.
     
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Mar 1, 2004, 09:12 PM
 
Originally posted by nonhuman:
Apparently I was just confused by the poorly written smbmount manpage and was passing the command-line arguments wrong. So I got it working without the two-way pipe. But I'd still like to figure out how to do this, it would be useful.

I don't seem to be able to get popen to give me a write-enabled file though. It always returns a read-only file.
Code:
>>> import os >>> print os.popen.__doc__ popen(command [, mode='r' [, bufsize]]) -> pipe Open a pipe to/from a command returning a file object.
If you want the pipe to be write, you have to pass "w' for the mode. "r" is assumed otherwise.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
Mar 1, 2004, 09:26 PM
 
Originally posted by Arkham_c:
Code:
>>> import os >>> print os.popen.__doc__ popen(command [, mode='r' [, bufsize]]) -> pipe Open a pipe to/from a command returning a file object.
If you want the pipe to be write, you have to pass "w' for the mode. "r" is assumed otherwise.
Right, I did that. But the files it returns still comes back read-only for some reason.
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:59 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2