dimanche 1 septembre 2019

Assistance needed with bash script parsing data from a file

Would first like to thank everyone for taking the time and reviewing this, and providing some assistance.

I am stuck on this bash script project I have been working on. This script is supposed to pull data from this file, export it to a csv, and then email it out. I was able to grab the required data and email it to myself but the problem is that the groups in the file have special characters. I need to have the lsgroup command executed on those groups in order to retrieve the users and then have it exported to the csv file.

For example, below is sample data that are in the file and how it looks like:

[Skyrim]
  comment = Elder Scrolls  
  path = /export/skyrim/elderscrolls 
  valid users = @dawnstar nords @riften 
  invalid users = @lakers

[SONY]
  comment = PS4
  path = /export/Sony/PS4
  valid users = @insomniac @activision 
  invalid users =  peterparker controller @pspro

The script is supposed to be gathering the name, comment, path, valid users, invalid users, and exporting them to the csv

So far this is what I have that works,

out="/tmp/parsed-report.csv"
file="/tmp/file.conf"

echo "name,comment,path,valid_users,invalid_users" > $out;

scp -q server:/tmp/parse/file.conf $out

grep "^\[.*\]$" $file |grep -Ev 'PasswordPickup|global' | while read shr ; do

    shr_regex=$(echo "$shr" | sed 's/[][]/\\&/g')
    shr_print=$(echo "$shr"|sed 's/[][]//g')

com=$(grep -p "$shr_regex"  $file|grep -v "#"| grep -w "comment"| awk -F'=' '{print $2}'|sed 's/,/ /g')
path=$(grep -p "$shr_regex" $file|grep -v "#"| grep -w "path"| awk -F'=' '{print $2}')
val=$(grep -p "$shr_regex"  $file|grep -v "#"| grep -w "valid users"| awk -F'=' '{print $2}')
inv=$(grep -p "$shr_regex"  $file|grep -v "#"| grep -w "invalid users"| awk -F'=' '{print$2}')

echo "$shr_print,$com,$path,$val,$inv" >> $out

done

exit 0

The text with '@' are considered groups so if $var3='@' then run the lsgroup command and export the data to csv file under the correct category, else if $vars3!='@' then export users to the csv file.

This is what I tried to come up with:

vars3="$val$inv"
Server="server_1"
for lists in $(echo "$vars3"); do
  if [[ $lists = *[!\@]* ]]; then
    ssh -q $Server "lsgroup -a users $(echo "$lists"|tr -d /@/)|awk - 
    F'=' '{print $1}'" > print to csv file as valid or invalid users
  else [[ $lists != *[!\@]* ]]; then
    echo "users without @" > to csv file as valid or invalid users

With the right commands the output should look like this

: skyrim
Comment: Elder Scrolls
Path:  /export/skyrim/elderscrolls  
Valid Users:  dragonborn argonian kajit nords
Invalid Users :  Shaq Kobe Phil Lebron 

: SONY
Comment: PS4
Path:  /export/Sony/PS4
Valid Users:   spiderman ratchet&clank callofduty spyro
Invalid Users :  peterparker controller 4k

Aucun commentaire:

Enregistrer un commentaire