美文网首页
本地shell脚本ssh到远程服务器并执行命令

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

作者: Alex0825 | 来源:发表于2020-09-27 21:28 被阅读0次
#!/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

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/nvzluktx.html