-
Notifications
You must be signed in to change notification settings - Fork 1
/
radduser
executable file
·76 lines (68 loc) · 1.37 KB
/
radduser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
DOPASS=""
DOSUDO=""
usage()
{
echo "usage: $0 [-s] [-p] <youruser@host> <newuser> <authfile>
-s adds user to sudoers
-p generates a password"
}
while getopts "sph?" opt; do
case $opt in
p)
DOPASS=1
;;
s)
DOSUDO=1
;;
h)
usage
exit
;;
?)
usage
exit
;;
esac
done
shift $(($OPTIND -1))
myuserhost=$1
user=$2
authfile=$3
if [ ! $authfile ]
then
usage
exit
fi
if [ ! -e $authfile ]
then
echo "keyfile $authfile not found"
exit
fi
mypubkey=`cat $authfile`;
authkeyfile="~$user/.ssh/authorized_keys"
mycmd="sudo useradd $user;
sudo mkdir -p ~$user/.ssh;
sudo chown -R root ~$user/.ssh;
sudo chmod 700 ~$user/.ssh;
sudo touch $authkeyfile;
sudo bash -c 'cp $authkeyfile /tmp/';
sudo bash -c 'chmod 777 /tmp/authorized_keys';
grep -v '$mypubkey' /tmp/authorized_keys > /tmp/authkeyfile.new;
sudo echo '$mypubkey' >> /tmp/authkeyfile.new;
sudo bash -c 'mv /tmp/authkeyfile.new $authkeyfile';
sudo bash -c 'chmod 644 $authkeyfile';
sudo bash -c 'chown -R $user:$user ~$user/.ssh'
"
ssh -t $myuserhost "$mycmd"
if [[ $DOSUDO =~ 1 ]]
then
ssh -t $myuserhost "grep $user /etc/sudoers || { echo \"$user ALL=(ALL) NOPASSWD: ALL\" >> /etc/sudoers; }"
fi
if [[ $DOPASS =~ 1 ]]
then
NEWPASS=`pwgen -n 32`
echo "Setting password to $NEWPASS"
DOPASS="echo '$NEWPASS' | passwd $user --stdin"
ssh -t $myuserhost "$DOPASS"
fi