Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Question [SOLVED] Scripting Help: Using File1 as list of objects to search for in File2

    Hello all,

    I need help with a project I am working on. The goal is to auto-update a webpage that contains links to network devices. These devices are all set to DHCP, and their hostnames are not guaranteed to stay the same, so static IPs or accessing via hostname is NOT an option.


    Run a script every hour or so to query devices on the local network to generate a list of responding devices by MAC address and what their current IP is (results.lst). Using a "static" MAC address list file "printers.db" as the database search "results.lst" to find specific MAC addresses. When a match is found, grab the IP from "results.lst" and update "index.html" with the new IP address. No comparison needs to be done between "results.lst" IP and "index.html" IP as I just want to overwrite whatever is there.

    Here is what I have so far (using dummy data):
    • Product Mac Address
    • Printer1 00:00:00:00:00:11
    • Printer2 00:00:00:00:11:11
    • Printer3 00:00:00:11:11:11
    • Printer4 00:00:11:11:11:11
    • Printer5 00:11:11:11:11:11
    • Printer6 11:11:11:11:11:11

    # Quick Links Updater v2.0    |
    # By Rob M.                   |
    #  with help from Tim F.      |
    # Clear previous runs
    if [ -f results.lst ]
    rm results.lst
    #Get network data and format to remove extraneous data
    nmap -sP | grep -iv -e "Raw" -e "is down" -e "Nmap" -e "Completed" -e "initiating" -e "Scanning" -e "NSE" | sed -e 's/) is.*//' -e 's/ is.*//' -e 's/[ ^I]*$//' -e '/^$/ d' > results.lst
    # concatonate lines and remove more extraneous data
    sed '
    /([A-Z]/ {
    # append a line
    # search for 192.168 on the second line
            /\n.*192.168./ {
    # found it - now edit making one line
    }' results.lst > results.tmp
    # remove final bit of extraneous data and delete temp file
    cat results.tmp | grep -iv -e "Host " -e "(Company)" | sed -e 's/MAC Address: //' > results.lst
    rm results.tmp
    # add end catch to file for loop
    echo "end" >> results.lst
    • 00:00:00:00:00:00
    • 00:00:00:00:00:11
    • 00:00:00:00:11:11
    • 00:00:00:11:11:11
    • 00:00:11:11:11:11
    • 00:11:11:11:11:11
    • 11:11:11:11:11:11
    • 11:00:00:00:00:00
    • 11:11:00:00:00:00
    • 11:11:11:00:00:00
    • 11:11:11:11:00:00
    • 11:11:11:11:11:00
    • 11:00:11:00:11:00
    • 00:11:00:11:00:11
    • 11:00:00:00:00:11
    • 00:00:11:11:00:00

    I've tried using awk, grep, sed, cut and while read, until, for, if... However I cannot seem to get it to:

    1) Read the first line from the "printers.db" to pull the MAC address from the list
    2) Search each line of "results.lst" until it finds a match and pull the IP address for that match
    3) Pull the product name from "printers.db"
    4) Search "index.html" for the product name
    5) Replace the IP address in "index.html" with the IP address pulled from "results.lst"

    Any help would be greatly appreciated.

    **EDIT: Corrected filename typos

  2. #2
    1.Using DHCP does'nt mean that adress will change. You can simple add host record in your dhcpd.conf file for those printer, so they always get the same IP. That's very easy to do.
    e.g. :
    range dynamic-bootp;
    host printer1 {
    hardware ethernet 00:00:00:00:00:01;

    2.If you use only dynamic IP, you can use 'on commit' statement in dhcpd.conf file to automaticaly update your DNS server each time a lease is done. Then you can use name resolution to access the device.
    e.g. : Dynamic DNS + DHCP.

  3. #3
    Unfortunately I do not have access to the DHCP server, as it is managed by an outside company, otherwise I would have already set up reservations.

  4. $spacer_open
  5. #4

    Update: Getting Closer

    Alright, I've got the script working to the point that it will search the nmap results using the product database as reference, pull the MAC and product name into two separate arrays, then pulling the IP address into another variable.

    Only step left is to modify the index.html to update all of the links for the products on the page. I've played around with sed, got it to work once (before the original post problem) and like a noob didn't save what I had and can't for the life of me remember what I coded.

    What I need is this:

    Using either sed, awk, or grep, locate a line in my index.html file based on the link text, then change the ip address on that line with the discovered ip address and save the file.

    What I have is this:

    grep -i ">${nameArray[$item]}<" index.html | sed 's/192.168.10.[0-9]/'$ipAddy'/' > index.html
    nameArray is the array with the product names
    product names show up in the html as >3600<
    ipAddy is the new ip address associated with the current entry in nameArray array.

  6. #5
    Alright, I figured it out. Here is the final results (in case anyone else needs to use a database file to search the results of an nmap scan and update IP addresses in a web page, lol)

    Here's the final results (Dummy data of course):

    00:58:BE:4B:14:87 N-12345
    00:58:BE:90:2C:58 N-13579
    00:58:BE:96:07:80 L-24680
    00:58:BE:9A:B0:49 L-02468
    00:58:BE:9E:B0:A6 S-78952
    00:58:BE:A2:40:81 S-85476
    00:58:BE:A9:D7:9D W-01121
    00:58:BE:A0:A1:0B W-13232
    00:58:BE:AB:40:2B H-25343
    00:58:BE:AC:10:7B H-37454
    00:58:BE:AD:C8:AA T-49565
    00:58:BE:B1:FF:E9 T-52676
    00:58:BE:B7:B6:85 K-64787
    00:58:BE:B9:09:78 K-76898
    00:58:BE:BC:3F:D5 A-88909
    00:58:BE:C2:01:4E A-90010
    00:58:BE:C2:10:B6 O-00130
    00:58:BE:C2:1D:0C O-18249
    00:58:BE:C3:58:3C Q-26358
    00:58:BE:CC:58:7E Q-34467
    00:58:BE:CE:32:0D R-42576
    00:58:BE:DB:02:0F R-59685
    00:58:F0:A0:58:00 E-67794
    00:45:52:CC:FB:04 E-75803
    06:38:32:81:63:B6 V-83542
    06:38:32:88:31:D5 V-91871
    06:38:32:88:90:09 X-05234
    06:38:32:9F:D9:66 X-17748
    # ------------------------
    # |   Overwatch v2.0    |
    # |    By R. Marquardt  |
    # ------------------------
    # make declarations
    declare -a macArray
    declare -a nameArray
    # get network data and format to remove extraneous data
    nmap -sP | grep -iv -e "Host" -e "Raw" -e "is down" -e "Nmap" -e "Completed" -e "initiating" -e "Scanning" -e "NSE" | sed -e 's/) is.*//' -e 's/ is.*//' -e 's/[ ^I]*$//' -e '/^$/ d' > results.lst
    # since nmap places MAC and IP on two lines we need to concatonate lines
    sed '/[^)]$/ {N;s/\n//;}' results.lst > results.tmp
    # remove final bit of extraneous data
    cat results.tmp | sed -e 's/Host.*.192.168.10/''192.168.10/' -e 's/MAC Address: / /' -e 's/ (.*//' > results.lst
    # read entries from products.db into macArray and nameArray
    let count=0
    while read line ; do
      macArray[$count]=$(echo $line | cut -d" " -f1)
      nameArray[$count]=$(echo $line | cut -d" " -f2)
    done < products.db
    # search results.lst for entries matching our array and pull out the IP address associated with the entry then update the index.html of the webpage
    for item in ${!macArray[*]}
        ipAddy=$(grep -i "${macArray[$item]}" results.lst | cut -d" " -f1)
        sed -i '/>'"${nameArray[$item]}"'</ s/192.168.10.[0-9]*\//'"$ipAddy"'\//' /var/www/ql/index.html
    # clean up
    if [ -f results.lst ]; then
        rm results.*
    I'd post an example of the line in the web page file that is getting changed so people could follow the logic, but apparently this site thinks the line I tried to copy in which had an 'h' and a couple 't' entries with a 'p' on the end was a link and I am not allowed to post URLs to other sites until after I have made 15 posts or more...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts