 |
 |
Interacting with shell command via Python?
|
 |
|
 |
|
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|