When I try to use an ssh command in a shell script, the command just sits there. Do you have an example of how to use ssh in a shell script?
当我尝试在shell脚本中使用ssh命令时,命令就在那里。你有一个如何在shell脚本中使用ssh的例子吗?
5 个解决方案
#1
30
Depends on what you want to do, and how you use it. If you just want to execute a command remotely and safely on another machine, just use
取决于你想做什么,以及你如何使用它。如果您只想在另一台机器上远程安全地执行命令,请使用
ssh user@host command
for example
ssh user@host ls
In order to do this safely you need to either ask the user for the password during runtime, or set up keys on the remote host.
为了安全地执行此操作,您需要在运行时询问用户密码,或者在远程主机上设置密钥。
#2
20
First, you need to make sure you've set up password-less (public key login). There are at least two flavors of ssh with slightly different configuration file formats. Check the ssh manpage on your system, consult you local sysadmin or head over to How do I setup Public-Key Authentication?.
首先,您需要确保已设置无密码(公钥登录)。至少有两种ssh配置文件格式略有不同。检查系统上的ssh联机帮助页,咨询本地sysadmin或转到如何设置公钥验证?
To run ssh in batch mode (such as within a shell script), you need to pass a command you want to be run. The syntax is:
要以批处理模式运行ssh(例如在shell脚本中),您需要传递要运行的命令。语法是:
ssh host command
If you want to run more than one command at the same time, use quotes and semicolons:
如果要同时运行多个命令,请使用引号和分号:
ssh host "command1; command2"
The quotes are needed to protect the semicolons from the shell interpreter. If you left them out, only the first command would be run remotely and all the rest would be run on the local machine.
需要引号来保护分析符不受shell解释器的影响。如果将它们遗漏,只有第一个命令可以远程运行,其余所有命令都将在本地机器上运行。
#4
0
You need to put your SSH public key into the ~/.ssh/authorized_keys
file on the remote host. Then you'll be able to SSH to that host password-less.
您需要将SSH公钥放入远程主机上的〜/ .ssh / authorized_keys文件中。然后,您将能够以无密码的方式SSH到该主机。
Alternatively you can use ssh-agent
. I would recommend against storing the password in the script.
或者,您可以使用ssh-agent。我建议不要在脚本中存储密码。
#5
-2
The easiest way is using a certificate for the user that runs the script.
最简单的方法是为运行脚本的用户使用证书。
A more complex one implies adding to stdin the password when the shell command asks for it. Expect, perl libraries, show to the user the prompt asking the password (if is interactive, at least), there are a lot of choices.
更复杂的一个意味着当shell命令要求时向stdin添加密码。期待,perl库向用户显示提示输入密码(如果是交互式的,至少),有很多选择。
#1
30
Depends on what you want to do, and how you use it. If you just want to execute a command remotely and safely on another machine, just use
取决于你想做什么,以及你如何使用它。如果您只想在另一台机器上远程安全地执行命令,请使用
ssh user@host command
for example
ssh user@host ls
In order to do this safely you need to either ask the user for the password during runtime, or set up keys on the remote host.
为了安全地执行此操作,您需要在运行时询问用户密码,或者在远程主机上设置密钥。
#2
20
First, you need to make sure you've set up password-less (public key login). There are at least two flavors of ssh with slightly different configuration file formats. Check the ssh manpage on your system, consult you local sysadmin or head over to How do I setup Public-Key Authentication?.
首先,您需要确保已设置无密码(公钥登录)。至少有两种ssh配置文件格式略有不同。检查系统上的ssh联机帮助页,咨询本地sysadmin或转到如何设置公钥验证?
To run ssh in batch mode (such as within a shell script), you need to pass a command you want to be run. The syntax is:
要以批处理模式运行ssh(例如在shell脚本中),您需要传递要运行的命令。语法是:
ssh host command
If you want to run more than one command at the same time, use quotes and semicolons:
如果要同时运行多个命令,请使用引号和分号:
ssh host "command1; command2"
The quotes are needed to protect the semicolons from the shell interpreter. If you left them out, only the first command would be run remotely and all the rest would be run on the local machine.
需要引号来保护分析符不受shell解释器的影响。如果将它们遗漏,只有第一个命令可以远程运行,其余所有命令都将在本地机器上运行。
#3
#4
0
You need to put your SSH public key into the ~/.ssh/authorized_keys
file on the remote host. Then you'll be able to SSH to that host password-less.
您需要将SSH公钥放入远程主机上的〜/ .ssh / authorized_keys文件中。然后,您将能够以无密码的方式SSH到该主机。
Alternatively you can use ssh-agent
. I would recommend against storing the password in the script.
或者,您可以使用ssh-agent。我建议不要在脚本中存储密码。
#5
-2
The easiest way is using a certificate for the user that runs the script.
最简单的方法是为运行脚本的用户使用证书。
A more complex one implies adding to stdin the password when the shell command asks for it. Expect, perl libraries, show to the user the prompt asking the password (if is interactive, at least), there are a lot of choices.
更复杂的一个意味着当shell命令要求时向stdin添加密码。期待,perl库向用户显示提示输入密码(如果是交互式的,至少),有很多选择。