ssh脚本返回 255错误

在我的代码中,我有以下运行远程脚本。

在我的代码中,我有以下运行远程脚本。

ssh root@host.domain.com "sh /home/user/backup_mysql.sh"

出于某种原因,它一直困扰着我 255。有什么想法吗?

我可以 SSH 到框中就好了(无密码密钥设置)

远程脚本:

MUSER='root' 
MPASS='123123'
MHOST="127.0.0.1"
VERBOSE=0
### Set bins path ###
GZIP=/bin/gzip
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
RM=/bin/rm
MKDIR=/bin/mkdir
MYSQL=/usr/bin/mysqladmin
GREP=/bin/grep
### Setup dump directory ###
BAKRSNROOT=/.snapshots/tmp
#####################################
### ----[ No Editing below ]------###
#####################################
### Default time format ###
TIME_FORMAT='%H_%M_%S%P'
### Make a backup ###
backup_mysql_rsnapshot(){
        local DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
        local db="";
        [ ! -d $BAKRSNROOT ] && ${MKDIR} -p $BAKRSNROOT
        ${RM} -f $BAKRSNROOT/* >/dev/null 2>&1
#       [ $VERBOSE -eq 1 ] && echo "*** Dumping MySQL Database ***"
#       [ $VERBOSE -eq 1 ] && echo -n "Database> "
        for db in $DBS
        do
                local tTime=$(date +"${TIME_FORMAT}")
                local FILE="${BAKRSNROOT}/${db}.${tTime}.gz"
#               [ $VERBOSE -eq 1 ] && echo -n "$db.."
                ${MYSQLDUMP} --single-transaction -u ${MUSER} -h ${MHOST} -p${MPASS} $db | ${GZIP} -9 > $FILE
        done
#               [ $VERBOSE -eq 1 ] && echo ""
#               [ $VERBOSE -eq 1 ] && echo "*** Backup done [ files wrote to $BAKRSNROOT] ***"
}
### Die on demand with message ###
die(){
        echo "$@"
        exit 999
}
### Make sure bins exists.. else die
verify_bins(){
        [ ! -x $GZIP ] && die "File $GZIP does not exists. Make sure correct path is set in $0."
        [ ! -x $MYSQL ] && die "File $MYSQL does not exists. Make sure correct path is set in $0."
        [ ! -x $MYSQLDUMP ] && die "File $MYSQLDUMP does not exists. Make sure correct path is set in $0."
        [ ! -x $RM ] && die "File $RM does not exists. Make sure correct path is set in $0."
        [ ! -x $MKDIR ] && die "File $MKDIR does not exists. Make sure correct path is set in $0."
        [ ! -x $MYSQL ] && die "File $MYSQL does not exists. Make sure correct path is set in $0."
        [ ! -x $GREP ] && die "File $GREP does not exists. Make sure correct path is set in $0."
}
### Make sure we can connect to server ... else die
verify_mysql_connection(){
        $MYSQL  -u $MUSER -h $MHOST -p$MPASS ping | $GREP 'alive'>/dev/null
        [ $? -eq 0 ] || die "Error: Cannot connect to MySQL Server. Make sure username and password are set correctly in $0"
}
### main ####
verify_bins
verify_mysql_connection
backup_mysql_rsnapshot
46

这通常发生在远程关闭 / 不可用时;或者远程机器没有安装 ssh;或者防火墙不允许建立到远程主机的连接。

发生错误时,ssh返回 255,或者远程脚本返回 255:

 EXIT STATUS
     ssh exits with the exit status of the remote command or
     with 255 if an error occurred.

通常你会收到类似的错误信息:

ssh: connect to host host.domain.com port 22: No route to host

ssh: connect to host HOSTNAME port 22: Connection refused

检查表:

如果直接从命令行运行 ssh 命令会发生什么?

你能ping那个机器吗?

远程安装了 ssh 吗?

如果已安装,那么 ssh 服务是否正在运行?

13

将 pdsh 用于“known_hosts”文件中未包含的主机时,也会发生此错误。

我能够通过手动 SSH 进入每个主机并接受“您要将其添加到已知主机”的问题来纠正这一点。

7

如果身份验证或连接有问题,例如无法从终端读取密码,ssh 将以 255 退出,而无法运行实际脚本。验证以确保可以运行“true”,以查看 ssh 连接是否成功建立。

4

是不是在线路的问题:

### Die on demand with message ###
die(){
  echo "$@"
  exit 999
}

如果我错了,请纠正我,但我相信退出 999 超出了退出代码的范围,并导致退出状态为 255。

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(94)
Xbox OneRight触发器不工作
上一篇
SwiftUI:ForEach筛选器布尔值
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(41条)