Guest User

Untitled

a guest
Apr 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.74 KB | None | 0 0
  1. function fGenTCPTAB(){
  2.         # This query pulls all the data necessary for TCPTAB
  3.         # from multiple tables. Probably don't want to touch this.
  4.         SQL="
  5.        SELECT
  6.                a.storename,
  7.                INET_NTOA(a.ip) as eth0_ip,
  8.                INET_NTOA('4294967232') as eth0_netmask,
  9.                INET_NTOA(b.ip) as eth0_gateway,
  10.                c.eth1_ip,c.eth1_netmask,c.eth1_gateway,c.additional
  11.                FROM $devicetable AS a
  12.        LEFT JOIN
  13.                (
  14.                        SELECT storename,type,ip
  15.                        FROM $devicetable
  16.                        WHERE type='Router / FRAD'  ORDER BY ip
  17.                ) AS b
  18.        ON a.storename=b.storename
  19.        LEFT JOIN
  20.                (
  21.                        SELECT
  22.                                storename,
  23.                                INET_NTOA(eth1_ip) as eth1_ip,
  24.                                INET_NTOA(eth1_netmask) as eth1_netmask,
  25.                                INET_NTOA(eth1_gateway) as eth1_gateway,
  26.                                additional
  27.                        FROM $addtable
  28.                ) AS c
  29.        ON a.storename=c.storename
  30.        WHERE a.type='RxServer'
  31.        GROUP BY a.storename
  32.        "
  33.         fRunQuery
  34.  
  35.         # Change IFS to NL
  36.         IFS="
  37.        "
  38.  
  39.         # For each line in the SQL result, split it apart.
  40.         for entry in $(echo "$RESULT")
  41.         do
  42.                 # Temporarily change IFS to default, and then read the line
  43.                 # into an array. Then restore IFS.
  44.                 OLDIFS=$IFS
  45.                 IFS="   "
  46.                 arr=( $entry )
  47.                 IFS=$OLDIFS
  48.                 # Get the number of columns in the array.
  49.                 colnums=${#arr[@]}
  50.  
  51.                 # Split up the data into readable variables.
  52.                 storename=${arr[0]}
  53.                 eth0_ip=${arr[1]}
  54.                 eth0_netmask=${arr[2]}
  55.                 eth0_gateway=${arr[3]}
  56.                 eth1_ip=${arr[4]}
  57.                 eth1_netmask=${arr[5]}
  58.                 eth1_gateway=${arr[6]}
  59.  
  60.                 # If we have more than 7 columns (remember, 0 counts)
  61.                 # and we have at least 10, this is extra routing information.
  62.                 # This will all be appended to a one variable for the
  63.                 # Extra field in TCPTAB.
  64.                 extra=''
  65.                 if [[ $colnums -ge 10 ]]
  66.                 then
  67.                         for (( i=7; i<${colnums}; i++));
  68.                         do
  69.                                 extra="$extra ${arr[$i]}"
  70.                         done
  71.                 fi
  72.  
  73.                 # The PHP code serverside requires that each of these fields
  74.                 # have either a string value or NULL (So we can accurately count them)
  75.                 # Change any NULL values to blank.
  76.  
  77.                 # NOTE: At least 3 words must exist for the $extra column to populate.
  78.                 [[ $eth1_ip == "NULL" ]] && eth1_ip=''
  79.                 [[ $eth1_netmask == "NULL" ]] && eth1_netmask=''
  80.                 [[ $eth1_gateway == "NULL" ]] && eth1_gateway=''
  81.                 [[ $eth0_ip == "NULL" ]] && eth1_ip=''
  82.                 [[ $eth0_netmask == "NULL" ]] && eth1_netmask=''
  83.                 [[ $eth0_gateway == "NULL" ]] && eth1_gateway=''
  84.                 [[ $extra =~ NULL ]] && extra=''
  85.  
  86.                 # Now setup printf to output the records.
  87.                 printf "%-15s %-15s %-15s %-15s | " $storename $eth0_ip $eth0_netmask $eth0_gateway >> $TCPTAB
  88.                 printf "%-15s %-15s %-15s | " $eth1_ip $eth1_netmask $eth1_gateway >> $TCPTAB
  89.                 printf "%-4s %-15s %-15s " $extra >> $TCPTAB
  90.                 printf "\n" >> $TCPTAB
  91.         done
  92. } #end function
Add Comment
Please, Sign In to add comment