Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix Shell Scripting - Add Extra Column In While Loop

Status
Not open for further replies.

gaka1108

Technical User
Feb 1, 2016
5
GB
I have a file server.txt containing different hostnames like:

Code:
hostname1.com
hostname2.com

My shell script servers.sh is written to get the /etc/passwd and /etc/group files from the list in servers.txt file.

I wish to add the hostname from which the entries came from in my final output file. My script looks something like below:

Code:
while read HOST ;
        do
           sshpass -p $password ssh -n $username@$HOST 'cat /etc/passwd'>>users.txt
           sshpass -p $password ssh -n $username@$HOST 'cat /etc/group'>>groups.txt
done < servers.txt
echo -e "UserName;UID;GID;HomeDir;Shell" > final_users.csv
cut -d: -f1,3,4,6,7 users.txt  | tr ':' ';'>> final_users.csv
echo -e "GroupName;GID;Members" > final_groups.csv
awk -F '' '{for(i=4;i<=NF;i++)print$1";"$3";"$i}' groups.txt >> final_groups.csv

The goal is to add another column in both
Code:
final_users.csv
and
Code:
final_groups.csv
like
Code:
hostname
so I can know which servers each entry came from.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top