如何从 bash 脚本中 ssh? [英] How to ssh from within a bash script?

查看:36
本文介绍了如何从 bash 脚本中 ssh?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 ssh 连接并从脚本中在远程服务器上执行一些操作.

但是终端提示我输入密码,然后在终端窗口而不是脚本中打开连接.在我退出连接之前不会执行这些命令.

如何在 bash 脚本中使用 ssh?

解决方案

  1. 如果您希望密码提示消失,请使用基于密钥的身份验证 (此处描述).

  2. 要通过 ssh 远程运行命令,您必须将它们作为参数提供给 ssh,如下所示:

<块引用>

root@host:~ # ssh root@www 'ps -ef |grep阿帕奇|grep -v grep |wc -l'

I am trying to create an ssh connection and do some things on the remote server from within the script.

However the terminal prompts me for a password, then opens the connection in the terminal window instead of the script. The commands don't get executed until I exit the connection.

How can I ssh from within a bash script?

解决方案

  1. If you want the password prompt to go away then use key based authentication (described here).

  2. To run commands remotely over ssh you have to give them as an argument to ssh, like the following:

root@host:~ # ssh root@www 'ps -ef | grep apache | grep -v grep | wc -l'

这篇关于如何从 bash 脚本中 ssh?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆