Linux ssh 脚本返回 255 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14885748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:01:17  来源:igfitidea点击:

ssh script returns 255 error

linuxbashshellssh

提问by Amanada Smith

In my code I have the following to run a remote script.

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

ssh [email protected] "sh /home/user/backup_mysql.sh"

For some reason it keeps 255'ing on me. Any ideas?

出于某种原因,它一直在我身上 255'ing。有任何想法吗?

I can SSH into the box just fine (passless keys setup)

我可以通过 SSH 进入盒子就好了(无密码密钥设置)

REMOTE SCRIPT:

远程脚本:

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 
 EXIT STATUS

     ssh exits with the exit status of the remote command or
     with 255 if an error occurred.
." [ ! -x $MYSQL ] && die "File $MYSQL does not exists. Make sure correct path is set in
ssh: connect to host host.domain.com port 22: No route to host
." [ ! -x $MYSQLDUMP ] && die "File $MYSQLDUMP does not exists. Make sure correct path is set in
ssh: connect to host HOSTNAME port 22: Connection refused
." [ ! -x $RM ] && die "File $RM does not exists. Make sure correct path is set in
 pssh -x '-tt' -h HOSTFILELIST -P "sudo yum -y install glibc"
." [ ! -x $MKDIR ] && die "File $MKDIR does not exists. Make sure correct path is set in
ssh AHOST "sudo yum -y install glibc"
." [ ! -x $MYSQLADMIN ] && die "File $MYSQLADMIN does not exists. Make sure correct path is set in
[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
." [ ! -x $GREP ] && die "File $GREP does not exists. Make sure correct path is set in
-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
." } ### 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 ##代码##" } ### main #### verify_bins verify_mysql_connection backup_mysql_rsnapshot

回答by that other guy

If there's a problem with authentication or connection, such as not being able to read a password from the terminal, ssh will exit with 255 without being able to run your actual script. Verify to make sure you can run 'true' instead, to see if the ssh connection is established successfully.

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

回答by P.P

This is usually happens when the remote is down/unavailable; or the remote machine doesn't have ssh installed; or a firewall doesn't allow a connection to be established to the remote host.

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

sshreturns 255 when an error occurred or 255 is returned by the remote script:

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

##代码##

Usually you would an error message something similar to:

通常,您会收到类似于以下内容的错误消息:

##代码##

Or

或者

##代码##

Check-list:

检查清单:

  • What happens if you run the ssh command directly from the command line?

  • Are you able to pingthat machine?

  • Does the remote has ssh installed?

  • If installed, then is the ssh service running?

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

  • 你能上ping那台机器吗?

  • 遥控器是否安装了 ssh?

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

回答by Wes Floyd

This error will also occur when using pdsh to hosts which are not contained in your "known_hosts" file.

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

I was able to correct this by SSH'ing into each host manually and accepting the question "Do you want to add this to known hosts".

我能够通过手动 SSH 连接到每个主机并接受问题“您想将其添加到已知主机”来纠正此问题。

回答by Steven the Easily Amused

I was stumped by this. Once I got passed the 255 problem... I ended up with a mysterious error code 1. This is the foo to get that resolved:

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

##代码##

-P means write the output out as you go and is optional. But the -x '-tt'trick is what forces a psuedo tty to be allocated.

-P 表示随时写出输出,是可选的。但是-x '-tt'技巧是强制分配伪 tty 的原因。

You can get a clue what the error code 1 means this if you try:

如果您尝试,您可以获得错误代码 1 的含义的线索:

##代码##

You may see:

你可能会看到:

##代码##

Notice the return code for this is 1, which is what pssh is reporting to you.

请注意,此返回码是 1,这是 pssh 向您报告的内容。

I found this -x -tt trick here. Also note that turning on verbose mode (pssh --verbose) for these cases does nothing to help you.

我在这里找到了这个 -x -tt 技巧。另请注意,为这些情况打开详细模式 (pssh --verbose) 对您没有任何帮助。

回答by Tejas Tank

SSH Very critical issue on Production. SSH-debug1: Exit status 255

SSH 生产中非常关键的问题。SSH-debug1:退出状态 255

I was working with Live Server and lots stuff stuck. I try many things to fix but exact issue of 255 don't figure out.

我正在使用 Live Server 并且卡住了很多东西。我尝试了很多方法来解决,但 255 的确切问题不知道。

Even I had resolved issue 100%

即使我已经 100% 解决了问题

Replace my sshd_config file from similar other my debian server

[email protected]:~# cp sshd_config sshd_config.snippetbucket.com.bkp #keep my backup file

[email protected]:~# echo "" > sshd_config

[email protected]:~# nano sshd_config #replaced all content with other exact same server

[email protected]:~# sudo service ssh restart #normally restart server

从类似的其他 debian 服务器替换我的 sshd_config 文件

[email protected]:~# cp sshd_config sshd_config.snippetbucket.com.bkp #保留我的备份文件

[email protected]:~# echo "" > sshd_config

[email protected]:~# nano sshd_config #用其他完全相同的服务器替换所有内容

[email protected]:~# sudo service ssh restart #正常重启服务器

That's 100% resolve my issue immediate.

那是 100% 立即解决我的问题。

#SnippetBucket-Tip: Always take backup of ssh related files, which help on quick restoration.

#SnippetBucket-Tip:始终备份 ssh 相关文件,这有助于快速恢复。

Note: After apply given changes you need to exit rescue mode and reboot your vps / dedicated server normally, than your ssh connection works.

注意:应用给定更改后,您需要退出救援模式并正常重启您的 vps / 专用服务器,然后您的 ssh 连接才能正常工作。

During rescue mode ssh don't allow user to login as normally. only rescue ssh related login and password works.

在救援模式 ssh 不允许用户正常登录。只有救援 ssh 相关的登录名和密码才有效。

回答by vahid-dan

As @wes-floyd and @zpon wrote, add these parameters to SSH to bypass "Are you sure you want to continue connecting (yes/no)?"

正如@wes-floyd 和@zpon 所写,将这些参数添加到 SSH 以绕过“您确定要继续连接(是/否)吗?”

##代码##

回答by Dmitry

If above didn't help: check if locale is valid on client and server:
https://www.linuxbabe.com/linux-server/fix-ssh-locale-environment-variable-error
How do not pass locale through ssh

如果以上没有帮助:检查客户端和服务器上的语言环境是否有效:
https: //www.linuxbabe.com/linux-server/fix-ssh-locale-environment-variable-error
如何不通过 ssh 传递语言环境

回答by Dimitris Moraitidis

It can very much be an ssh-agent issue. Check whether there is an ssh-agent PID currently running with eval "$(ssh-agent -s)"

这很可能是 ssh 代理问题。检查当前是否有正在运行的 ssh-agent PIDeval "$(ssh-agent -s)"

Check whether your identity is added with ssh-add -land if not, add it with ssh-add <pathToYourRSAKey>.

检查您的身份是否已添加,ssh-add -l如果没有,请添加ssh-add <pathToYourRSAKey>

Then try again your ssh command (or any other command that spawns ssh daemons, like autosshfor example) that returned 255.

然后再次尝试返回 255 的ssh 命令(或任何其他生成 ssh 守护进程的命令,例如autossh)。