1.yum安装
yum install sshpass -y
1.1编译安装
yum install wget -y
wget http://sourceforge.net/projects/sshpass/files/latest/download -O sshpass.tar.gz
tar -zxvf sshpass-1.06.tar.gz
cd sshpass-1.06
./configure
make
make install
2.自定义脚本
#!/bin/bash
password="admin123!"
for num in `seq 21 22`
do
sshpass -p $password ssh root@172.1x.1x.$num -o StrictHostKeyChecking=no "mkdir /tmp/sb"
done
3.用法介绍
-p password #后跟密码
[root@zhu ~]# sshpass -p 123456 ssh root@192.168.56.102
Last login: Wed Apr 16 15:35:22 2014 from 192.168.56.1
[root@jiang ~]# exit
logout
Connection to 192.168.56.102 closed.
-f filename #后跟保存密码的文件名,密码是文件内容的第一行。
[root@zhu ~]# cat 1.txt
123456
[root@zhu ~]# sshpass -f 1.txt ssh root@192.168.56.102
Last login: Fri Apr 18 13:48:20 2014 from 192.168.56.101
[root@jiang ~]# exit
logout
Connection to 192.168.56.102 closed.
-e #将环境变量SSHPASS作为密码
[root@zhu ~]# export SSHPASS=123456
[root@zhu ~]# sshpass -e ssh root@192.168.56.102
Last login: Fri Apr 18 13:51:45 2014 from 192.168.56.101
[root@jiang ~]# exit
logout
Connection to 192.168.56.102 closed.
#从命令行方式传递密码 -p指定密码
$ sshpass -p '123456' ssh user_name@host_ip
$ sshpass -p '123456' scp root@host_ip:/home/test/t ./tmp/
如在多台主机执行命令:
[root@zhu ~]# cat a.sh
#!/bin/bash
for i in $(cat /root/1.txt)
do
echo $i
sshpass -p123456 ssh root@$i 'ls -l'
done