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

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

Related

How to create username from txt file

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

Hide output of cat command

I have this line of code which I would like to hide its output.
Vrs=$(cat $(echo $line | awk -F"-" '{print "/var/AS-"$2"-"toupper($3)"-"$4}') | grep "YES" | cut -d":" -f5)
I have tried to include &> /dev/null at the end of the line but it doesn't work.
Does anyone know how to do this?
I am not exactly sure what you are trying to achieve, but your cat call looks redundant to me.
Vrs=$(echo "$line" | awk -F"-" '{print "/var/AS-"$2"-"toupper($3)"-"$4}' | grep "YES" | cut -d":" -f5)
You could rephrase the statement to
Vrs=$(echo "$line" | awk -F"-" '{print "/var/AS-"$2"-"toupper($3)"-"$4}' | grep "YES" | cut -d":" -f5)
This does the same thing. In the command is successful, you would get the result stored in Vrs. No output would be shown in the stdout. However, if you expect errors, you could do :
Vrs=$(echo "$line" | awk -F"-" '{print "/var/AS-"$2"-"toupper($3)"-"$4}' | grep "YES" | cut -d":" -f5 2>/dev/null)
This will suppress the errors and give you an empty $Vrs
Notes:
I have double quoted $line to prevent globbing and word splitting.

Bash script any reason why it wont write the file

I am helping debug some code that exec the following script is there any reason why its not writing a file to the server? - if that what it does. All the $ data and permissions are ok:
Script:
#!/bin/bash
RANGE=$1
ALLOCATION=`echo $RANGE | cut -f1,2,3 -d'.'`
/sbin/ip rule add from $1 lookup $2
echo $ALLOCATION
rm /path/too/file/location/$ALLOCATION
for i in `seq 3 254`
do
echo $ALLOCATION.$i >> /path/too/file/location/$ALLOCATION
done
ETH=`/sbin/ifconfig | grep eth0 | tail -n1 | cut -f2 -d':' | cut -f1 -d' '`

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

Generate User List From AV output File In Bash

I want to generate list of users having malware in their public html
I am using avgscan for scanning,
/opt/avg/av/bin/avgscan -a -c --ignerrors --report=avoutput.txt
but it generates report like
/home/someuser/mail/info/cur/1395054106.H396740P84180,S=47470:2,S:/form_ident.rar Trojan horse Inject2.WPP
/home/someuser/public_html/__swift/files/attach_pq2ar348en1z435o5jhqy37de2xfb391 Trojan horse Zbot.BMI
I tried some thing but it didnt workout as list also have /backup folder which I don't want to be counted in list
I just need list of users, how to do?
#!/bin/bash
in="your_av_report file.txt" # in this case avoutput.txt
a=$(cat $in | grep -i "/mail/" | grep -v "/backup/" | cut -d'/' -f3 | awk '!a[$0]++' | uniq)
b=$cat $in | grep -i "/public_html/" | grep -v "/backup/" | cut -d'/' -f3 | awk '!a[$0]++' | uniq)
echo $a >>foo.txt
echo $b >>foo.txt
I hope it Helps :)

Resources