bash bash脚本中的ssh“端口22:没有到主机的路由”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31341615/
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-09-18 13:19:05  来源:igfitidea点击:

ssh "port 22: no route to host" error in bash script

linuxbashsshsed

提问by Brave

I wrote a scipt to execute a couple of ssh remote comands relating to apache storm. When I execute the script it says:

我写了一个 scipt 来执行几个与 apache Storm 相关的 ssh 远程命令。当我执行脚本时,它说:

ssh: connect to host XXX.XXX.XXX.XXX port 22: No route to host
ssh: connect to host XXX.XXX.XXX.XXX port 22: No route to host
ssh: connect to host XXX.XXX.XXX.XXX port 22: No route to host
ssh: connect to host XXX.XXX.XXX.XXX port 22: Connection refused

If I execute the commands manually it works out well and I can ping the machine. So that there has to be something wrong with this code:

如果我手动执行命令,它运行良好,我可以 ping 机器。所以这段代码肯定有问题:

while [ $i -le $numVM ]

do
    if [ $i -eq 1 ];then
        ssh -i file root@IP1 './zookeeper-3.4.6/bin/zkServer.sh start' 
    else
        ssh -i file root@IP2 'sed -i '"'"'s/#\ storm.zookeeper.servers.*/storm.zookeeper.servers:/'"'"' /root/apache-storm-0.9.3/conf/storm.yaml'
        ssh -i file root@IP2 'sed -i '"'"'0,/^#[[:space:]]*-[[:space:]]*\"server1\".*/s//"      - \"'${IParray[1]}'\""/'"'"' /root/apache-storm-0.9.3/conf/storm.yaml'
        ssh -i file root@IP2 'sed -i '"'"'s/#\ nimbus.host:.*/"nimbus.host: \"'${IParray[2]}'\""/'"'"' /root/apache-storm-0.9.3/conf/storm.yaml'
        ssh -i file root@IP2 './zookeeper-3.4.6/bin/zkCli.sh -server ${IParray[1]} &'
        sleep 10
        ssh -i file root@IP2 './apache-storm-0.9.3/bin/storm nimbus &' &
        sleep 10
        ssh -i file root@IP2 './apache-storm-0.9.3/bin/storm ui &' & 
        sleep 10
        ssh -i file root@IP2 './apache-storm-0.9.3/bin/storm supervisor &' &
    fi  
    ((i++))
done

I'm starting several processes on 2 virtual machines that are deployed from the same image, so that they are identical in general. The confusing part is, that the first ssh command (zkServer.sh start) is working well but if I the script tries to execute the three "sed"-ssh-commands I get the error message above. But then the last four ssh-commands are working out well again. That does not make any sense to me...

我正在从同一映像部署的 2 个虚拟机上启动多个进程,因此它们通常是相同的。令人困惑的部分是,第一个 ssh 命令(zkServer.sh start)运行良好,但是如果我的脚本尝试执行三个“sed”-ssh-commands,我会收到上面的错误消息。但是最后四个 ssh 命令再次运行良好。这对我来说没有任何意义......

回答by David W.

Several things I can think of:

我能想到的几件事:

  • Most sshddaemons won't allow rootaccess. Heck, many versions of Unix/Linux no longer allow root login. If you need root access, you need to use sudo.
  • The sshddaemon on the remote machine isn't running. Although rare, some sites may never had it setup, or purposefully shut it off as a security issue.
  • Your sshcommands themselves are incorrect.
  • 大多数sshd守护进程不允许root访问。哎呀,许多版本的 Unix/Linux 不再允许 root 登录。如果您需要 root 访问权限,则需要使用sudo.
  • sshd远程机器上的守护程序没有运行。尽管很少见,但某些站点可能从未设置过它,或者出于安全问题故意将其关闭。
  • 你的ssh命令本身是不正确的。

Instead of executing the sshcommands in your shell script, modify the shell script just to print out what you're attempting to execute. Then, see if you can execute the command outsideof the shell script. This way you can determine whether the problem is the shell script, or the problem is with the sshcommand itself.

不要ssh在您的 shell 脚本中执行命令,而是修改 shell 脚本以打印出您尝试执行的内容。然后,看看是否可以在shell脚本之外执行命令。通过这种方式,您可以确定问题是 shell 脚本的问题,还是ssh命令本身的问题。

If your sshcommands don't work outsidefrom the command line, you can then simplify them and see if you can determine what the issue could be. You have ssh -i file root@IP2. Is this suppose to be ssh -i $file root@$IP2? (i.e., you're missing the leading sigil).

如果您的ssh命令在命令行之外不起作用,那么您可以简化它们,看看您是否可以确定问题所在。你有ssh -i file root@IP2。这是假设ssh -i $file root@$IP2吗?(即,您错过了领先的印记)。

$ ssh -i file root@$IP2 ls   # Can't get simpler than this...
$ ssh -i file root@IPS       # See if you can remotely log on...
$ ssh root@IP2               # Try it without an 'identity file'
$ ssh bob@IP2                # Try it as a user other than 'root'
$ telnet IP2 22              # Is port 22 even open on the remote machine?

If these don't work, then you have some very basic issue with the setup of your remote machine's sshdcommand.

如果这些都不起作用,那么您在设置远程机器的sshd命令时就会遇到一些非常基本的问题。