Ssh

主機密鑰驗證失敗ry_nc:connectionunexpected我_關閉_____rs是nC:C這nn和C噸一世這n在n和Xp和C噸和dl是Cl這s和drsync: connection unexpectedly closed

  • March 22, 2017

我遇到了一個特殊的問題,即通過 crontab 執行 rsync 命令時它沒有執行。

下面是程式碼:

#!/bin/sh -x
PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/mysql/bin:/opt/android-sdk-linux/tools:/opt/android-sdk-linux/platform-tools:~/usr/lib/jvm/jdk-6/bin
/bin/sh /etc/profile
MyUSER="root"       # USERNAME
MyPASS="password"         # PASSWORD
MyHOST="localhost"  # Hostname
Password="" #Linux Password

MYSQL="$(which mysql)"
if [ -z "$MYSQL" ]; then
echo "Error: MYSQL not found"
exit 1
fi
MYSQLADMIN="$(which mysqladmin)"
if [ -z "$MYSQLADMIN" ]; then
   echo "Error: MYSQLADMIN not found"
   exit 1
fi
CHOWN="$(which chown)"
if [ -z "$CHOWN" ]; then
   echo "Error: CHOWN not found"
   exit 1
fi
CHMOD="$(which chmod)"
if [ -z "$CHMOD" ]; then
   echo "Error: CHMOD not found"
   exit 1
fi

GZIP="$(which gzip)"
if [ -z "$GZIP" ]; then
   echo "Error: GZIP not found"
   exit 1
fi
CP="$(which cp)"
if [ -z "$CP" ]; then
   echo "Error: CP not found"
   exit 1
fi
MV="$(which mv)"
if [ -z "$MV" ]; then
   echo "Error: MV not found"
   exit 1
fi
RM="$(which rm)"
if [ -z "$RM" ]; then
   echo "Error: RM not found"
   exit 1
fi
RSYNC="$(which rsync)"
if [ -z "$RSYNC" ]; then
   echo "Error: RSYNC not found"
   exit 1
fi

MYSQLBINLOG="$(which mysqlbinlog)"
if [ -z "$MYSQLBINLOG" ]; then
   echo "Error: MYSQLBINLOG not found"
   exit 1
fi
# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y-%T")"

DEST="/home/db-backup"
mkdir $DEST/Increment_backup.$NOW
LATEST=$DEST/Increment_backup.$NOW
$MYSQLADMIN -u$MyUSER -p$MyPASS flush-logs
newestlog=`ls -d /usr/local/mysql/data/mysql-bin.?????? | sed 's/^.*\.//' | sort -g | tail -n 1`
echo $newestlog
for file in `ls /usr/local/mysql/data/mysql-bin.??????`
do
       if [ "/usr/local/mysql/data/mysql-bin.$newestlog" != "$file" ]; then
    echo $file
    echo $Password | sudo -S $CHMOD 0777 $file
        #sudo $MYSQLBINLOG $file>$file.$NOW.sql
    $CP "$file" $LATEST
    #$RM "$file.$NOW.sql"
    #$MV $file.sql.gz /$LATEST
       fi
done
for file1 in `ls $LATEST/mysql-bin.??????`
do
$MYSQLBINLOG $file1>$file1.$NOW.sql 
$GZIP -9 "$file1.$NOW.sql"     
$RM "$file1"
done
$RSYNC -v -e ssh $LATEST abc@192.168.1.9:/home/rsync-backup/
#FILE=$LATEST/"mysql-bin.??????"
#$MYSQLBINLOG $FILE>$FILE.$NOW.sql
#$GZIP -f "$FILE.$NOW.sql"
pwd

手動執行程式碼時會發生 Rsync,但通過 crontab 執行時會失敗。其餘命令工作正常。從日誌中我得到了這些資訊:

Host key verification failed.^M
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(600) [sender=3.0.6]

謝謝大家。。。終於得到答案了。當我手動執行它時,我以本地使用者身份執行。但是,在 cron 上,我以 root 使用者身份執行它,沒有為其生成 ssh 密鑰。我做了ssh-keygen並創建了密鑰。現在它起作用了。

sshrsync執行)不信任伺服器的主機密鑰,並且由於沒有互動式終端來提示使用者,它只是失敗了。原因可能是在 cron 下執行時ssh查看的文件與手動執行時不同。known_hosts

如果 cronjob 以與您不同的使用者身份執行,您需要通過執行以下操作手動接受主機密鑰:

sudo -u cronuser HOME=/home/cronuser ssh abc@192.168.1.9

如果 cronjob 以您的使用者身份執行,則$HOME可能設置不正確。在 crontab 中明確指定:

HOME=/home/vishu

引用自:https://serverfault.com/questions/474178