Hallo,
ich möchte eine Shell aus einem Pythonskript starten, und einzelne Komandos in ihr ausführen.
hat Jemand eine Idee?
Grüsse
Von
popen2.popen3('bash') ?
Hi,
danke für deine Antwort.
sub.sh
das läuft wunderbar.
ich muss aber genau das gegenteil erreichen..
also ein Skript zeilenweise ausführen
aber wenn man das so macht
wird auch nichts bei
.............| i.write(cmd)
ausgeführt..
(das wäre glaube ich das selbe als ob man das so aufruft
ich@home# echo "echo HELLO" | bash )
bei
read()
gibt es dann einen Deadlock
weil die Shell selber ständig auf eingabe wartet?
ich bin für alle Vorschläge offen
danke für deine Antwort.
du meinst soLeonidas hat geschrieben:Wie wäre es ein Script zu machen, die Bash das Script ausführen zu lassen und den stdout abfragen?
Code: Alles auswählen
import popen2
o.i.e=popen2.popen3('/bin/bash /home/user/sub.sh')
out=o.read()
print out
HELLO
Code: Alles auswählen
#!/bin/bash
echo HELLO
ich muss aber genau das gegenteil erreichen..
also ein Skript zeilenweise ausführen
aber wenn man das so macht
Code: Alles auswählen
import popen2
o.i.e=popen2.popen3('/bin/bash')
i.write('echo HELLO')
out=o.read()
ZzzZZzz
.............| i.write(cmd)
ausgeführt..
(das wäre glaube ich das selbe als ob man das so aufruft
ich@home# echo "echo HELLO" | bash )
bei
read()
gibt es dann einen Deadlock
weil die Shell selber ständig auf eingabe wartet?
ich bin für alle Vorschläge offen
-
- Python-Forum Veteran
- Beiträge: 16025
- Registriert: Freitag 20. Juni 2003, 16:30
- Kontaktdaten:
Hmm, schon schwieriger.Anonymous hat geschrieben:du meinst sosub.shCode: Alles auswählen
import popen2 o.i.e=popen2.popen3('/bin/bash /home/user/sub.sh') out=o.read() print out HELLO
das läuft wunderbar.Code: Alles auswählen
#!/bin/bash echo HELLO
ich muss aber genau das gegenteil erreichen..
also ein Skript zeilenweise ausführen
Na dann gib ihr halt eine eingabe?Anonymous hat geschrieben:bei read() gibt es dann einen Deadlock weil die Shell selber ständig auf eingabe wartet?
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
-
- User
- Beiträge: 773
- Registriert: Mittwoch 5. November 2003, 18:06
- Wohnort: Schweiz
- Kontaktdaten:
Hi
Also ich kann nur ne vermutung machen, probier mal:
gruss
Also ich kann nur ne vermutung machen, probier mal:
Code: Alles auswählen
i.write('echo HELLO\n')
-
- User
- Beiträge: 25
- Registriert: Samstag 22. Januar 2005, 19:43
http://pexpect.sourceforge.net/
Da solltest du die Lösung finden für den Deadlock.
Da solltest du die Lösung finden für den Deadlock.
Q: Why not just use a pipe (popen())?
A: A pipe works fine for getting the output to non-interactive programs. If you just want to get the output from ls, uname, or ping then this works. Pipes do not work very well for interactive programs and pipes will almost certainly fail for most applications that ask for passwords such as telnet, ftp, or ssh.
There are two reasons for this.
First an application may bypass stdout and print directly to its controlling TTY. Something like SSH will do this when it asks you for a password. This is why you cannot redirect the password prompt because it does not go through stdout or stderr.
The second reason is because most applications are built using the C Standard IO Library (anything that uses #include <stdio.h>). One of the features of the stdio library is that it buffers all input and output. Normally output is line buffered when a program is printing to a TTY (your terminal screen). Everytime the program prints a line-feed the currently buffered data will get printed to your screen. The problem comes when you connect a pipe. The stdio library is smart and can tell that it is printing to a pipe instead of a TTY. In that case it switches from line buffer mode to block buffered. In this mode the currently buffered data is flushed when the buffer is full. This causes most interactive programs to deadlock. Block buffering is more efficient when writing to disks and pipes. Take the situation where a program prints a message "Enter your user name:\n" and then waits for you type type something. In block buffered mode, the stdio library will not put the message into the pipe even though a linefeed is printed. The result is that you never receive the message, yet the child application will sit and wait for you to type a response. Don't confuse the stdio lib's buffer with the pipe's buffer. The pipe buffer is another area that can cause problems. You could flush the input side of a pipe, whereas you have no control over the stdio library buffer.