您的位置:首页 > 其它

批量操作SSH账户信任

2015-12-12 00:25 489 查看
#!/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=`awk NR==$(echo $(awk -F ':' '{print $1}' /etc/passwd | grep -xn $(whoami)) | awk -F ':' '{print $1}') /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`
for i in $ip
do
if [ $data_size -lt 4 ];then
auto_ssh_key '"-p '$port' '$username'@10.0.0.'$i'"' $password
else
auto_ssh_key '"-p '$port' '$username'@'$i'"' $password
fi
done

本文出自 “、矿泉水” 博客,请务必保留此出处http://guwenqiang.blog.51cto.com/5462040/1722230
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: