Einen Aufruf mit subprocess.run() inkl. Spaces und Quotes starten

Heute ist Stammtischzeit:
Jeden Donnerstag 20:30 Uhr hier im Chat.
Wer Lust hat, kann sich gerne beteiligen. ;)
  • Hallo,

    bisher habe diverse Dateien per RSYNC mit in einem Python-Skript so kopiert:

    os.system(f'sshpass -p {password} rsync -e "ssh -o StrictHostKeyChecking=no" -a {file} {server_and_path}')

    klappt wunderbar.

    Ich würde mich gerne von os.system trennen und es mit subprocess.run() machen, damit ich den Exit-Code besser überprüfen kann, da hier aber nicht nur ein zweites Programm das erste startet, sondern noch Quotes und Spaces in der Übergabe zu zweiten Programm vorkommen, sind alle meine bisherigen Versuchen, das zu escapen, gescheitert.

    Auf die rsync-Option StrictHostKeyChecking=no kann ich aus anderen Gründen nicht verzichten und die Quotes auch nicht, sonst meckert rsync kräftig.

    Wie kann ich das dennoch mit subprocess.run() realisieren?

  • Einen Aufruf mit subprocess.run() inkl. Spaces und Quotes starten? Schau mal ob du hier fündig wirst!

  • Hallo,

    versuche mal:

    Grüße

    Dennis

    🎧 With the music execution and the talk of revolution, it bleeds in me and it goes 🎧

  • Hallo,

    versuche mal:

    Code
    [...]

    Grüße

    Dennis

    Ging leider nicht:

    rsync: [sender] Failed to exec ssh -o StrictHostKeyChecking=no: No such file or directory (2)

    rsync error: error in IPC code (code 14) at pipe.c(85) [sender=3.2.3]

    rsync: connection unexpectedly closed (0 bytes received so far) [sender]

    rsync error: error in IPC code (code 14) at io.c(228) [sender=3.2.3]

    Traceback (most recent call last):

    File "/ram/basis/rsync-helper.py", line 35, in <module>

    run(

    File "/usr/lib/python3.9/subprocess.py", line 528, in run

    raise CalledProcessError(retcode, process.args,

    subprocess.CalledProcessError: Command '['sshpass', '-p', 'TOLLES_PASSWORT', 'rsync', '-e', '"ssh -o StrictHostKeyChecking=no"', '-a', '/home/pi/basis.zip', 'pi@SERVER:/PFAD/']' returned non-zero exit status 14.

  • Sorry, mir ist das mit dem 'ssh -o StrictHostKeyChecking=no' unbekannt. Wie würde dass denn im Terminal aussehen?

    So:

    Code
    sshpass -p password rsync -e "ssh -o StrictHostKeyChecking=no" -a file server_and_path

    ?

    Wobei die rsync-Fehlermeldung sagt, dass eine Datei oder Verzeichnis nicht vorhanden ist.

    Grüße

    Dennis

    🎧 With the music execution and the talk of revolution, it bleeds in me and it goes 🎧

  • Ja, so wie du es geschrieben hast, klappt das bei mir im Terminal einwandfrei.

    Da es um Zugangsdaten für einen Account geht, kann ich nicht das volle Script und/oder die Benutzerdaten posten, leider.

  • In der manpage von rsync lautet ein Beispiel so:

    Code
    -e 'ssh -o "ProxyCommand nohup ssh firewall nc -w1 %h %p"'

    Das heißt, die ssh-Optionen (-o) sind für sich gequoted.

    Also vielleicht bei Dir dann so:?

    Code
    sshpass -p password rsync -e 'ssh -o "StrictHostKeyChecking=no"' -a file server_and_path
  • Für solche Anliegen verwende ich aus Faulheit shlex. ;)

    In dem Fall hier sind Anführungszeichen im Befehl, die escapt werden müssen, sonst bekommt man einen SyntaxError: invalid syntax.

    Hier das Beispiel:

    Python
    >>> from shlex import split
    >>> cmd = "sshpass -p password rsync -e 'ssh -o \"StrictHostKeyChecking=no\"' -a file server_and_path"
    >>> print(split(cmd))
    ['sshpass', '-p', 'password', 'rsync', '-e', 'ssh -o "StrictHostKeyChecking=no"', '-a', 'file', 'server_and_path']
    >>>

Jetzt mitmachen!

Du hast noch kein Benutzerkonto auf unserer Seite? Registriere dich kostenlos und nimm an unserer Community teil!