Hello,
The code below pulls printer information from a three-column file (set by the ${MULTIFILE} variable at the end), that has the printer queue name, IP, and type of printer. It is supposed to use this info to add the printers in the list to one, or more servers . The input file (${MULTIFILE} variable) looks like this:
queuename1 10.10.10.1 hp
queuename2 10.10.10.2 xerox
queuename3 192.168.1.3 hp
This is also the info that feeds the following:
while read QNAME PRTIP PTYPE
The problem I am running into is that it only executes on the first row of the input file, and doesn't go down the input file at all, and I believe this is due to the ${HNAME} variable, which is for the server. How would I make it so that I can have the list of printers installed on multiple servers?
Here is the code:
echo "Do you want to install these printers to the standard list of servers?"
echo "THIS COULD TAKE A WHILE!!"
print -n "Enter 'y' or 'n':"
read CHOICE
case "$CHOICE" in
y|yes|Yes) HNAME=`cat servers.txt`
continue ;;
n|no|No) echo
echo "Enter server host names separated by a blank space. ex.(server1 server2 server3):"
read HNAME
continue ;;
*) add_multiple_printer_func
exit ;;
while read QNAME PRTIP PTYPE
do
for i in $HNAME
do
if [ "$PTYPE" = "hp" -o "$PTYPE" = "Hp" -o "$PTYPE" = "HP" ] ; then
PTYPE=257
elif [ "$PTYPE" = "xerox" -o "$PTYPE" = "Xerox" -o "$PTYPE" = "XEROX" ] ; then
PTYPE=9991
elif [ "$PTYPE" = "unknown" -o "$PTYPE" = "Unknown" -o "$PTYPE" = "UNKNOWN" ] ; then
PTYPE=9991
fi
if [ "$HNAME" = localhost ]
then
echo "Canceling print jobs for $QNAME on $HNAME"
for j in `lpstat -o | grep -w $QNAME" | awk '{print $1}'`
do
echo ${j}
cancel ${j}
done
/opt/hpnp/bin/removequeue -q$QNAME
echo
echo "/opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP"
echo
/opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP ; lpstat -p | grep $QNAME
# echo "Sending test print to printer"
# print "Printer succssfully installed on $HNAME" | lp -d $QNAME
else
echo $i
echo "--------------------------------------------------------"
echo "Canceling print jobs for $QNAME on $i"
for j in `ssh ${i} "lpstat -o | grep -w $QNAME" | awk '{print $1}'`
do
echo ${j}
echo "ssh ${i} cancel ${j}"
ssh ${i} cancel ${j}
done
fi
done
ssh $i "lpshut ; sleep 02 ; /opt/hpnp/bin/removequeue -q$QNAME ; sleep 02"
echo
echo "ssh $i /opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP"
echo
ssh $i "/opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP ; lpsched ; lpstat -p | grep $QNAME"
done < ${MULTIFILE} 2> tlg_printer.log 1>&2
sleep 04
Here is the output:
--------------------------------------------------------
Canceling print jobs for queuename1 on server1
lpshut: scheduler not running
queuename1 does not exist
ssh server1 /opt/hpnp/bin/addqueue -i 257 -q queuename1 -h 10.10.10.1
queuename1 added to spooler.
scheduler is running
printer queuename1 is idle. enabled since Apr 8 01:18
After this output, it ends, and doesn't do the other printers.
I've had this before and it is a problem with nested while; do done loops.
A simple test would be to run:
while read QNAME PRTIP PTYPE; do
echo QNAME = ${QNAME}
echo PRTIP = ${PRTIP}
echo PTYPE = ${PTYPE}
done < ${MULTIFILE}
Then but the next for or while loop in and so on to prove if the nesting of one of the loops is a loop too far!
Here is a trace of the script running:
root@server> sh -x tlg_printer.sh 2>&1
+ clear
+ main_menu_func
/home/user/scripts
Wed Apr 8 14:05:02 CDT 2009
This script adds to, and deletes printers from HP-UX servers using Jetdirect.
There are also options to delete a queue, or queues only.
Printer Script Menu
1. Add A Single Printer To One Or More Servers.
(Deletes the queue first if it already exists.)
2. Delete A Single Printer From One Or More Servers.
3. Add Multiple Printers To One Or More Servers.
(Deletes the queue first if it already exists.)
4. Delete Multiple Printers From One Or More Servers.
5.Exit
V 1.6 4/7/2009
Enter your selection:3
You will need a file of printers to read from that has three columns formatted as follows:
For example:
QueueName IPaddress PrinterType(hp,xerox,unknown)
queue03 192.168.1.3 hp
The print queue name can only be 12 characters!!!
Please verify your list!!!
Do you have this file?
Enter 'y' or 'n':y
Enter the path to the input file: lists/Printer_List.txt
Do you want to install these printers to the standard list of servers?
THIS COULD TAKE A WHILE!!
Enter 'y' or 'n':n
Enter server host names separated by a blank space. ex.(server1 server2 server3):
server1
The script is running, and the menu will return when complete.
Please execute a 'tail -f' on the log file tlg_printer.log
in a separate window to see the output and error messages
scrolling, or view the logfile after the script finishes.
/home/user/scripts
Wed Apr 8 14:08:55 CDT 2009
This script adds to, and deletes printers from HP-UX servers using Jetdirect.
There are also options to delete a queue, or queues only.
Printer Script Menu
1. Add A Single Printer To One Or More Servers.
(Deletes the queue first if it already exists.)
2. Delete A Single Printer From One Or More Servers.
3. Add Multiple Printers To One Or More Servers.
(Deletes the queue first if it already exists.)
4. Delete Multiple Printers From One Or More Servers.
5.Exit
V 1.6 4/7/2009
Enter your selection:5
Here is the logfile output:
--------------------------------------------------------
Canceling print jobs for queuename1 on server1
lpshut: scheduler stopped
queuename1 does not exist
ssh server1 /opt/hpnp/bin/addqueue -i 257 -q queuename1 -h 192.168.1.3
queuename1 added to spooler.
scheduler is running
printer queuename1 is idle. enabled since Apr 8 14:09
Ok, the script works now. The solution was to use one of the following solutions suggested by Dennis Handly, of the HP ITRC forums:
The first thing you could do is to throw everything away in your while loop and just print $QNAME $PRTIP $PTYPE.
This should show you can read the whole file. (TonyFullerMalv's suggestion)
The next thing you should do is gradually add back the contents of loop.
You'll come to the conclusion that you have been stabbed in the back by the remsh/ssh habit of eating up stdin.
You'll need to use "remsh hostname -n" or "ssh hostname -n". Or you can redirect stdin to /dev/null:
ssh ... < /dev/null
Or you could possibly use a hammer and stop this in its tracks by this design pattern:
while read X Y Z; do
(
exec < /dev/null
...
)
done < file
I chose to use the last suggestion. Here is how it was used in my script:
echo "Do you want to install these printers to the standard list of servers?"
echo "THIS COULD TAKE A WHILE!!"
print -n "Enter 'y' or 'n':"
read CHOICE
case "$CHOICE" in
y|yes|Yes) HNAME=`cat servers.txt`
continue ;;
n|no|No) echo
echo "Enter server host names separated by a blank space. ex.(server1 server2 server3):"
read HNAME
continue ;;
*) add_multiple_printer_func
exit ;;
while read QNAME PRTIP PTYPE ; do
(
exec < /dev/null
for i in $HNAME
do
if [ "$PTYPE" = "hp" -o "$PTYPE" = "Hp" -o "$PTYPE" = "HP" ] ; then
PTYPE=257
elif [ "$PTYPE" = "xerox" -o "$PTYPE" = "Xerox" -o "$PTYPE" = "XEROX" ] ; then
PTYPE=9991
elif [ "$PTYPE" = "unknown" -o "$PTYPE" = "Unknown" -o "$PTYPE" = "UNKNOWN" ] ; then
PTYPE=9991
fi
if [ "$HNAME" = localhost ]
then
echo "Canceling print jobs for $QNAME on $HNAME"
for j in `lpstat -o | grep -w $QNAME" | awk '{print $1}'`
do
echo ${j}
cancel ${j}
done
/opt/hpnp/bin/removequeue -q$QNAME
echo
echo "/opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP"
echo
/opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP ; lpstat -p | grep $QNAME
# echo "Sending test print to printer"
# print "Printer succssfully installed on $HNAME" | lp -d $QNAME
else
echo $i
echo "--------------------------------------------------------"
echo "Canceling print jobs for $QNAME on $i"
for j in `ssh ${i} "lpstat -o | grep -w $QNAME" | awk '{print $1}'`
do
echo ${j}
echo "ssh ${i} cancel ${j}"
ssh ${i} cancel ${j}
done
fi
done
ssh $i "lpshut ; sleep 02 ; /opt/hpnp/bin/removequeue -q$QNAME ; sleep 02"
echo
echo "ssh $i /opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP"
echo
ssh $i "/opt/hpnp/bin/addqueue -i $PTYPE -q $QNAME -h $PRTIP ; lpsched ; lpstat -p | grep $QNAME"
echo
)
done < ${MULTIFILE} 2> tlg_printer.log 1>&2
sleep 04
Here is the output:
--------------------------------------------------------
Canceling print jobs for queuename1 on server1
lpshut: scheduler stopped
queuename1 does not exist
ssh server1 /opt/hpnp/bin/addqueue -i 257 -q queuename1 -h 192.168.1.3
queuename1 added to spooler.
scheduler is running
printer queuename1 is idle. enabled since Apr 10 09:09
--------------------------------------------------------
Canceling print jobs for queuename2 on server1
lpshut: scheduler stopped
queuename2 does not exist
ssh server1 /opt/hpnp/bin/addqueue -i 257 -q queuename2 -h 192.168.1.4
queuename2 added to spooler.
scheduler is running
printer queuename2 is idle. enabled since Apr 10 09:09
--------------------------------------------------------
Canceling print jobs for queuename3 on server1
lpshut: scheduler stopped
queuename3 does not exist
ssh server1 /opt/hpnp/bin/addqueue -i 257 -q queuename3 -h 192.168.1.5
queuename3 added to spooler.
scheduler is running
printer queuename3 is idle. enabled since Apr 10 09:10