|
1 |
| -#!/bin/bash |
2 |
| - |
3 |
| -now=`date +%d%b%Y-%H%M` |
4 |
| - |
5 |
| -exp() |
6 |
| -{ |
7 |
| - "$1" <(cat <<-EOF |
8 |
| - spawn passwd $USER |
9 |
| - expect "Enter new UNIX password:" |
10 |
| - send -- "$passw\r" |
11 |
| - expect "Retype new UNIX password:" |
12 |
| - send -- "$passw\r" |
13 |
| - expect eof |
14 |
| - EOF |
15 |
| - ) |
16 |
| - echo "password for USER $USER updated successfully - adding to sudoers file now" |
17 |
| -} |
18 |
| - |
19 |
| -setup_pass() |
20 |
| -{ |
21 |
| - |
22 |
| -if [ $1 == "sles" ];then |
23 |
| - |
24 |
| - if [ ! -f /usr/bin/expect ] && [ ! -f /bin/expect ];then |
25 |
| -# zypper -y update |
26 |
| - zypper install -y expect |
27 |
| - exp "/usr/bin/expect" |
28 |
| - else |
29 |
| - exp "/usr/bin/expect" |
30 |
| - fi |
31 |
| - |
32 |
| -elif [ $1 == "ubuntu" ];then |
33 |
| - |
34 |
| - if [ ! -f /usr/bin/expect ] && [ ! -f /bin/expect ];then |
35 |
| - apt-get update |
36 |
| - apt install -y expect |
37 |
| - exp "/usr/bin/expect" |
38 |
| - else |
39 |
| - exp "/usr/bin/expect" |
40 |
| - fi |
41 |
| - |
42 |
| -elif [ $1 == "amzn" ];then |
43 |
| - |
44 |
| - echo $1 |
45 |
| - if [ ! -f /usr/bin/expect ] && [ ! -f /bin/expect ];then |
46 |
| - rpm -Uvh http://epel.mirror.net.in/epel/6/x86_64/epel-release-6-8.noarch.rpm |
47 |
| - yum install -y expect |
48 |
| - exp "/usr/bin/expect" |
49 |
| - else |
50 |
| - exp "/usr/bin/expect" |
51 |
| - fi |
52 |
| - |
53 |
| -elif [ $1 == "centos" ];then |
54 |
| - |
55 |
| - echo $1 |
56 |
| - if [ ! -f /usr/bin/expect ] && [ ! -f /bin/expect ];then |
57 |
| - rpm -Uvh http://epel.mirror.net.in/epel/6/x86_64/epel-release-6-8.noarch.rpm |
58 |
| - yum install -y expect |
59 |
| - exp "/bin/expect" |
60 |
| - else |
61 |
| - exp "/bin/expect" |
62 |
| - fi |
63 |
| -else |
64 |
| - echo "could not find case $1" |
65 |
| -fi |
66 |
| - |
67 |
| -} |
68 |
| - |
69 |
| -update_conf() |
70 |
| -{ |
71 |
| - sudofile="/etc/sudoers" |
72 |
| - sshdfile="/etc/ssh/sshd_config" |
73 |
| - mkdir -p /home/backup |
74 |
| - if [ -f $sudofile ];then |
75 |
| - cp -p $sudofile /home/backup/sudoers-$now |
76 |
| - sa=`grep $USER $sudofile | wc -l` |
77 |
| - if [ $sa -gt 0 ];then |
78 |
| - echo "$USER user already present in $sudofile - no changes required" |
79 |
| - grep $USER $sudofile |
80 |
| - else |
81 |
| -# echo "$USER ALL=(ALL) ALL" >> $sudofile |
82 |
| - echo "$USER ALL=(ALL) NOPASSWD: ALL" >> $sudofile |
83 |
| - echo "updated the sudoers file successfully" |
84 |
| - fi |
85 |
| - else |
86 |
| - echo "could not find $sudofile" |
87 |
| - fi |
88 |
| - |
89 |
| - if [ -f $sshdfile ];then |
90 |
| - cp -p $sshdfile /home/backup/sshd_config-$now |
91 |
| - sed -i '/ClientAliveInterval.*0/d' $sshdfile |
92 |
| - sed -i '/PermitRootLogin.*yes/d' $sshdfile |
93 |
| - sed -i '/PasswordAuthentication.*no/d' $sshdfile |
94 |
| - sed -i '/PasswordAuthentication.*yes/d' $sshdfile |
95 |
| - sed -i '/PermitRootLogin.*prohibit-password/d' $sshdfile |
96 |
| - echo "PermitRootLogin yes" >> $sshdfile |
97 |
| - echo "PasswordAuthentication yes" >> $sshdfile |
98 |
| - echo "ClientAliveInterval 240" >> $sshdfile |
99 |
| - echo "updated $sshdfile Successfully -- restarting sshd service" |
100 |
| - service sshd restart |
101 |
| - else |
102 |
| - echo "could not find $sshdfile" |
103 |
| - fi |
104 |
| -} |
105 |
| - |
106 |
| -############### MAIN ################### |
107 |
| - |
108 |
| -USER="devops" |
109 |
| -GROUP="devops" |
110 |
| -passw="today@1234" |
111 |
| - |
112 |
| -if [ -f /etc/os-release ];then |
113 |
| - osname=`grep ID /etc/os-release | egrep -v 'VERSION|LIKE|VARIANT' | cut -d'=' -f2 | sed -e 's/"//' -e 's/"//'` |
114 |
| - echo $osname |
115 |
| -else |
116 |
| - echo "can not locate /etc/os-release - unable find the osname" |
117 |
| - exit 8 |
118 |
| -fi |
119 |
| - |
120 |
| -case "$osname" in |
121 |
| - sles|amzn|ubuntu|centos) |
122 |
| - userdel -r $USER |
123 |
| - groupdel $GROUP |
124 |
| - sleep 3 |
125 |
| - groupadd $GROUP |
126 |
| - useradd $USER -m -d /home/$USER -s /bin/bash -g $GROUP |
127 |
| - setup_pass $osname |
128 |
| - update_conf |
129 |
| - ;; |
130 |
| - *) |
131 |
| - echo "could not determine the correct osname -- found $osname" |
132 |
| - ;; |
133 |
| -esac |
134 |
| -exit 0 |
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +now=`date +%d%b%Y-%H%M` |
| 4 | + |
| 5 | +exp() |
| 6 | +{ |
| 7 | + "$1" <(cat <<-EOF |
| 8 | + spawn passwd $USER |
| 9 | + expect "Enter new UNIX password:" |
| 10 | + send -- "$passw\r" |
| 11 | + expect "Retype new UNIX password:" |
| 12 | + send -- "$passw\r" |
| 13 | + expect eof |
| 14 | + EOF |
| 15 | + ) |
| 16 | + echo "password for USER $USER updated successfully - adding to sudoers file now" |
| 17 | +} |
| 18 | + |
| 19 | +setup_pass() |
| 20 | +{ |
| 21 | + |
| 22 | +if [ $1 == "sles" ];then |
| 23 | + |
| 24 | + if [ ! -f /usr/bin/expect ] && [ ! -f /bin/expect ];then |
| 25 | +# zypper -y update |
| 26 | + zypper install -y expect |
| 27 | + exp "/usr/bin/expect" |
| 28 | + else |
| 29 | + exp "/usr/bin/expect" |
| 30 | + fi |
| 31 | + |
| 32 | +elif [ $1 == "ubuntu" ];then |
| 33 | + |
| 34 | + if [ ! -f /usr/bin/expect ] && [ ! -f /bin/expect ];then |
| 35 | + apt-get update |
| 36 | + apt install -y expect |
| 37 | + exp "/usr/bin/expect" |
| 38 | + else |
| 39 | + exp "/usr/bin/expect" |
| 40 | + fi |
| 41 | + |
| 42 | +elif [ $1 == "amzn" ];then |
| 43 | + |
| 44 | + echo $1 |
| 45 | + if [ ! -f /usr/bin/expect ] && [ ! -f /bin/expect ];then |
| 46 | + rpm -Uvh http://epel.mirror.net.in/epel/6/x86_64/epel-release-6-8.noarch.rpm |
| 47 | + yum install -y expect |
| 48 | + exp "/usr/bin/expect" |
| 49 | + else |
| 50 | + exp "/usr/bin/expect" |
| 51 | + fi |
| 52 | + |
| 53 | +elif [ $1 == "centos" ];then |
| 54 | + |
| 55 | + echo $1 |
| 56 | + if [ ! -f /usr/bin/expect ] && [ ! -f /bin/expect ];then |
| 57 | + rpm -Uvh http://epel.mirror.net.in/epel/6/x86_64/epel-release-6-8.noarch.rpm |
| 58 | + yum install -y expect |
| 59 | + exp "/bin/expect" |
| 60 | + else |
| 61 | + exp "/bin/expect" |
| 62 | + fi |
| 63 | +else |
| 64 | + echo "could not find case $1" |
| 65 | +fi |
| 66 | + |
| 67 | +} |
| 68 | + |
| 69 | +update_conf() |
| 70 | +{ |
| 71 | + sudofile="/etc/sudoers" |
| 72 | + sshdfile="/etc/ssh/sshd_config" |
| 73 | + mkdir -p /home/backup |
| 74 | + if [ -f $sudofile ];then |
| 75 | + cp -p $sudofile /home/backup/sudoers-$now |
| 76 | + sa=`grep $USER $sudofile | wc -l` |
| 77 | + if [ $sa -gt 0 ];then |
| 78 | + echo "$USER user already present in $sudofile - no changes required" |
| 79 | + grep $USER $sudofile |
| 80 | + else |
| 81 | +# echo "$USER ALL=(ALL) ALL" >> $sudofile |
| 82 | + echo "$USER ALL=(ALL) NOPASSWD: ALL" >> $sudofile |
| 83 | + echo "updated the sudoers file successfully" |
| 84 | + fi |
| 85 | + else |
| 86 | + echo "could not find $sudofile" |
| 87 | + fi |
| 88 | + |
| 89 | + if [ -f $sshdfile ];then |
| 90 | + cp -p $sshdfile /home/backup/sshd_config-$now |
| 91 | + sed -i '/ClientAliveInterval.*0/d' $sshdfile |
| 92 | + sed -i '/PermitRootLogin.*yes/d' $sshdfile |
| 93 | + sed -i '/PasswordAuthentication.*no/d' $sshdfile |
| 94 | + sed -i '/PasswordAuthentication.*yes/d' $sshdfile |
| 95 | + sed -i '/PermitRootLogin.*prohibit-password/d' $sshdfile |
| 96 | + echo "PermitRootLogin yes" >> $sshdfile |
| 97 | + echo "PasswordAuthentication yes" >> $sshdfile |
| 98 | + echo "ClientAliveInterval 240" >> $sshdfile |
| 99 | + echo "updated $sshdfile Successfully -- restarting sshd service" |
| 100 | + service sshd restart |
| 101 | + else |
| 102 | + echo "could not find $sshdfile" |
| 103 | + fi |
| 104 | +} |
| 105 | + |
| 106 | +############### MAIN ################### |
| 107 | + |
| 108 | +USER="devops" |
| 109 | +GROUP="devops" |
| 110 | +passw="today@1234" |
| 111 | + |
| 112 | +if [ -f /etc/os-release ];then |
| 113 | + osname=`grep ID /etc/os-release | egrep -v 'VERSION|LIKE|VARIANT' | cut -d'=' -f2 | sed -e 's/"//' -e 's/"//'` |
| 114 | + echo $osname |
| 115 | +else |
| 116 | + echo "can not locate /etc/os-release - unable find the osname" |
| 117 | + exit 8 |
| 118 | +fi |
| 119 | + |
| 120 | +case "$osname" in |
| 121 | + sles|amzn|ubuntu|centos) |
| 122 | + userdel -r $USER |
| 123 | + groupdel $GROUP |
| 124 | + sleep 3 |
| 125 | + groupadd $GROUP |
| 126 | + useradd $USER -m -d /home/$USER -s /bin/bash -g $GROUP |
| 127 | + setup_pass $osname |
| 128 | + update_conf |
| 129 | + ;; |
| 130 | + *) |
| 131 | + echo "could not determine the correct osname -- found $osname" |
| 132 | + ;; |
| 133 | +esac |
| 134 | +exit 0 |
0 commit comments