#!/bin/bash
auto_ssh_key () {
expect -c "
set timeout -1;
spawn ssh-copy-id -i $path/.ssh/id_rsa $1;
expect {
*yes/no* {
send -- yes\r
expect *assword*
send -- $2\r;
expect {
*denied* {exit 2;}
eof
}
}
*assword* {
send -- $2\r;
expect {
*denied* {exit 2;}
eof
}
}
eof
}
"
}
echo ‘
-------------------- Directions for use --------------------
| Internal IP using examples: |
| please input port: 10 15 29 21 21 |
| External use IP example: |
| please input port: 8.8.8.8 google.xxx.com |
| |
| please input port: |
| A null value as the default port 22 |
| |
| please input username: |
| A null value as the current user |
------------------------------------------------------------
‘
path=`grep $(whoami) /etc/passwd | awk -F ‘:‘ ‘{print $6}‘`
read -p "please input ip:" ip
read -p "please input port:" port
read -p "please input username:" username
read -s -p "please input password:" password
# 判断是否指定端口, 默认为22
if [ -z $port ];then
port=‘22‘
fi
# 判断是否指定用户名, 默认为当前账号
if [ -z $username ];then
username=$(whoami)
fi
# 检测输入的IP是内部还是外部机器
data_size=`echo $ip | awk -F " " ‘{print $1}‘|wc -L`
if [ $data_size -lt 4 ];then
for i in $ip
do
auto_ssh_key ‘"-p ‘$port‘ ‘$username‘@10.0.0.‘$i‘"‘ $password
done
else
for i in $ip
do
auto_ssh_key ‘"-p ‘$port‘ ‘$username‘@‘$i‘"‘ $password
done
fi