I have a task to add all the network printers found on a local LAN segment on our Linux thin clients. I think I have some of the fundamentals down on how to find them via an snmp discovery, and I now some of the basics on how to add a cups printer (not in the code snippit), but the thing I'm stuck on is how to get the information split up into usable chunks.
Here is the basic code for the discovery:
Code:
#!/bin/bash
tfile=/tmp/snmp.log
[ -f $tfile ] && rm $tfile
/usr/lib/cups/backend/snmp 2>&1 | tee $tfile
while read line
do
#echo "line - ${line}"
typ=$(echo $line | awk '{ print $1 }')
socket=$(echo $line | awk '{ print $2 }')
if [ "$typ" = "network" ]
then
echo "Adding network printer $socket"
else
echo "skipping: $line"
fi
done < $tfile
The resulting code will come back with a temp file like this:
network socket://192.168.24.12 "Canon iR5070 35.05" "Canon iR5070 35.05" "" "description x"
network socket://192.168.24.13 "Canon iR3570 35.06" "Canon iR3570 35.06" "" "description y"
network socket://192.168.24.15 "Canon iR5070 37.01" "Canon iR5070 37.01" "" ""
network socket://192.168.24.16 "Canon iR5070 37.01" "Canon iR5070 37.01" "" "Location x"
So here is my question. How to do I split each line into the usable chucks. I'm thinking a regular expression, but to be honest, sometimes those blow my mind on how to set them up. I'm thinking I (for example) in line:
network socket://192.168.24.12 "Canon iR5070 35.05" "Canon iR5070 35.05" "" "description x"
...I would need to remove the "network socket://192.168.24.12" so I'm just left with:
"Canon iR5070 35.05" "Canon iR5070 35.05" "" "description x"
...then split each field into variables so I can use them in the lpadmin command later:
var1="Canon iR5070 35.05"
var2="Canon iR5070 35.05"
var3=""
var4="description x"
Anyone know how I would accomplish this?