Skip to content

Instantly share code, notes, and snippets.

@372046933
Forked from mrioan/create_user.sh
Last active September 4, 2023 13:36
Show Gist options
  • Save 372046933/26628e95f47db710f1ea1e3a6e90ccdf to your computer and use it in GitHub Desktop.
Save 372046933/26628e95f47db710f1ea1e3a6e90ccdf to your computer and use it in GitHub Desktop.
Script to create users in an Asus router (OpenWRT) to login via SSH for instance
#!/bin/sh
echo -e "\nUsage: create_user.sh <username> <password>"
USERNAME=$1
PASSWORD=$2
echo "Creating user ${USERNAME}"
if [ ! -d "/home/${USERNAME}" ]
then
echo "Directory /home/${USERNAME} does not exist. Let's continue normally."
mkdir /home/${USERNAME}
chown ${USERNAME} /home/${USERNAME}
echo "${USERNAME}:x:200:200:${USERNAME}:/home/${USERNAME}:/bin/bash" >> /jffs/configs/passwd.add
echo "${USERNAME}:@ZZZZ@:0:0:99999:7:0:0:" >> /jffs/configs/shadow.add
echo "${USERNAME}:x:200:" >> /jffs/configs/group.add
echo "${USERNAME}:*:200:" >> /jffs/configs/gshadow.add
echo "${USERNAME}:x:200:200:${USERNAME}:/home/${USERNAME}/:/bin/bash" >> /etc/passwd
echo "mkdir /home/${USERNAME}" >> /jffs/configs/users_to_add_on_every_reboot.sh
echo "chown ${USERNAME} /home/${USERNAME}" >> /jffs/configs/users_to_add_on_every_reboot.sh
else
echo "Directory /home/${USERNAME} already exists. Let's only update the password."
sed -i.bak "/${USERNAME}:/d" /jffs/configs/shadow.add
echo "${USERNAME}:@ZZZZ@:0:0:99999:7:0:0:" >> /jffs/configs/shadow.add
fi
echo "${USERNAME}:$PASSWORD" | sudo chpasswd
ENCRYPTED_PASSWORD=`cat /etc/passwd | grep -Po "${USERNAME}:\K(.*)(?=:200:200:)" | head -n 1`
#echo "New encrypted password $ENCRYPTED_PASSWORD"
sed -i "s/${USERNAME}:@ZZZZ@/${USERNAME}:${ENCRYPTED_PASSWORD}/g" /jffs/configs/shadow.add
if [ $? -eq 0 ]; then
echo -e "All done\n"
else
echo -e "- IMPORTANT ERROR. PLEASE READ - User action is required: The last command (sed) failed since it is likely that the generated encrypted password has a '/'. You must either a) re-run this script (the time-based seed will create a different password), or b) copy the encrypted password from /etc/passwd and paste it into /jffs/configs/shadow.add replacing @ZZZZ@\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment