Thursday, May 19, 2011

Dell printer warranty gathering information bash script for linux/unix

We have a LOT of different printer. A large number of them (over 60 and counting) are Dell branded printers. This includes laser printer models like the:
Dell Color Laser 3110cn
Dell Laser Printer 5210n
Dell Laser Printer M5200
Dell 5330dn Laser Printer
Dell Laser Printer 1720dn
Dell Laser Printer 5310n
Dell 2335dn MFP
Dell 2355dn Laser MFP
Dell 1815dn MFP
Dell 1600n MFP
Dell 3335dn MFP

I had a need to make sure to get all newer printers under warranty. Most of the units above have a web interface and most of the time the Service Tag is viewable from that interface. However, I'm very lazy even when it comes to clicking through all of the different types of printer web interfaces... then needing to go to Dell's support web site to manually check each Service Tag...

Naturally, I had to hack some shell script to do all of the dirty work for me. You will need snmpwalk, links, w3m and some added web fluff in the for of sortable columns and line highlighting via Sortable Table 2 java script. The script below will provide info on all printers found, just not warranty or Service Tag for non-Dell printers. The other good part is that you can tell from the "sysDescr.0" what firmware a printer is running also.

I don't remember why I have both links and w3m since this script was created a while back. I just thought it would be nice to post it now. Naturally, you could change out all of the html fluff and dump straight to csv for easy import into whatever you want.

If you have suggestions on code cleanup, I would love to see them. I think I have all the internal company specific stuff taken out and still usable. Enjoy. Sorry the formatting looks *UGLY*, but blogger freaks on the "span" greps in the code and this is the only way I have figured out how to show the code here. I need to verify that the stuff coming out of blogger is still working...

#!/bin/bash

WEBPINFO="/var/www/html/SOME/DIR/Printer_Info.html"
JSPATH="http://`hostname`/SOME/DIR"
PINFO="$WEBPINFO.`date +%y%m%d`"

cat > "$WEBPINFO" << EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Printer Warranty and firmware Information - sortable</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="$JSPATH/sortedTable.js"></script>
<link type="text/css" href="$JSPATH/sortedTable.css" rel="stylesheet"/>
<link type="text/css" href="$JSPATH/nav.css" rel="stylesheet" />
<style>
        .red{color:crimson;}
        #nav{font-size:0.82em;}
</style>
</head>
<body>
<div class="tableWrapper">
<table id="table_1">
<thead>
<tr class="headerLine"><th class="sorttable_alpha">Hostname</th> <th class="sorttable_alpha">sysDescr.0</th> <th class="sorttable_mmdd">warranty expire</th> <th class="sorttable_alpha">Service Tag</th> <th class="sorttable_alpha">Printer name</th></tr>
</thead>
<tbody>
EOF

#get all network based print servers and exclude if needed
for PRINT_SERVER in `lpstat -t | egrep 'lpd://|socket://' |egrep -v 'SOME_PRINTER|SOME_OTHER_PRINTER'| cut -d"/" -f3 | cut -d":" -f1 | sort -u`
do
 unset SER ST PRTS http_proxy
 PRTS=`lpstat -t|grep $PRINT_SERVER[/:]|awk '{ print $3 }'|tr -s ':\n' ' '`
 ping -c 2 $PRINT_SERVER >& /dev/null
 if [ $? -eq 0 ]; then
   MOD=`snmpwalk -Os -c public -v 1 $PRINT_SERVER sysDescr.0  | cut -d":" -f2 `
   echo $MOD | grep -qi dell
   if [ "$?" -eq 0 ]; then
      #2335dn and JD24 and 1815dn are the same ST, but hrDeviceDescr.1 provides firmware version.
      if [ "`echo $MOD | egrep -qi '5330dn|2335dn|2355dn|JD24';echo $?`" -eq 0 ]; then
        ST="`links -dump http://$PRINT_SERVER/printer_info.htm|grep Service|tr -s ' '|cut -f6 -d ' '`"
      elif [ "`echo $MOD | egrep -qi '1815dn';echo $?`" -eq 0 ]; then
        MOD="$MOD `links -dump http://$PRINT_SERVER/printer_info.htm|grep 'Printer Firmware Version:'|cut -f2 -d ':'|tr -s ' '`"
        ST="`links -dump http://$PRINT_SERVER/printer_info.htm|grep Service|tr -s ' '|cut -f6 -d ' '`"
      elif [ "`echo $MOD | egrep -qi '3335dn';echo $?`" -eq 0 ]; then
        ST="`links -dump "http://$PRINT_SERVER/cgi-bin/dynamic/printer/config/reports/deviceinfo.html" |egrep 'Service'|tr -s ' '|cut -f6 -d ' '`"
      elif [ "`echo $MOD | egrep -qi '1720dn';echo $?`" -eq 0 ]; then
        ST="`links -dump "http://$PRINT_SERVER/cgi-bin/dynamic/information.html?path=/printer/info" |egrep 'Service'|tr -s ' '|cut -f6 -d ' '`"
      elif [ "`echo $MOD | egrep -qi '5210n|5310n';echo $?`" -eq 0 ]; then
        ST="`links  -dump "http://$PRINT_SERVER/cgi-bin/dynamic/config/reports/deviceinfo.html" |grep Service|tr -s ' '|cut -f6 -d ' '`"
      elif [ "`echo $MOD | egrep -qi 'M5200';echo $?`" -eq 0 ]; then
        ST="`links -dump http://$PRINT_SERVER/printer/info |egrep 'Service'|tr -s ' '|tr -d '|'|cut -f6 -d ' '`"
      elif [ "`echo $MOD | egrep -qi '3110cn';echo $?`" -eq 0 ]; then
        ST="`links -dump http://$PRINT_SERVER/ews/status/infomation.htm |egrep 'Service'|tr -s ' '|tr -d '|'|cut -f7 -d ' '`"
      fi
      #need a proxy? Squid is a great one!
      #export http_proxy=http://USERNAME:PASSWORD@PROXY_SERVER:PORT
      LINK="http://support.dell.com/support/topics/global.aspx/support/my_systems_info/details?c=us&l=en&s=pub&servicetag=$ST"
      SER="`w3m -no-cookie -dump "$LINK" |grep -A 6 Days|cut -c50-|grep "/"|awk '{ print $3 }'|sort |tail -1`"
    fi
    echo -e "<tr><td><a href="http://${PRINT_SERVER}">${PRINT_SERVER}</a></td> <td>${MOD}</td> <td>${SER}</td> <td><a href="$LINK">${ST}</a></td> <td>${PRTS}</td></tr>" >> $WEBPINFO
  else
   echo -e "<tr><td>${PRINT_SERVER}</td> <td>No Response</td> <td></td> <td></td> <td>${PRTS}</td></tr>" >> $WEBPINFO
 fi
done

cat >> "$WEBPINFO" << EOF
</tbody>
</table>

<p>
Total print servers = `lpstat -t | egrep 'lpd://|socket://' |egrep -v 'SOME_PRINTER|SOME_OTHER_PRINTER'| cut -d"/" -f3 | cut -d":" -f1 | sort -u|wc -l`
Created by $0 at `date` on `hostname`
<script>
        var config = {
                tableId:'table_1', // the only mandatory parameter
                rowOver:'row_over',
                sortMap:'1,2,3:desc'
        }
        var t2 = new Chth.sortTable();
        t2.init(config);
</script>
</body>
</html>
EOF

#keep a historical copy
cp -f $WEBPINFO $PINFO

No comments:

Post a Comment