在Shell脚本中通过SSH登录远程主机的一个方法


下面给出步骤,其中我自己用户相关的信息隐去了。

(1)先生成一对RSA密钥:Passphrase是指密码短语,可以直接按回车,表示为空。
xxxxxxx:~> ssh-keygen  -t rsa        
Generating 2048-bit rsa key pair
   3 o.oOo.oOo..o
Key generated.
2048-bit rsa, xxxxxxxxxx, Thu May 23 2013 09:48:18 +0800
Passphrase :
Again      :
Key is stored with NULL passphrase.
 (You can ignore the following warning if you are generating hostkeys.)
 This is not recommended.
 Don't do this unless you know what you're doing.
 If file system protections fail (someone can access the keyfile),
 or if the super-user is malicious, your key can be used without
 the deciphering effort.
Private key saved to /home/xxxx/.ssh2/id_rsa_2048_a
Public key saved to /home/xxxx/.ssh2/id_rsa_2048_a.pub
通过这一步生成了密钥和公钥。

(2)将公钥复制到远程主机:
xxxxxxxx:~> ssh-copy-id  -i ~/.ssh2/id_rsa_2048_a.pub 
[email protected]
root's password:
Authentication successful.
Now try logging into the machine, with "ssh
'[email protected]'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

authorized_keys位于~/.ssh/目录中。


(3)用脚本登录远程主机:
#!/bin/sh

ssh [email protected]

exit  0

 
输入密码就可以直接登录了,不要每次都在命令行ssh。

你可能感兴趣的:(shell,ssh)