我如何自动化两层SSH加docker执行程序?

我一天做多次。 任何关于自动化的线索,以便我可以运行一个命令来获取日志? 有两个ssh ,然后是docker exec

➜ ~ ssh host Last login: Tue Jun 27 15:44:11 2017 from 10.82.34.63 $ ssh another-host Last login: Tue Jun 27 15:44:18 2017 from host $ docker exec -it app-container bash [root@app-container opt]# tail -f tomcat/logs/catalina.out

这至less是ssh的部分答案。 看看ssh使用情况输出:

 ssh (.... lots of options ....) [user@]hostname [command] 

所以,在参数列表末尾有一个可选的command ,在所需的hostname 。 这的确如你所期望的那样工作,你可以在这里链接另外一个远程执行的ssh命令:

 ssh host ssh another-host 

会做。

请注意,在这种情况下,你的ssh将不会分配一个tty ,所以它不会让你有一个交互式会话。 但是当然,你可以给这个第二个 ssh执行一些东西

 ssh host ssh another-host docker exec [...] 

对于最后一部分,我只是查找了docker文档。 选项-t需要一个tty ,所以你应该把它排除在外。 那么你应该能够在你的容器中执行任何你喜欢的东西,只要它没有任何交互性

 ssh host ssh another-host docker exec -i app-container tail -f tomcat/logs/catalina.out 

当然,为了实现全自动化,请使用SSH密钥,并在添加了密钥的情况下运行SSH代理 。

第一部分我们可以利用OpenSSH中的ProxyCommand (通过代理主机跳转到其他SSH)。 ~/.ssh/config的例子如下所示:

 Host another-host ProxyCommand ssh -W %h:%p host HostName another-host 

如果你所代理的所有主机碰巧在同一个域中,你可以用一个通配符来捕获一堆主机:

 Host jumphost Hostname host.mydomain Host *.mydomain ProxyCommand ssh -W %h:%p jumphost 

对于第二部分,在使用命令之前,不需要使用shell exec容器。 做docker exec -it app-container tail -f tomcat/logs/catalina.out是完全有效的。

结合SSHconfiguration,您可以分配一个伪TTY( -t ),然后只需执行一个命令:

 ssh -t another-host docker exec -it app-container tail -f tomcat/logs/catalina.out