【发布时间】:2017-03-13 12:25:02
【问题描述】:
我正在使用下面的代码来 ssh 到不同的节点并查找用户是否存在。如果用户不存在,它将创建它。
如果我不使用 ssh,脚本可以正常工作,但如果我使用 ssh,脚本会失败。
如何使用这个脚本遍历不同的节点?
for node in `nodes.txt`
usr=root
ssh $usr@$node
do
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "F
ailed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi
done
【问题讨论】:
-
为什么要添加没有主文件夹的用户?尝试在 useradd 命令中创建不带 -m 标志的用户。