How to create username from txt file - bash

I am trying to create a user from a a text file called names.txt
(first and second field will be comment, third field will be username and forth field will be password)
Jessica Brown,jessicabrown,id0001
James Santos,jamessantos,id0002
This is what I have so far, but it is not working and I have a feeling I can do it a shorter way but can't figure it out.
user_name=$(cat names.txt | cut -d, -f3)
password1=$(cat names.txt | cut -d, -f4)
comment1=$(cat names.txt | cut -d, -f1 -f2)
user_name2=$(cat names.txt | cut -d, -f3)
password2=$(cat names.txt | cut-d, -f4)
comment2=$(cat names.txt | cut -d, -f1 -f2)
useradd "$user_name" -p "$password1" -c "$comment1"
useradd "$user_name2" -p "$password2" -c "$comment2"

To read the file and split the fields you may simply use read-while loop
Check man useradd for proper options for the command
Example (echo is here only for demonstration, remove it in final script):
#!/usr/bin/env bash
while IFS="," read -r COMMENT1 COMMENT2 USERNAME PASSWORD ; do
echo useradd "$USERNAME" --password "$PASSWORD" --comment "$COMMENT1,$COMMENT2"
done <names.txt

Related

How do I fix my error saying the home directory is invalid?

I'm still learning how to work with bash but my program is part of part 1 which is posted below but they are wanting us to use the file cs.rosters.txt to generate the new_users.txt file.
The cs.rosters.txt looks like this:
CMPSC 1513 03|doejan|Doe, Jane|0510|0350
CMPSC 1513 03|smijoh|Smith, John|0510
CMPSC 1133 01|cp2stu3| CPII, Student3 Neither|2222|0020
Below is what I created for part 1 which runs correctly:
#!/bin/bash
while read -r line || [[ -n "$line" ]]; do
username=$(echo "$line" | cut -d: -f1)
GECOS=$(echo "$line" | cut -d: -f5)
homedir=$(echo "$line" | cut -d: -f6)
echo "adduser -g '$GECOS' -d '$homedir' -s /bin/bash '$username'"
done < "new_users.txt"
Here's where I'm struggling
The code above pretty much displays the information in the new_users.txt file
I'm struggling on the second part, my code is posted below:
What I want to do is create a script that generates the cs.rosters.txt file like I did above.
#!/bin/bash
while read line; do
user=$(echo $line | cut -d'|' -f2)
pass=$(echo $line | cut -d'|' -f3)
home=$(echo $line | cut -d'|' -f4)
useradd -m -d $home -s /bin/bash $user
echo $pass | passwd --stdin $user
done < cs_roster.txt
I'm getting two errors: one is saying the Computer directory is invalid and the function --stdin is invalid. Can you help me?

generate different password for each usr in list

I use the following bash script to change password for users in list:
pass=`cat /dev/urandom | tr -dc 'a-zA-Z0-9-_!##$%^&*()_+{}|:<>?=' | fold -w 12 | grep -i '[!##$%^&*()_+{}|:<>?=]' | head -n 1`
for usr in `cat usrs.lst`
do
printf "Username is $usr and password is $pass\n"
done
The problem with the previous code it that doesn't change the password
meaning it gives one password for every user.
I need the previous script to give a different password for each user
so what am I missing in the previous code?
you should assign pass variable in loop as below;
#!/bin/bash
while IFS= read -r usr; do
pass=$(tr -dc 'a-zA-Z0-9-!##$%^&*()+{}|:<>?=' < /dev/urandom | fold -w 12 | grep -i '[!##$%^&*()_+{}|:<>?=]' | head -n 1)
printf 'Username is %s and password is %s\n' "$usr" "$pass"
done < usrs.lst

Email whois details on user login using mailx - whois output format not working using sed

I am using the below command in my .bash_profile to get email alerts on ssh user logins
I get the email alerts, the only problem is format of whois command output - its wrapped.
Output of whois in command line is neat. Even after using sed -r G it doesn't work.
echo -e 'ALERT - Shell Access on:' `date` `who` '\n\n' `whois $(who | cut -d'(' -f2 | cut -d')' -f1)` | sed -r G | mail -s "Alert: SSH Access from `who | cut -d'(' -f2 | cut -d')' -f1`" user#example.com
SOLUTION:
Paste the following in .bash_profile in the user/root directory to get email alerts
# Send email on user login - manually added
SSHLOGINIP=`who | cut -d'(' -f2 | cut -d')' -f1 | tail -n 1`
echo -e "ALERT - Shell Access on: `date` \n\n Active Sessions:\n `who` \n\n `whois $SSHLOGINIP`" | sed -r G | mail -s "Alert: SSH Access from $SSHLOGINIP" user#example.com
unset SSHLOGINIP
For whois to work on RHEL based systems like CentOS you would need to install jwhois
You need to double-quote the echo statement in its entirety:
echo -e "ALERT - Shell Access on:' `date` `who` '\n\n' `whois $(who | cut -d'(' -f2 | cut -d')' -f1)`"
That will prevent wrapping of the whois output before it is passed to sed -- preserving all the linebreaks. The remainder of your mailx line should be fine. One other thought. Why not just write all of the output to a tmp file following | sed -r G > tmpfile. Then just add the tmpfile as an attachment to mailx?
mail -s "Alert: SSH Access from `who | cut -d'(' -f2 | cut -d')' -f1`" -a tmpfile user#example.com

Print the name and home directory path associated with a username

I need to ask for a username and print the name and home directory path associated with that username
echo Please enter your username
read username
I am not sure how to use $username to locate information regarding its home directory and name.
awk -F: -v v="$username" '{if ($1==v) print $6}' /etc/passwd
Without awk:
cat /etc/passwd | grep "$username" | cut -d ":" -f6
User name:
cat /etc/passwd | grep "$username" | cut -d ":" -f5
echo Please enter a username
read username
cat /etc/passwd | grep "$username" | cut -d ":" -f6
cat /etc/passwd | grep "$username" | cut -d ":" -f5
use getent
read -p "username: " username
IFS=: read username password uid gid gecos homedir shell < <(getent passwd "$username")
echo "fullname=${gecos%%,*}"
echo "homedir=$homedir"

Add two variables in one line and save them in a text file

I've created the following script
#!/bin/bash
shdw=$(cat /etc/shadow | cut -d: -f1,2)
users=$(cat /etc/passwd | cut -d: -f3,4,5,6,7)
echo $users:$shdw >> bk.txt
I want it to save each user account into one line. perhaps a do while iduno
The join program is your friend:
join -t : /etc/passwd /etc/shadow | cut -d: -f3-7,1,8
cut -d: -f1-2 /etc/shadow > myshadow
cut -d: -f3-7 /etc/passwd > mypasswd
paste -d: mypasswd myshadow >> bk.txt

Resources