#!/bin/bash
 
function del-tmp-files() {
   if ls /tmp/wsd-*.txt 1> /dev/null 2>&1; then
      rm /tmp/wsd-*
   fi
return 0
}
 
# Delete pre-existing temporary work files.
 
del-tmp-files
 
# Get the V5 UUID of this machine
UUID=$(python3 $HOME/discover/wsd-gen-uuid.py)
 
# Send a multicast probe to all WSD capable devices and store the XML output in wsd-probe1.txt
echo
echo "Please wait.....sending multicast discovery probe and waiting 2 seconds for responses"
echo
python3 $HOME/discover/wsd-mcast-probe.py > /tmp/wsd-probe1.txt
 
# Iterate through the XML until the UUID to IPv4 mappings are obtained in wsd-probe9.txt
more /tmp/wsd-probe1.txt | grep Computer | awk -F "<wsa:Address>" '{print $2}' > /tmp/wsd-probe2.txt
 
sort -u /tmp/wsd-probe2.txt > /tmp/wsd-probe3.txt
 
more /tmp/wsd-probe3.txt | awk -F "uuid:" '{print $2}' > /tmp/wsd-probe4.txt
 
more /tmp/wsd-probe4.txt | awk -F "</wsa:Address>" '{print $1,"******",$2}' > /tmp/wsd-probe5.txt
 
more /tmp/wsd-probe5.txt | awk -F "from" '{print $1,"******",$2}' > /tmp/wsd-probe6.txt
 
more /tmp/wsd-probe6.txt | awk -F "******" '{print $1 $3}' > /tmp/wsd-probe7.txt
 
more /tmp/wsd-probe7.txt | awk -F "\\\('" '{print $1 $2}' > /tmp/wsd-probe8.txt
 
more /tmp/wsd-probe8.txt | awk -F "'" '{print $1}' > /tmp/wsd-probe9.txt
 
# Read the UUID to IPv4 mappings until end of file and send XML requests to each WSD host
 
while read RECORD; do
 
    URN=$(echo $RECORD | cut -d" " -f1)
    IPA=$(echo $RECORD | cut -d" " -f2)
 
    # Generate the HTTP/XML request file from the template
    cat $HOME/discover/wsd-template.xml | sed 's/XXXXXXXXXX/'$URN'/g' > /tmp/wsd-request.txt
    cat /tmp/wsd-request.txt | sed -i 's/YYYYYYYYYY/'$UUID'/g' /tmp/wsd-request.txt
 
    # Send the XML/SOAP request to the target machine
    curl -s -A wsd --header "Accept-Encoding: identity" --header "Connection: Close" \
    --header "Content-Type: application/soap+xml" --header "User-Agent: wsd" \
    --data @/tmp/wsd-request.txt http://$IPA:5357/$URN > /tmp/wsd-response-$IPA.txt
 
    # Extract, format and display the information returned
    echo
    echo "Device IP : $IPA"
    echo "==========================="
    echo -n "Name         :";cat /tmp/wsd-response-$IPA.txt | awk -F "FriendlyName" '{print $2}' | awk -F "<" '{print $1}' | cut -d">" -f2
    echo -n "Manufacturer :";cat /tmp/wsd-response-$IPA.txt | awk -F "Manufacturer" '{print $2}' | awk -F "<" '{print $1}' | cut -d">" -f2
    echo -n "Model        :";cat /tmp/wsd-response-$IPA.txt | awk -F "ModelName" '{print $2}' | awk -F "<" '{print $1}' | cut -d">" -f2
    echo -n "Category     :";cat /tmp/wsd-response-$IPA.txt | awk -F "DeviceCategory" '{print $2}' | awk -F "<" '{print $1}' | cut -d">" -f2
    echo -n "URN          :";cat /tmp/wsd-response-$IPA.txt | awk -F "Address" '{print $2}' | awk -F "<" '{print $1}' | cut -d">" -f2
    echo -n "Type         :";cat /tmp/wsd-response-$IPA.txt | awk -F "Types" '{print $2}' | awk -F "<" '{print $1}' | cut -d">" -f2
    echo -n "Workgroup    :";cat /tmp/wsd-response-$IPA.txt | awk -F "<pub:Computer" '{print $2}' | awk -F "<" '{print $1}' | cut -d">" -f2
    echo
 
done < /tmp/wsd-probe9.txt
 
# This next bit is just a bit of fluff to display printers. The formatting is inconsistent because every printer
# has a different web page. Printer manufacturers are listed in the file $HOME/discover/printers.txt. If the printer
# is not in this file it won't be found in the HTTP information
 
# Check whether the original multicast response contains any printer information
cat /tmp/wsd-probe1.txt | grep -q -A2 Print
 
if [[ $? -eq 0 ]]; then # A printer of some sort has been found
 
   # Get the line that contains 'Print' and the two lines after it (one of which contains the printer IP and URL)
   more /tmp/wsd-probe1.txt | grep -A2 Print > /tmp/wsd-probe10.txt
 
   # Remove any duplicate entries
   sort -u /tmp/wsd-probe10.txt > /tmp/wsd-probe11.txt
 
   # Isolate the printer IP and URL information
   cat /tmp/wsd-probe11.txt | awk -F"XAddrs>" '{print $2}' | awk -F"/wsd" '{print $1}' > /tmp/wsd-probe12.txt
 
   # Remove blank lines to clean up the file
   sed '/^$/d' /tmp/wsd-probe12.txt > /tmp/wsd-probe13.txt
 
   # Read each line of the file containing the printer URLs and contact the printers in turn
   while read RECORD; do
 
    echo "Printers"
    echo "==========================="
    URL=$RECORD
    # Try to get the printer's HTML page
        curl -s $URL/index.html > /tmp/wsd-printer.txt
    if [[ $? -ne 0 ]]; then
           echo "Couldn't get HTML info from $URL"
    else
       # Read each line of the printers.txt file and try to get the Make and Model from the HTML
           while read PRT; do
         grep -q $PRT /tmp/wsd-printer.txt
         if [[ $? -eq 0 ]]; then # Printer in the list is contained in the returned HTML
            # Extract the Make and the following word hoping it's the Model
            TYP=$(grep $PRT /tmp/wsd-printer.txt | awk -v a=$PRT '{for(i=1;i<=NF;i++) if ($i==a) print $i,$(i+1)}')
            echo "URL   : $URL"
                    echo "Make  : $TYP"
         fi
       done < $HOME/discover/printers.txt
    fi
 
   done < /tmp/wsd-probe13.txt
 
fi
 
echo
 
#
# Delete the latest temporary work files.
#
del-tmp-files
