如何使用 ssh 连接到远程服务器并获取信息
我正在尝试使用 bash 编写 shell 脚本。我有多个服务器,每个服务器上运行多个应用程序。每个服务器还有特定的应用程序脚本来检查/停止/启动等。我想做的就是执行 ssh 并连接到远程服务器。 我也能够成功地做到这一点并执行命令。
在某些情况下,我需要检查远程计算机上的某些进程状态,应用程序特定的脚本已经做到了这一点。但是当我尝试执行该脚本时使用我的 ssh 我没有得到任何信息(它被执行但没有传递任何信息)。我如何从远程主机获取信息并显示在本地主机上。
非常感谢对此的任何帮助。
问候, 森尼
I'm trying to write a shell script using bash. I have multiple servers and each servers have multiple apps runnings on it. Each server also has specific app scripts to check/stop/start etc. All I want to do is that, do a ssh and connect to the remote server.
Which I'm also able to do sucessfully and exceute the commands also..
In some instance I need to check some process status on a remote machine, the app sepecific scripts already does that. But using my ssh when i try to execute that script I dont get any info ( it gets executed but no info is passed ). How do i get the information from the remote host and display on the local host here.
Any help on this is really appreciated.
Regards,
Senny
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过将命令作为字符串传递到 ssh 来运行远程命令并在本地获取结果。
在脚本中,您可以执行以下操作:
该命令将远程运行,并将输出存储在
CMD_OUT
变量中。然后,您可以解析脚本中的输出以获得您想要的结果。为了简化脚本的使用,您可能需要设置 无密码 ssh 这样您就不必在每次脚本尝试运行远程命令时都输入密码。
You can run remote commands and get results locally by passing the command as a string to ssh.
In your script, you can do:
The command will be run remotely and the output store in the
CMD_OUT
variable. You can then parse the output in your script to get the results you want.To simplify usage of your script, you might want to set up passwordless ssh so you don't have to type your password each time the script tries to run a remote command.