Skip to content

Instantly share code, notes, and snippets.

@mrioan
Last active September 4, 2023 13:34
Show Gist options
  • Save mrioan/16ec5ca46212447b5c9d8302cbce3e37 to your computer and use it in GitHub Desktop.
Save mrioan/16ec5ca46212447b5c9d8302cbce3e37 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/sh" >> /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/sh" >> /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
@372046933
Copy link

Great, works now. Thanks a million!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment