使用 SSH 可以实现直接在本地对远程主机执行操作
- $ ssh-copy-id ifmicro@remote-host
-
它实现了这样一个功能 :
- 把 `~/.ssh/id_rsa.pub` 内容添加到 remote-host 的用户 ifmicro 的用户主目录下的文件 `~/.ssh/authorized_keys` 的末尾
-
通过命令组合, 我们也可以实现同样的功能 :
- $ ssh ifmicro@remote-host 'mkdir -p .ssh && cat >> .ssh/authorized_keys && chmod 600 .ssh/authorized_keys' < ~/.ssh/id_rsa.pub
-
因此, 我们可以得出这样一个结论 :
- SSH 可以在用户和服务器之间,建立一条通道来实现命令和数据的传输
-
- $ tar czv hello-cpp | ssh ifmicro@remote-host 'tar xz'
-
- $ ssh ifmicro@remote-host 'tar czv hello-cpp' | tar xz
-
- $ ssh ifmicro@remote-host 'ls .. | wc -w'
-
- #!/bin/bash
- # 这只是一个远程自动化脚本的架构
- remote_auto(){
- ssh -T $1 <<"EOF"
- echo "Hi, I'm in $1, my name is"$(whoami);
- pwd;
- # 这里可以添加更多的命令
- EOF
- }
-
- # 这里可以继续添加更多其他服务器执行任务
- remote_auto ifmicro@remote-host-1 &
- remote_auto ifmicro@remote-host-1 &
- ...
-
- # 等待所有后台进程结束
- wait
- # 做些结果处理
-
当采用 Here Document 执行命令的时候,可能会出现 :
- Pseudo-terminal will not be allocated because stdin is not a terminal.
-
意思是无法分配一个伪终端给这个 ssh 链接
在伪终端中执行脚本, 可以进行交互
而没有伪终端, 则不能进行交互
因此对于此的解决方案就呼之欲出了 :
这里是 man ssh 中这两个参数的描述 :
- -T Disable pseudo-terminal allocation.
- -t Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based
- programs on a remote machine, which can be very useful, e.g. when implementing menu
- services. Multiple -t options force tty allocation, even if ssh has no local tty
-
我们上面的自动化脚本框架中使用了 ssh -T, 是因为对于自动化来说,基本上不用交互的
当然也有可能需要交互, 可以考虑 expect 来实现自动交互