为 vps 批量添加用户,为该用户的ssh,vpn,ftp服务提供统一的帐号密码。
使用方法
- $ ./adduser.sh # 从users.list文件读入用户名,并为该用户名生成users.XXXX形式的密码(XXXX为随机数)
-
$ ./addvpn.sh # 从users.list文件读入用户名和密码, 同步到 /etc/ppp/chap-secrets 文件.
命令脚本
- #
- #users.list
- #
-
user1
-
user2
-
user3
-
user4
#
# adduser.sh
#
-
#!/bin/bash
-
-
ERROR () {
-
echo "error occur: $1"
-
exit 1
-
-
}
-
-
-
LIST="./users.list"
-
PASSWD="./passwd.exp"
-
export IFS=':'
-
-
#set -x
-
[[ $UID -ne 0 ]] && echo "Permission deny" && exit 1
-
-
while read login passwd; do
-
if [[ -z $passwd ]] ; then
-
echo "Add account: $login..."
-
useradd -N -m -G sshd -s /bin/false $login || ERROR "useradd failed"
-
passwd=$(mktemp -u ${login}.XXXX)
-
$PASSWD $login $passwd || ERROR "$PASSWD failed"
-
sed -i "s/${login}.*/$login:$passwd/" $LIST
-
echo "--------------------"
-
fi
-
done < $LIST
#
# passwd.exp
#
-
#!/usr/bin/expect -f
-
-
# ./passwd.exp $login $passwd
-
-
set password [lindex $argv 1]
-
set login [lindex $argv 0]
-
-
set ret 0
-
set timeout 3
-
-
spawn passwd $login
-
expect "*password:"
-
sleep 1
-
send "$password\r"
-
expect "*password:"
-
sleep 1
-
send "$password\r"
-
-
expect {
-
"*successfully" {
-
send_user "# passwd $login $password Okay\n"
-
}
-
timeout {
-
send_user "PASSWD error";
-
set ret 1
-
}
-
}
-
-
-
exit $ret
#
# addvpn.sh
#
-
#!/bin/bash
-
-
# add user.list to /etc/ppp/chap-secrets
-
-
ERROR () {
-
echo "error occur: $1"
-
exit 1
-
-
}
-
-
LIST="./users.list"
-
CHAP_SECRETS="/etc/ppp/chap-secrets"
-
export IFS=':'
-
-
[[ $UID -ne 0 ]] && echo "Permission deny" && exit 1
-
-
while read login passwd; do
-
if [[ -n $passwd ]]; then
-
echo "Add vpn accout: ${login}@${passwd}..."
-
sed -i "/${login}/d" $CHAP_SECRETS
-
echo -e "${login}\tpptpd\t${passwd}\t*" >> ${CHAP_SECRETS} || ERROR "sed failed"
-
echo "--------------------"
-
fi
-
done < $LIST
阅读(521) | 评论(0) | 转发(0) |