popen2.popen3('bash') ?

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
von_ernen
User
Beiträge: 1
Registriert: Montag 24. Januar 2005, 16:36
Kontaktdaten:

Hallo,
ich möchte eine Shell aus einem Pythonskript starten, und einzelne Komandos in ihr ausführen.

hat Jemand eine Idee?

Grüsse
Von
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

Wie wäre es ein Script zu machen, die Bash das Script ausführen zu lassen und den stdout abfragen?
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
Gast

Hi,
danke für deine Antwort.
Leonidas hat geschrieben:Wie wäre es ein Script zu machen, die Bash das Script ausführen zu lassen und den stdout abfragen?
du meinst so

Code: Alles auswählen

import popen2
o.i.e=popen2.popen3('/bin/bash /home/user/sub.sh')
out=o.read()
print out
HELLO
sub.sh

Code: Alles auswählen

  
#!/bin/bash
echo HELLO
das läuft wunderbar.

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
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 :)
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

Anonymous hat geschrieben:du meinst so

Code: Alles auswählen

import popen2
o.i.e=popen2.popen3('/bin/bash /home/user/sub.sh')
out=o.read()
print out
HELLO
sub.sh

Code: Alles auswählen

  
#!/bin/bash
echo HELLO
das läuft wunderbar.

ich muss aber genau das gegenteil erreichen..
also ein Skript zeilenweise ausführen
Hmm, schon schwieriger.
Anonymous hat geschrieben:bei read() gibt es dann einen Deadlock weil die Shell selber ständig auf eingabe wartet?
Na dann gib ihr halt eine eingabe?
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
rayo
User
Beiträge: 773
Registriert: Mittwoch 5. November 2003, 18:06
Wohnort: Schweiz
Kontaktdaten:

Hi

Also ich kann nur ne vermutung machen, probier mal:

Code: Alles auswählen

i.write('echo HELLO\n')
gruss
Slalomsk8er
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.
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.
Antworten