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"
donebash
3.用法介紹ssh
-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..net
-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.ip
#從命令行方式傳遞密碼 -p指定密碼
$ sshpass -p '123456' ssh user_name@host_ip
$ sshpass -p '123456' scp root@host_ip:/home/test/t ./tmp/get
如在多臺主機執行命令:it
[root@zhu ~]# cat a.sh
#!/bin/bash
for i in $(cat /root/1.txt)
do
echo $i
sshpass -p123456 ssh root@$i 'ls -l'
doneio