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 MYSQLADMIN=/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 $MYSQLADMIN ] && die "File $MYSQLADMIN 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(){ $MYSQLADMIN -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 

Solutions Collecting From Web of "ssh脚本返回255错误"

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

当发生错误或远程脚本返回255时, ssh返回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服务?

使用pdsh到“known_hosts”文件中不包含的主机时,也会发生此错误。

我可以通过SSH手动更正每个主机,并接受“您是否想将其添加到已知主机”这个问题。

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

我被这个难住了。 一旦我通过了255的问题…我结束了一个神秘的错误代码1.这是得到解决的foo:

  pssh -x '-tt' -h HOSTFILELIST -P "sudo yum -y install glibc" 

-P表示将输出写出,并且是可选的。 但-x'-tt'技巧是迫使一个tty被分配的东西。

你可以得到一个线索什么错误代码1意味着这一点,如果你尝试:

 ssh AHOST "sudo yum -y install glibc" 

你可能会看到:

 [slc@bastion-ci ~]$ ssh MYHOST "sudo yum -y install glibc" sudo: sorry, you must have a tty to run sudo [slc@bastion-ci ~]$ echo $? 1 

注意这个返回码是1,这是pssh向你报告的。

我在这里找到了这个-x -tt技巧。 另外请注意,为这些情况启用详细模式(pssh –verbose)对您无能为力。