expect安装测试-批量用户管理
安装:
yum list | grep expect
yum install expect
批量创建用户:
ansible mysqldb -m user -a 'name=ansible state=present'
ansible mysqldb -m raw -a 'echo 123456|passwd --stdin ansible'
批量添加sudo权限:
[root@zabbix ~]# cat ansible-sudo ----编辑sudo文件
## Allow root to run any commands anywhere
#root ALL=(ALL) ALL
ansible ALL=(ALL) ALL
# ansible mysqldb -m copy -a 'src=~/ansible-sudo dest=/etc/sudoers.d/' ----批量添加sudo文件
# ansible mysqldb -m file -a "dest=/etc/sudoers.d/ansible-sudo mode=440 owner=root group=root" ----修改权限
# ansible mysqldb -m shell -a "rm -f /etc/sudoers.d/ansible-sudo" ----批量删除文件
批量配置SSH 免密钥登录脚本:
[root@zabbix ~]# cat sendsshkey.sh
#!/bin/bash
USER=$1
if [ -z $1 ]; then
echo "Usage: ./$0 username passwd"
fi
function sendSSHKey(){
SSH='ssh-copy-id -i'
HOME_DIR=`cd ~ && pwd`
KEYSTORE=$HOME_DIR/.ssh/id_rsa.pub
# $SSH $KEYSTORE ${1}@${2}
echo "========$# -- $1 -- $2 -- $3 ========="
if [ ! $2 ] || [ ! $3 ] ;then
echo "ip/password not found !"
exit
else
expect -c "
spawn $SSH $KEYSTORE ${1}@${2}
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password*\" {send \"$3\r\"; exp_continue}
\"*Password*\" {send \"$3\r\";}
}
"
echo -e "\033[40;32m send sshkey to $2 success \033[0m\n"
fi
}
cat ./serverlist.txt |while read i
do
USER=$1
IP=`echo $i | awk '{print $1}'`
#echo " $# -- $1 -- $2"
if [ $# -eq 1 ];then
PASSWORD=`echo $i | awk '{print $2}'`
elif [ $# -eq 2 ];then
PASSWORD=$2
else
exit
fi
sendSSHKey $USER $IP $PASSWORD
done
批量配置免密钥登录IP:
[root@zabbix ~]# cat serverlist.txt
172.16.100.23
172.16.100.24
172.16.100.25
172.16.100.26
172.16.100.27
# ansible mysqldb -m shell -a "rm -f /root/.ssh/*" ----删除所有密钥,取消免密钥登陆配置
执行配置:
[root@zabbix ~]# sh sendsshkey.sh ansible 123456