关于unix:带有远程登录/命令的SSH

SSH With Remote Login/Commands

如何使用SSH将多个命令发送到联网系统,以root用户身份登录并提供密码?

我知道你可以做到:

1
ssh -l <username> target_host

登录,但是我不确定如何提供密码和命令,因此都可以在一行中执行所有操作。我正在寻找一个对网络系统使用许多单行命令的脚本,这些脚本可以一次性在另一台计算机上运行。


ssh root @ host "命令"

即:ssh [email protected] " cat / etc / fstab "

如果您尝试执行多个命令,建议您使用某种形式的Expect脚本。我个人是Pexpect(Python)的粉丝。

使用密钥规避密码提示(来自man ssh):

The file ~/.ssh/authorized_keys lists the public keys that are
permitted
for logging in. When the user logs in, the ssh program tells the
server
which key pair it would like to use for authentication. The
client
proves that it has access to the private key and the server
checks that
the corresponding public key is authorized to accept the account.

The user creates his/her key pair by running ssh-keygen(1). This
stores
the private key in ~/.ssh/identity (protocol 1), ~/.ssh/id_dsa
(protocol
2 DSA), or ~/.ssh/id_rsa (protocol 2 RSA) and stores the public
key in
~/.ssh/identity.pub (protocol 1), ~/.ssh/id_dsa.pub (protocol 2
DSA), or
~/.ssh/id_rsa.pub (protocol 2 RSA) in the user's home directory.
The
user should then copy the public key to ~/.ssh/authorized_keys in
his/her
home directory on the remote machine. The authorized_keys file
correa€?
sponds to the conventional ~/.rhosts file, and has one key per
line,
though the lines can be very long. After this, the user can log
in witha€?
out giving the password.


您随时可以

1
ssh user@targetHost 'cd /tmp; ls -l'

1
2
myDir="/tmp"
ssh  user@targetHost"cd $myDir; ls -l"

我已经将完整的shell脚本链接为\\'command \\',但是我不建议这样做(-0)! 。

相反,将您的脚本复制到远程计算机上,然后运行它

1
 ssh user@targetHost 'yourRemoteScript'

最后,您无法提供密码将整个内容package在expect中,而很多系统却没有,很多都不允许,等等。如果要进行无人值守的操作,则必须使用ssh_keys。在S.O.上搜索有关准确执行操作的许多信息。

祝您好运,希望对您有所帮助。