So, nochmal der Vollstaendigkeit halber scp via Python (Achtung, mit null Fehlerbehandlung!
):
Erstmal zwei Funktionen zum lesen:
Code: Alles auswählen
#read a line:
def readline(tty):
string = "";
while True:
c = os.read(tty, 1);
if c == os.linesep: return string;
string += c;
#read until exp_string has been read:
def expect(tty, exp_string):
string = "";
while True:
string += os.read(tty, 1);
if exp_string in string: return string;
Nun das eigenliche scp:
Code: Alles auswählen
#copy a file to a remote host:
def rcopy(host, user, passwd, lfile, rfile):
print lfile, user + "@" + host + ":" + rfile;
[pid, tty] = pty.fork();
if pid == 0:
full_rfile = user + "@" + host + ":" + rfile;
os.execl("/usr/bin/scp", "scp", lfile, full_rfile);
else:
print expect(tty, " password: "); #wait for password prompt
os.write(tty, passwd + os.linesep); #provide password
print readline(tty); #without this the write has no effect
print readline(tty); #... whereas this isn't necessary
os.waitpid(pid, 0);
Wobei, wie gesagt, ein write ohne ein anschliessendes read keinen Effekt hat.
Wenn man lesen moechte, aber nicht genau weiss, ob es noch Zeichen zum lesen gibt, ist das oben benutzte os.read(fd, n) nicht sinnvoll, denn es blockiert solange, bis mindestens ein Zeichen da ist (wartet aber nicht, bis alle n Zeichen da sind). --> select.select(...) benuzten? (da kann man timeout angeben.). Wird halt ein bisschen komplizierter, wenn man nicht genau weiss, was fuer eine Ausgabe das Programm produziert (z.B. wenn man ein ls an eine Shell absetzt).
Mit dem folgenden kann man uebrigens das echo der Konsole ein- oder ausschalten:
Code: Alles auswählen
new = termios.tcgetattr(tty);
new[3] = new[3] & ~termios.ECHO; #echo off
#new[3] = new[3] | termios.ECHO; #echo on, this is the default
termios.tcsetattr(tty, termios.TCSANOW, new);
wobei der Effekt trotz TCSANOW nicht augenblicklich eintritt, sondern erst nach einem klitzekleinen Augeblick.
EDIT: Typos gefunden...