本地shell脚本ssh到远程服务器并执行命令

2020-09-27  本文已影响0人  Alex0825
#!/bin/bash  
ssh root@xxx.xxx.xx.xx  << remotessh 
pwd
ls
exit  
remotessh 

远程执行的命令行在"< < remotessh " 至" remotessh "之间,remotessh可以随便修改成其他形式。
如果不想在本机输出日志,可以修改为:

#!/bin/bash  
ssh root@xxx.xxx.xx.xx  > /dev/null 2>&1 << remotessh #无输出日志
pwd
ls
exit  
remotessh 

可能遇到的问题
问题:远程登录主机时出现Pseudo-terminal will not be allocated because stdin is not a terminal错误。
解决方案:伪终端将无法分配,因为标准输入不是终端。需要增加-t -t参数来强制伪终端分配,即使标准输入不是终端(to force pseudo-tty allocation even if stdin isn’t a terminal)

#!/bin/bash  
ssh -t -t root@xxx.xxx.xx.xx  > /dev/null 2>&1 << remotessh #无输出日志
pwd
ls
exit  
remotessh 

参考: https://blog.csdn.net/mouliu6141/article/details/105008282

上一篇 下一篇

猜你喜欢

热点阅读