且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 bash 脚本中 ssh?

更新时间:2023-02-07 12:08:46

  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'