I am trying to create a script that will use the /etc/passwd file to create a listing like this:
Full Name: Blah Blah User Account: bblah User ID: 5000 Last logged in: Sun Feb 21 18:13
Full Name: Mike Mike User Account: mmike User ID: 5001 Last logged in: Sun Feb 21 18:13
With my script, I get it to show
Full Name: Blah Blah User Account: bblah User ID: 1000
because I grep the 1000 accounts in my passwd file. As soon as I do the 5000 series, I get all the full names, then all the user accounts, and then all the user ids listed. How do I go about fixing this? How do I go about adding the "Last logged in" bit?
Is there a way to do this without using awk?
Script so far is:
#!/bin/bash
passfile=/etc/passwd
for i in $(grep 5000 ${passfile} | cut -d : -f 5)
do
account=$(grep 5000 /etc/passwd | cut -d : -f 3)
username=$(grep 5000 /etc/passwd | cut -d : -f 1)
echo Full Name:${i} Username: ${username} User ID: ${account} Last login: ${lastlogin)
done
Last logged in is done by last command:
last username
Use this:
#!/bin/bash
echo -en "BEGIN{\n}{\n system(var)\n}\n" > h.awk
while read in; \
do \
uname=`echo $in | sed -r 's/:/\ /g' | awk '{print $1}'`; \
p1=`echo $in | sed -r 's/:/\ /g' | awk '{print "name: "$5, "username: "$1, "id: "$3}'`; \
p2=`echo | awk -f ./h.awk var="last $uname| head -n 1"|awk '{print $5 $6}'`; \
echo $p1 last login: $p2; \
done < /etc/passwd
If you need more detail of last login to be prompted, use this one:
echo -en "BEGIN{\n}{\n system(var)\n}\n" > h.awk
while read in; \
do \
uname=`echo $in | sed -r 's/:/\ /g' | awk '{print $1}'`; \
p1=`echo $in | sed -r 's/:/\ /g' | awk '{print "name: "$5, "username: "$1, "id: "$3}'`; \
p2=`echo | awk -f ./h.awk var="last $uname| head -n 1"|awk '{print $4" "$5 $6" "$7}'`; \
echo $p1 last login: $p2; \
done < /etc/passwd
Related
I am trying to create a shell script where it should prompt and read the values as input and then update the same shell script with that value in a specific line and column.
Or if i can use another shell script to get the values and update original file.
i wanted to read and update below parameters values as input
USER=''user''
PASS=''password''
URL=''http://10.xxx.xxx.xxx:9206/MGR/status''
PORT1=''9204''
PORT2=''9206''
Full script
#!/bin/bash
USER=''user''
PASS=''password''
URL=''http://10.xxx.xxx.xxx:9206/MGR/status''
PORT1=''9204''
PORT2=''9206''
echo Metrics for $PORT1
echo =====================
maxthreads=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT1}.* | awk '{ print $4 }'`
echo Max Threads: $maxthreads
currentthreadcount=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT1}.* | awk '{ print $8 }'`
echo Current Threads: $currentthreadcount
busythreadcount=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT1}.* | awk '{gsub("<br>", "");print $12}'`
echo Busy Threads: $busythreadcount
maxprocesstime=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT1}.* | awk '{ print $16 }'`
echo Max Processing Time: $maxprocesstime
processtime=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT1}.* | awk '{ print $20 }'`
echo Processing Time: $processtime
requestcount=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT1}.* | awk '{ print $24 }'`
echo Request Count: $requestcount
errorcount=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT1}.* | awk '{ print $27 }'`
echo Error Count: $errorcount
echo Metrics for $PORT2
echo =====================
maxthreads=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT2}.* | awk '{ print $4 }'`
echo Max Threads: $maxthreads
currentthreadcount=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT2}.* | awk '{ print $8 }'`
echo Current Threads: $currentthreadcount
busythreadcount=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT2}.* | awk '{gsub("<br>", "");print $12}'`
echo Busy Threads: $busythreadcount
maxprocesstime=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT2}.* | awk '{ print $16 }'`
echo Max Processing Time: $maxprocesstime
processtime=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT2}.* | awk '{ print $20 }'`
echo Processing Time: $processtime
requestcount=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT2}.* | awk '{ print $24 }'`
echo Request Count: $requestcount
errorcount=`curl -s -u ${USER}:${PASS} ${URL} | grep -o ${PORT2}.* | awk '{ print $27 }'`
echo Error Count: $errorcount
My intension is not to edit the file and input values, instead read it as prompt and update the file in background.
Thanks Team. I did another method for achieving this using AWS secret manager. The code can read the data from secret manager. So our engineer can update the same in secret manager as a key value pair. Thank you all for the suggestion and help. I will further look into the option suggested to improve the code.
I have a users.txt file with the following content:
[builders.ca]
UniqueID=DB#LqlFP
Login=buildca
Pass=5nFvZLwx
RelativePath=1
[DeluxeDoors.ca]
UniqueID=RgOkvU4Z
Login=DeluxDSM
Pass=L9pP3iaK
RelativePath=1
[Sonicwall.com]
UniqueID=JVpFoXad
Login=firewall
Pass=azasadsa
RelativePath=1
I wrote a script to replace all the passwords with random passwords in the file.
The script is:
users=($(cat users.txt | grep 'Login=' | cut -c 7-))
for user in "${users[#]}"; do
pass=$(cat users.txt | grep -A 2 $user | grep 'Pass' | cut -c 6-)
new_pass=$(cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-zA-Z0-9' | head -c 8)
echo $pass;
echo $new_pass;
#perl -pi -e 's/$pass/$new_pass/g' users.txt
sed -i '' 's/"${pass}"/"${new_pass}"/g' users.txt
done
But it is not updating the passwords in the users.txt.
Any help would be highly appreciated.
sed -i '' 's/'"${pass}/${new_pass}"'/g' users.txt
^ ^
These seem to be missing, so the sed was getting ${pass} and ${new_pass} as literal strings, not the expanded variables.
I'm trying to automate the user creation(Bulk Users) in Linux. So, I've created a text file with all the needed parameters for user's creation.
The Text file contains the below content:
tom:tom1:588:0:test user1:/home/test:/bin/false
harry:harry1:589:0:test test2:/hom/test2:/bin/nologin
Trying to use awk command to print the column's in for loop and create the users with the information's from /tmp/user.txt
for userdetails in $(cat /tmp/user.txt)
do
user=`echo $userdetails | awk -v FS=: '{print $1}'`
passwd=`echo $userdetails | awk -v FS=: '{print $2}'`
uid=`echo $userdetails | awk -v FS=: '{print $3}'`
gid=`echo $userdetails | awk -v FS=: '{print $4}'`
comment=`echo $userdetails | awk -v FS=: '{print $5}'`
home_dir=`echo $userdetails | awk -v FS=: '{print $6}'`
user_shell=`echo $userdetails | awk -v FS=: '{print $7}'`
useradd -d "$home_dir" -c "$comment" -s "$user_shell" -u "$uid" -g "$gid" "$user"
echo "$passwd" | passwd "$user" --stdin;
done
Actual Output:
useradd: invalid home directory ''
passwd: Unknown user name 'tom'.
useradd: invalid home directory ''
passwd: Unknown user name 'user1'.
useradd: invalid home directory ''
passwd: Unknown user name 'harry'.
useradd: invalid home directory ''
passwd: Unknown user name 'test2'.
What I'm doing wrong ?
P.S : I am aware there is a command called newusers in Linux, but i need to check the same via script to create bulk users in Linux.
idk how you got started on that track but you're off-base. Just use a shell loop:
while IFS=':' read -r user passwd uid gid comment home_dir user_shell; do
useradd -d "$home_dir" -c "$comment" -s "$user_shell" -u "$uid" -g "$gid" "$user"
echo "$passwd" | passwd "$user" --stdin;
done < /tmp/user.txt
The above is just showing how to read the file contents into variables, it assumes you know what you're doing with the "useradd" and "passwd" lines.
I would use while for read a file by line so the entire line will be stored in the variable you choose( in my case: line) . Then you could echo variable and awk will do the rest of the work. I prefer $() than `` so I use both for now. For this much data you could create a loop into the while to make code sorter and better manageable.
cat /tmp/user.txt | while read line
do
user=`echo $line | awk -F ":" '{print $1}'`
passwd=$(echo $line | awk -F ":" '{print $2}')
uid=$(echo $line | awk -F ":" '{print $3}')
gid=$(echo $line | awk -F ":" '{print $4}')
comment=$(echo $line | awk -F ":" '{print $5}')
home_dir=$(echo $line | awk -F ":" '{print $6}')
user_shell=$(echo $line | awk -F ":" '{print $7}')
useradd -d "$home_dir" -c "$comment" -s "$user_shell" -u "$uid" -g "$gid" "$user"
echo "$passwd" | passwd "$user" --stdin;
done
If you are stuck with awk, you can use Awk Global substitution:
awk '{gsub(/:/,"-")} 1' your_file
This is also feasible using sed with the g flag ( Sed Global substitution ):
sed "s/:/-/g" your_file
In both casesn your edited lines would be printed to the screen.
This is a code that shows my all user names.
-q user | grep -A 0 -B 2 -e uid:\ 5'[0-9][0-9]' | grep ^name | cut -d " " -f2-
For example, the output is like...
usernameone
hello
whoami
Then, I hope that I want to check a length of all user names.
Like this output...
11 //usernameone
5 //hello
6 //whoami
How can I get a length of pipeline code?
Given some command cmd that produces the list of users, you can do this pretty easily with xargs:
$ cat x
usernameone
hello
whoami
$ cat x | xargs -L 1 sh -c 'printf "%s //%s\n" "$(echo -n "$1" | wc -c)" "$1"' '{}'
11 //usernameone
5 //hello
6 //whoami
To get a piped command might not be possible, so here's a one liner that uses a split and a while loop to accomplish this:
-q user | grep -A 0 -B 2 -e uid:\ 5'[0-9][0-9]' | grep ^name | cut -d " " -f2-|tr " " "\n"|while read user; do echo $(echo $user|wc -c) '//'$user;done|tr "\n" " ";echo
This should give you an output in the desired format. I used user as a file hence the cat
i=0;for token in $(cat user); do echo -n "${#token} //$token";echo;i=$((i+1));done;echo;
I have a homework assignment: "for each unique user, report which group they are a member of and when they last logged in"
So far I have:
#!/bin/sh
echo "Your initial login:"
who | cut -d' ' -f1 | sort | uniq
echo "Now is logged:"
whoami
echo "Group ID:"
id -G $whoami
case $1 in
"-l") last -Fn 10 | tr -s " " ;;
*) last -Fn 10 | tr -s " " | egrep -v '(^reboot)|(^$)|(^wtmp a)|(^ftp)' | cut -d" " -f1,5,7 | sort -uM | uniq -c
esac
My question is: how I can show the each unique user? The script above only show the more recent user logged in the system, but I need all unique users.
Can anyone help?
Script using last
#!/bin/bash
while read user; do
echo "User '$user':"
echo -e "\t Last login: $(last -1R "$user" | awk 'NR==1{if($0 ~ /^$/){print "Never"}else{$1=$2="";print}}')"
echo -e "\t Groups: $(getent group | awk -F: -v user="$user" '$0 ~ user{a[i++]=$1} END{for(item in a)printf("%s ", a[item])}')"
done < <(getent passwd | awk -F: '{print $1}')
Output
Names have been changed to protect the innocent
User 'foo':
Last login: Oct 19 15:07:19 -0700 2010
Groups: foo groupA groupB
User 'bar':
Last login: Nov 16 11:40:23 -0800 2008
Groups: bar groupA groupC groupD
User 'baz':
Last login: Never
Groups: baz groupA groupD
The following link has some alternate ways of listing users. They involve reading /etc/passwd.
http://www.linuxquestions.org/linux/answers/Networking/How_to_list_all_your_USERs
There seems to be no other way than reading /etc/passwd, for example:
$ for u in `cat /etc/passwd | cut -d: -f1`; do echo $u `id -Gn $u`; done
To loop through any unique user you could get the content of the passwd file and get the first token of each line.
I'd suggest using getent passwd to read passwd, since /etc/passwd only contains users from local machine files (eg: no users from LDAP or other PAM plugins).
getent passwd | cut -d':' -f1
This command will return one user per line.
Then last and id will tell you their last login and their group:
for user in `getent passwd | cut -d':' -f1`
do
id ...
last ...
done