Skip to content

Instantly share code, notes, and snippets.

@riipandi
Last active July 6, 2018 15:56
Show Gist options
  • Save riipandi/7974032 to your computer and use it in GitHub Desktop.
Save riipandi/7974032 to your computer and use it in GitHub Desktop.
Simple Linux User Management
#!/bin/bash
# echo "" > /usr/bin/dynmotd; chmod +x /usr/bin/dynmotd; nano /usr/bin/dynmotd
USER=`whoami`
MEMORY1=`free -t -m | grep "buffers/cache" | awk '{print $3" MB";}'`
MEMORY2=`free -t -m | grep "Mem" | awk '{print $2" MB";}'`
# time of day
HOUR=$(date +"%H")
if [ $HOUR -lt 12 -a $HOUR -ge 0 ]; then
TIME="morning"
elif [ $HOUR -lt 17 -a $HOUR -ge 12 ]; then
TIME="afternoon"
else
TIME="evening"
fi
# System uptime
uptime=`cat /proc/uptime | cut -f1 -d.`
upDays=$((uptime/60/60/24))
upHours=$((uptime/60/60%24))
upMins=$((uptime/60%60))
# System load
LOAD1=`cat /proc/loadavg | awk {'print $1'}`
LOAD5=`cat /proc/loadavg | awk {'print $2'}`
LOAD15=`cat /proc/loadavg | awk {'print $3'}`
echo "
===================================================================
- Good $TIME $USER :-)
===================================================================
- Hostname............: `hostname -f`
- Release.............: `uname -a | awk '{print $1" "$3" "$12}'`
- Users...............: Currently `users | wc -w` user(s) logged on
===================================================================
- Current user........: $USER
- CPU usage...........: $LOAD1, $LOAD5, $LOAD15 (1, 5, 15 min)
- Memory used.........: $MEMORY1 / $MEMORY2
- Swap in use.........: `free -m | tail -n 1 | awk '{print $3}'` MB
- Processes...........: `ps -Afl | wc -l` running
- System uptime.......: $upDays days $upHours hours $upMins minutes
===================================================================
"
#!/bin/bash
# clear; echo "" > /usr/bin/uadd; chmod +x /usr/bin/uadd; nano /usr/bin/uadd
#
# Contoh penggunaan: uadd [nama] [sesi] [masa_aktif]
#
## Cek Root Dulu
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "Only root may add a user to the system"
exit 1
fi
group_1=`egrep "^single" /etc/group >/dev/null`
group_2=`egrep "^double" /etc/group >/dev/null`
## Validasi dulu
if [[ -n $1 && -n $2 && -n $3 ]] ; then
## Buat Group
if [[ -n "$group_1" && -n "$group_2" ]] ; then
groupadd -g 6001 single; groupadd -g 6002 double
echo "@single hard nproc 1" > /etc/security/limits.conf
echo "@double hard nproc 2" >> /etc/security/limits.conf
fi
## Buat User
egrep "^$1" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "User: $1 exists!"
exit 1
else
## Password dan Grup
if [[ -n $4 ]] ; then
paswrd=$4
else
paswrd=`cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1`
fi
if [ $2 = 1 ]
then
useradd -r -s /bin/false -d /bin/null -g single $1
elif [ $2 = 2 ]
then
useradd -r -s /bin/false -d /bin/null -g double $1
else
echo "Sesi Login hanya 1 atau 2"
exit 2
fi
## Set password dan Masa Aktif
echo "$1:$paswrd" | chpasswd
chage -M $3 $1
## Cetak ke layar
info_pas_exp=`chage -l $1 | grep "Password expires" | tr -s [:space:]`
info_pas_ina=`chage -l $1 | grep "Password inactive" | tr -s [:space:]`
if [ $? -eq 0 ]; then
echo "------------------------------------------"
echo -e "User: $1 created with pass: $paswrd"
echo $info_pas_exp
echo $info_pas_ina
echo "------------------------------------------"
else
echo "Gagal membuat user :-("
exit 3
fi
fi
else
echo "Contoh penggunaan: uadd [nama] [sesi] [masa_aktif]"
exit 0
fi
#!/bin/bash
# clear; echo "" > /usr/bin/ugen; chmod +x /usr/bin/ugen; nano /usr/bin/ugen
#
## Cek Root Dulu
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "Only root may add a user to the system"
exit 1
fi
SENDTOMAIL=false
EMAIL_FROM="[email protected]"
EMAIL_FRWD="[email protected]"
group_1=`egrep "^single" /etc/group >/dev/null`
group_2=`egrep "^double" /etc/group >/dev/null`
## Buat Group
if [[ -n "$group_1" && -n "$group_2" ]] ; then
groupadd -g 6001 single; groupadd -g 6002 double
echo "@single hard nproc 1" > /etc/security/limits.conf
echo "@double hard nproc 2" >> /etc/security/limits.conf
fi
## Buat User
read -p "Masukkan Username : " username
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "User: $username exists!"
exit 1
else
## Input Password
read -s -p "Masukkan Password : " get_pass
if [ -n "$get_pass" ]; then
paswrd=$get_pass
else
paswrd=`cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1`
fi
## Input Jenis Grup
echo ""
read -p "Sesi Login (1/2) : " grup_tipe
if [ $grup_tipe = 1 ]
then
useradd -r -s /bin/false -d /bin/null -g single $username
elif [ $grup_tipe = 2 ]
then
useradd -r -s /bin/false -d /bin/null -g double $username
else
echo "Sesi Login hanya 1 atau 2"
exit 1
fi
## Set password dan Masa Aktif
read -p "Masa aktif (hari) : " psswd_age
echo "$username:$paswrd" | chpasswd
chage -M $psswd_age $username
## Cetak ke layar
info_pas_exp=`chage -l $username | grep "Password expires" | tr -s [:space:]`
info_pas_ina=`chage -l $username | grep "Password inactive" | tr -s [:space:]`
if [ $? -eq 0 ]; then
echo ""
echo "------------------------------------------"
echo -e "User: $username created with pass: $paswrd"
echo $info_pas_exp
echo $info_pas_ina
echo "------------------------------------------"
## Kirim Email
if [ $SENDTOMAIL = true ]; then
read -p "Kirim via email ke : " email_to
echo "------------------------------------------"
isi_email="Subject:User Created\nUser $username created with pass $paswrd\n$info_pas_exp\n$info_pas_ina"
echo -e $isi_email | ssmtp -f $EMAIL_FROM -F "Admin Ospian" $EMAIL_FRWD
[ -n "$email_to" ] && echo -e $isi_email | ssmtp -f $EMAIL_FROM -F "Admin Ospian" $email_to
echo "Email terkirim :-)"
fi
else
echo "Gagal membuat user :-("
exit 1
fi
fi
#!/bin/bash
# clear; echo "" > /usr/bin/uinfo; chmod +x /usr/bin/uinfo; nano /usr/bin/uinfo
#
# Contoh penggunaan: uinfo [nama]
#
## Cek Root Dulu
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "Only root may add a user to the system"
exit 1
fi
if [ "$1" = "" ]
then
echo
echo "Usage: $0 USERNAME"
echo
echo "Example: $0 kam"
echo
exit 1
fi
Username=`cat /etc/passwd | grep -Ew ^$1 | cut -d":" -f1`
if [ "$Username" = "" ]
then
echo "Username $1 doesn't exist"
exit 2
fi
Userid=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f3`
UserPrimaryGroupId=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f4`
UserPrimaryGroup=`cat /etc/group | grep :"$UserPrimaryGroupId": | cut -d":" -f1`
UserInfo=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f5`
UserHomeDir=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f6`
UserShell=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f7`
UserGroups=`groups $Username | awk -F": " '{print $2}'`
PasswordExpiryDate=`chage -l $Username | grep "Password expires" | awk -F": " '{print $2}'`
LastPasswordChangeDate=`chage -l $Username | grep "Last password change" | awk -F": " '{print $2}'`
AccountExpiryDate=`chage -l $Username | grep "Account expires" | awk -F": " '{print $2}'`
HomeDirSize=`du -hs $UserHomeDir | awk '{print $1}'`
echo
printf "%-25s : %5s [User Id - %s]\n" "Username" "$Username" "$Userid"
printf "%-25s : %5s\n" "User Info" "$UserInfo"
echo
printf "%-25s : %5s [Group Id - %s]\n" "User's Primary Group" "$UserPrimaryGroup" "$UserPrimaryGroupId"
printf "%-25s : %5s\n" "User is Member of Groups" "$UserGroups"
echo
printf "%-25s : %5s [Size Occupied - %s]\n" "Home Directory" "$UserHomeDir" "$HomeDirSize"
printf "%-25s : %5s\n" "Default Shell" "$UserShell"
echo
printf "%-25s : %5s\n" "Last Password Changed On" "$LastPasswordChangeDate"
printf "%-25s : %5s\n" "Password Expiry Date" "$PasswordExpiryDate"
printf "%-25s : %5s\n" "Account Expiry Date" "$AccountExpiryDate"
echo
#!/bin/bash
# clear; echo "" > /usr/bin/ulists; chmod +x /usr/bin/ulists; nano /usr/bin/ulists
#
## Cek Root Dulu
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "Only root may add a user to the system"
exit 1
fi
echo -e "---------------------------------------------------------------------------------"
printf "%-3s %-15s %-15s %-15s %-15s %-15s \n" "##" "[Username]" "[Group]" "[Shell]" "[PassChanged]" "[PassExpired]"
echo -e "---------------------------------------------------------------------------------"
no=0
awk -F: '($3>=200) && ($3!=65534)' /etc/passwd | cut -d":" -f1 | \
while read Username ; do
no=$(( $no + 1 ))
Userid=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f3`
UserPrimaryGroupId=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f4`
UserPrimaryGroup=`cat /etc/group | grep :"$UserPrimaryGroupId": | cut -d":" -f1`
UserInfo=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f5`
UserHomeDir=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f6`
UserShell=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f7`
UserGroups=`groups $Username | awk -F": " '{print $2}'`
PasswordExpiryDate=`chage -l $Username | grep "Password expires" | awk -F": " '{print $2}'`
LastPasswordChangeDate=`chage -l $Username | grep "Last password change" | awk -F": " '{print $2}'`
AccountExpiryDate=`chage -l $Username | grep "Account expires" | awk -F": " '{print $2}'`
#HomeDirSize=`du -hs $UserHomeDir | awk '{print $1}'`
printf "%-3s %-15s %-15s %-15s %-15s %-15s \n" "$no" "$Username" "$UserPrimaryGroup" "$UserShell" "$LastPasswordChangeDate" "$PasswordExpiryDate"
done
echo -e "---------------------------------------------------------------------------------"
echo -e "Script by: [email protected] http://aris.ospian.com/"
echo -e "---------------------------------------------------------------------------------"
echo -e "Untuk menambah user gunakan perintah : uadd [nama] [sesi] [masa_aktif]"
echo -e "Untuk menghapus user gunakan perintah : userdel [nama]"
echo -e "Untuk ganti password gunakan perintah : passwd [nama]"
echo -e "---------------------------------------------------------------------------------"
echo
#!/bin/bash
# clear; echo "" > /usr/bin/umon; chmod +x /usr/bin/umon; nano /usr/bin/umon
#
## Cek Root Dulu
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "Only root may add a user to the system"
exit 1
fi
echo -e "---------------------------------------------------------------------------------"
printf "%-15s %-15s %-15s %-15s \n" "[USER]" "[KONEK VIA]" "[LOGIN SEJAK]*" "[INFO]"
echo -e "---------------------------------------------------------------------------------"
#1 3 4 5 6 7 5
#sisadmin pts/3 180.253.3.115 Thu Dec 19 00:31 still logged in
#hafiez pts/0 127.0.0.1 Mon Dec 16 12:41 - 12:41
ps aux | grep "sshd" | sort -k 72
tgl_skrg=`date | awk '{print $3}'`
last -n 20 -a dena | awk -F: '($6=$tgl_skrg)'
last | grep -Ew ^dena | awk '{print $1}'
last | grep 'pts' | grep '127.0.0.1'
| \
while read Username ; do
username=`last -50 | grep -Ew ^$Username | cut -d":" -f1`
konekvia=`last -50 | grep $Username | awk '{print $3}'`
loginsjk=`last -50 | grep $Username | awk '{print $7 $6 $5}'`
info_pid=`last -50 | grep $Username | awk '{print $1}'`
printf "%-15s %-15s %-15s %-15s \n" "$username" "$konekvia" "$loginsjk" "$info_pid"
done
echo -e "---------------------------------------------------------------------------------"
echo -e "Kick user : kill -9 <PID>"
echo -e "Contohnya : kill -9 1234"
echo -e "---------------------------------------------------------------------------------"
echo -e "*) Waktu Server [by: [email protected]]"
echo -e "---------------------------------------------------------------------------------"
#-------------------------------------------------------------------
#[USER] [KONEK VIA] [LOGIN SEJAK]* [INFO]
#-------------------------------------------------------------------
#superman 123.345.567.789:91827 00:00:00 30 Nov PID[1234]
#superman 123.345.567.789:91827 00:00:00 30 Nov PID[1234]
#superman 123.345.567.789:91827 00:00:00 30 Nov PID[1234]
#-------------------------------------------------------------------
#!/bin/bash
# echo "" > /usr/bin/usexpt; chmod +x /usr/bin/usexpt; nano /usr/bin/usexpt
#
## Cek Root Dulu
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "Only root may add a user to the system"
exit 1
fi
if [[ -n $1 ]]; then
cd $1
now="$(date +'%Y_%m_%d')"
nma="userlist-$now.zip"
awk -F: '($3>=500) && ($3!=65534)' /etc/passwd > passwd.new
awk -F: '($3>=500) && ($3!=65534)' /etc/group > group.new
awk -F: '($3>=500) && ($3!=65534) {print $1}' /etc/passwd | grep -f - /etc/shadow > shadow.new
zip $nma *.new; rm *.new
echo "User berhasil di-export ke: $1/$nma"
else
echo "Contoh penggunaan: usexpt [lokasi_folder_tujuan]"
exit 2
fi
#!/bin/bash
# echo "" > /usr/bin/usimpt; chmod +x /usr/bin/usimpt; nano /usr/bin/usimpt
#
## Cek Root Dulu
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "Only root may add a user to the system"
exit 1
fi
if [[ -n $1 ]]; then
cd /tmp; unzip $1
cat passwd.new >> /etc/passwd
cat group.new >> /etc/group
cat shadow.new >> /etc/shadow
cat /etc/passwd > passwd.join
cat /etc/group > group.join
cat /etc/shadow > shadow.join
cat passwd.join | awk '{ if (!seen[$0]++) print}' > passwd.ok
cat group.join | awk '{ if (!seen[$0]++) print}' > group.ok
cat shadow.join | awk '{ if (!seen[$0]++) print}' > shadow.ok
cat passwd.ok > /etc/passwd
cat group.ok > /etc/group
cat shadow.ok > /etc/shadow
rm *.new *.join *.ok
echo "User berhasil di-import !!"
else
echo "Contoh penggunaan: usimpt [lokasi_file_zip_backup]"
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment