Bash Regular expression for validating user accounts on Linux - linux

Looking for a simpler way to validate a user is requesting a password change for an account that is local and its a valid user ID format.
Valid user ID format is - ^[^a-zA-Z]*[a-zA-Z][^a-zA-Z]*t.*
With the first character being upper or lowercase letter followed by the letter t followed by 4 alphanumeric characters for a total of 6 characters long.
Currently I am using the follow to evaluate an if else - grep -Eow '\w{6}' /etc/passwd | grep ^[^a-zA-Z]*[a-zA-Z][^a-zA-Z]*t.* /etc/passwd
if grep -Eow '\w{6}' /etc/passwd | grep ^[^a-zA-Z]*[a-zA-Z][^a-zA-Z]*t.* /etc/passwd | grep -c "$cUname" $1 >/dev/null 2>&1; then
echo -e "$linuxPassword\n$newPassword\n$newPassword" | passwd
else
echo "Account is not a local account on this machine or not a valid account"
fi
Is there a better way of doing this?

if [[ $cUname == [[:alpha:]]t[[:alnum:]][[:alnum:]][[:alnum:]][[:alnum:]] ]] &&
grep -q "^${cUname}:" /etc/passwd
then
echo "OK to change password"
else
echo "$cUname: invalid username or username not found"
fi

Related

if condition for when a token is not found in a shell script

I am trying to construct an if-else block wherein one of the conditions is to echo a message if, when running a grep command on a text file, the specified token can not be found.
The grep command is
grep -i -n "token" file | cut -d':' -f 1
If the token is found, it will return the line number as usual. I want to know how to account for the case when the token does not exist in the text file and the terminal simply outputs nothing when the command is executed.
i.e.
if []
then
echo "This token does not exist in the file"
fi
I hope that's what you need:
result=$(grep -i -n "token" file | cut -d':' -f 1)
if [[ -z "$result" ]]; then
echo "Not found"
else
echo "$result"
fi

Bash Script To Remove Users From Wordlist

I wrote the following script to remove users from a wordlist. The wordlist has 3 or 4 fields which are First Name, Middle Name, Last Name, and User ID. I am using awk to create a username that comprises of the user's firstname initial, lastname and last two digits of their ID. Then using the command userdel with flag r to remove users with their home directories as well.
However when I run the script it gives me an error saying the following:
Usage: userdel [options] LOGIN
Options:
-f, --force force some actions that would fail otherwise
e.g. removal of user still logged in
or files, even if not owned by the user
-h, --help display this help message and exit
-r, --remove remove home directory and mail spool
-R, --root CHROOT_DIR directory to chroot into
-Z, --selinux-user remove any SELinux user mapping for the user
The script:
#! /bin/bash
# Removing users using positional parameters
getusername(){
line=${1}
len=`echo ${line}|awk '{ FS = " " } ; { print NF}'`
if [[ ${len} -eq 3 ]]
then
initial=`echo ${line}| awk {'print $1'} |cut -c1`
lastname=`echo ${line} | awk {'print $2'}`
id=`echo ${line}| awk {'print $3'}|grep -o '..$'`
username=`echo ${initial}${lastname}${id} |tr '[:upper:]' '[:lower:]'`
elif [[ ${len} -eq 4 ]]
then
initial=`echo ${line} | awk {'print $1'} |cut -c1`
lastname=`echo ${line} | awk {'print $3'}`
id=`echo ${line}| awk {'print $4'}|grep -o '..$'`
username=`echo ${initial}${lastname}${id} |tr '[:upper:]' '[:lower:]'`
else
echo "Line ${line} is not expected as it should be considered for creating Username and Password"
fi
}
sudo userdel -r $getusername
How to invoke userdel?
userdel -r username
This deletes the account of user username, and removes that user's home directory and associated mail files.
For that, you need to use a variable instead of to invoke a function.
Otherwise userdel will complain, like it is already doing or like just typing userdel.

Brute force bash script with another bash script

so i currently have a bash script that takes a hash value and then asks the user to input a password, converts their input to hash and then compares.
#!/bin/bash
crypt="8277e0910d750195b448797616e091ad"
echo "please enter a password!"
read inc
hash="$((echo -n $inc|md5sum) | awk '{print $1}')"
if [[ "$hash" == "$crypt" ]];
then
echo "logged in"
else
echo "incorrect pass"
fi
I now want to create another program that brute forces this password by adding values(from a-z) into the password input but im running into trouble as i feel my knowledge on bash file manipulation is limited as ive never ran a script against another script before.
#!/bin/bash
for i in {a..z}; do
(echo -n "$i: " && ./hashscript $i) | grep logged in
done
Since the 1st script is reading the data from stdin (with read), the 2nd script will need to pass the data in that way:
#!/bin/bash
for i in {a..z}; do
(echo -n "$i: " && echo $i | ./hashscript) | grep logged in
done

Beginner's Bash Scripting: "Unary Operator Expected" error && how to use grep to search for a variable from the output of a command?

I'm attempting to write a bash script that is executed through "./filename username" and checks whether or not that user is logged in, printing the result. I'm still new to scripting and am having trouble understanding how to make this work.
I'm currently getting the error "line 7: [: ambonill: unary operator expected". What does that mean and how can I go about fixing that error?
Additionally, how would I get grep to work instead of sort | uniq? I'd like to grep for the variable from the output of the command but can't find anything related in the man page.
#! /bin/bash
# This script will take a username as an argument and determine whether they are logged on.
function loggedin {
for u in `who | cut -f1 -d" " | sort | uniq`
do
if [ $u == $1 ]
then
echo "$1 is logged on"
else
echo "$1 is not logged on"
fi
exit 0
done
}
loggedin $u
exit 1
Try to find a simpler solution, like:
#!/bin/bash
echo "$1 is $([ -z "$(w -h $1)" ]&&echo -n not\ )logged on"

How do I search for a certain piece of text inside of a variable?

I am working on a script which prompts the user for their username. Once entered, the script uses the 'rwho' command to get a list of users who are logged into the network. It should crosscheck the text they entered (their username) with the results from the rwho command.
If a match is found then it displays a message saying so, if not then it also makes the user aware of this.
Here is the script and my attempt so far:
#!/bin/sh
#
# User network checking script
#
# Using rwho command to get user list
OUTPUT="$(rwho)"
echo "${OUTPUT}"
# Prompt for username
echo "Please enter your username: "
read username
# Input validation
if [ -z "$username"]
then
echo "No username supplied"
echo "Please enter your username: "
read username
fi
# Search for user
if `echo ${OUTPUT} | grep "${username}" 1>/dev/null 2>&1'
then
echo "$username is logged in."
else
echo "$username is not present."
fi
I consistently get errors with the Search for User part. I don't have outstanding knowledge of Linux so if anyone could fix this and help me I would be greatly appreciative.
Your usage of quotes is weird.
if echo "$OUTPUT" | grep -q "$username"
should work.
-q makes grep quiet (and is shorter than your redirections).

Resources