要实现ssh自动登录,网上搜了一下,主要有两种方法:1、生成公钥。2、编写expect脚本。这两种方法,还有一种是sshpass:
用法:
sshpass 参数SSH命令(ssh,sftp,scp等)。
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
-f filename Take password to use from file
-d number Use number as file descriptor for getting password
-p password Provide password as argument (security unwise)
-e Password is passed as env-var "SSHPASS"
With no parameters - password will be taken from stdin
-h Show help (this screen)
-V Print version information
At most one of -f, -d, -p or -e should be used
比如说:
scpabc@192.168.0.5:/home/xxx/test/root
这个命令的作用是将服务器端文件test传到本地文件夹/root下。
利用sshpass,假设密码为efghi,则可写作:
ssh -p efghiscp abc@192.168.0.5:/home/xxx/test /root
另外,对于ssh的第一次登陆,会提示:“Are you sure you want to continue connecting(yes/no)”,这时用sshpass会不好使,可以在ssh命令后面加上 -oStrictHostKeyChecking=no来解决。比如说上面的命令,就可以写作ssh -p efghi scpabc@192.168.0.5:/home/xxx/test /root -oStrictHostKeyChecking=no。
用法:
sshpass 参数SSH命令(ssh,sftp,scp等)。
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
-f filename Take password to use from file
-d number Use number as file descriptor for getting password
-p password Provide password as argument (security unwise)
-e Password is passed as env-var "SSHPASS"
With no parameters - password will be taken from stdin
-h Show help (this screen)
-V Print version information
At most one of -f, -d, -p or -e should be used
另外,对于ssh的第一次登陆,会提示:“Are you sure you want to continue connecting(yes/no)”,这时用sshpass会不好使,可以在ssh命令后面加上 -oStrictHostKeyChecking=no来解决。比如说上面的命令,就可以写作ssh -p efghi scpabc@192.168.0.5:/home/xxx/test /root -oStrictHostKeyChecking=no。
![用sshpass实现ssh的自动登陆 - 努力加贝 - 努力加贝 用sshpass实现ssh的自动登陆 - 努力加贝 - 努力加贝](http://img.bimg.126.net/photo/FRm3kWtuWb4KrB2_RV_KvQ==/862720803619618994.jpg)