SSH rsync cron expect Automatisierung

SSH rsync vollständig automatisieren mit cron und expect

Ich möchte SSH rsync mit cron und expect vollständig automatisieren.

Shou Arisaka
3 Min. Lesezeit
11. Okt. 2025

Ich möchte SSH rsync mit cron und expect vollständig automatisieren.

rsync expect

Erstellen Sie ein Skript wie dieses.

#!/usr/bin/expect

# log_file /var/log/expect.log

set PW "password"
set Prompt "\[#$%>\]"
# set RemoteHost [lindex $argv 0]

set timeout 5

spawn rsync -av -e "ssh" -r "/mnt/c/ContaCam/Logicool HD Pro Webcam C920/2018/" [email protected]/root/lib/contacam
# spawn env LANG=C /usr/bin/ssh ${RemoteHost}
expect {
    -glob "(yes/no)?" {
        send "yes\n"
        exp_continue
    }
    -glob "password:" {
        send -- "${PW}\n"
    }
}

expect {
    -glob "${Prompt}" {
        interact
        exit 0
    }
}

Bitte ändern Sie set PW "password" und spawn rsync -av -e "ssh" -r "/mnt/c/ContaCam/Logicool HD Pro Webcam C920/2018/" [email protected]/root/lib/contacam.

Versuchen wir, das erstellte Skript auszuführen.

yuis@DESKTOP-UHU8FSH:/mnt/c/pg$ /mnt/c/pg/expect/rsync_ssh
spawn rsync -av -e ssh -r /mnt/c/ContaCam/Logicool HD Pro Webcam C920/2018/ [email protected]:/root/lib/contacam
[email protected]'s password:
sending incremental file list
created directory /root/lib/contacam
./
01/
01/31/
01/31/確認済み/
01/31/確認済み/det_2018_01_31_08_23_53.gif
01/31/確認済み/det_2018_01_31_08_23_53.mp4
01/31/確認済み/det_2018_01_31_08_24_12.gif
01/31/確認済み/det_2018_01_31_08_24_12.mp4
01/31/確認済み/det_2018_01_31_08_26_38.gif
01/31/確認済み/det_2018_01_31_08_26_38.mp4
01/31/確認済み/det_2018_01_31_08_28_57.gif
01/31/確認済み/det_2018_01_31_08_28_57.mp4
01/31/確認済み/rec_2018_01_31_16_17_52.mp4

sent 1,986,489 bytes  received 247 bytes  1,324,490.67 bytes/sec
total size is 1,985,304  speedup is 1.00

Ja, es hat die Passworteingabe ordnungsgemäß automatisiert.

Als Nächstes registrieren wir dies in cron.

rsync cron

Die Registrierung in cron sollte wahrscheinlich funktionieren.

*/10 * * * * /mnt/c/pg/expect/rsync_ssh

Allerdings ist cron in bash on windows zu fehlerhaft, um verwendbar zu sein.

Daher möchte ich eine Methode verwenden, bei der in einem Shell-Skript eine Schleife ausgeführt wird. Hoffentlich wird Microsoft die cron-Fehler irgendwann beheben, bis dahin müssen wir durchhalten.

Zuerst zum Testen.

#!/bin/bash

while [ 1=0 ]
do
  sleep 3
  echo "===================================================================="
  df -h
done

Nun auch dieser.

#!/bin/bash

while [ 1=0 ]
do
  sleep 3
  echo "===================================================================="
  /mnt/c/pg/expect/rsync_ssh
done

Haben Sie solche Ausgaben erhalten?

$ . /mnt/c/pg/dev.sh
====================================================================
spawn rsync -av -e ssh -r /mnt/c/ContaCam/Logicool HD Pro Webcam C920/2018/ [email protected]:/root/lib/contacam
[email protected]'s password:
sending incremental file list

sent 334 bytes  received 15 bytes  698.00 bytes/sec
total size is 1,985,304  speedup is 5,688.55
====================================================================
spawn rsync -av -e ssh -r /mnt/c/ContaCam/Logicool HD Pro Webcam C920/2018/ [email protected]:/root/lib/contacam
[email protected]'s password:
sending incremental file list

sent 334 bytes  received 15 bytes  698.00 bytes/sec
total size is 1,985,304  speedup is 5,688.55
====================================================================
spawn rsync -av -e ssh -r /mnt/c/ContaCam/Logicool HD Pro Webcam C920/2018/ [email protected]:/root/lib/contacam
[email protected]'s password:
sending incremental file list

sent 334 bytes  received 15 bytes  698.00 bytes/sec
total size is 1,985,304  speedup is 5,688.55

Lassen Sie es nun alle 5 Minuten ausführen.

#!/bin/bash

while [ 1=0 ]
do
  sleep 300
  echo "===================================================================="
  /mnt/c/pg/expect/rsync_ssh
done

Das war's.

Diesen Artikel teilen

Shou Arisaka 11. Okt. 2025

🔗 Links kopieren