这是我正在运行的脚本主体......
for i in `cat ${hostfile}`
do
echo "Adding user $username on $i"
ssh -q $i /usr/bin/sudo /usr/sbin/adduser $username --gecos "$username,,," --disabled-password;
echo "$username:$password" | /usr/bin/sudo chpasswd;
/usr/bin/sudo mkdir -p /home/$username/.ssh;
/usr/bin/sudo touch /home/$username/.ssh/authorized_keys;
/usr/bin/sudo chmod 600 /home/$username/.ssh/authorized_keys;
/usr/bin/sudo chmod 700 /home/$username/.ssh;
/usr/bin/sudo chown -R $username:$username /home/$username
#ssh -q $i /usr/bin/sudo /usr/sbin/adduser $username --gecos "$username,,," --disabled-password; sudo echo $username:'$password' | chpasswd; /usr/bin/sudo mkdir /home/$username/.ssh; /usr/bin/sudo touch /home/$username/.ssh/authorized_keys; /usr/bin/sudo chmod 600 /home/$username/.ssh/authorized_keys; /usr/bin/sudo chmod 700 /home/$username/.ssh; /usr/bin/sudo chown -R $username:$username /home/$username; adduser $username $group
done
它执行但不会创建.ssh 目录。
答案1
您的脚本仅运行一远程服务器上的命令:
ssh -q $i /usr/bin/sudo /usr/sbin/adduser $username --gecos "$username,,," --disabled-password;
此后的所有内容都将在本地计算机上运行。如果您想在同一ssh
会话中运行多个命令,则需要将它们ssh
作为单个命令传递。例如:
while IFS= read -r host; do
echo "Adding user $username on $i"
ssh -q "$host" "/usr/bin/sudo /usr/sbin/adduser '$username' --gecos '$username,,,' --disabled-password; \
echo '$username:$password' | /usr/bin/sudo chpasswd; \
/usr/bin/sudo mkdir -p /home/'$username'/.ssh; \
/usr/bin/sudo touch /home/'$username'/.ssh/authorized_keys; \
/usr/bin/sudo chmod 600 /home/'$username'/.ssh/authorized_keys; \
/usr/bin/sudo chmod 700 /home/'$username'/.ssh; \
/usr/bin/sudo chown -R '$username:$username' /home/'$username'"
done < "$hostfile"
请注意,所有命令都位于同一个" "
引号块内。\
行末的 只是为了清晰起见:它们允许您在单独的行中添加下一个命令。