How to write an AppleScript that downloads Wi-Fi network status from router's web interface?

Solution 1:

Based on the discussions had, this should handle the original scope of the question.

Note: This is example code and does not contain much, if any, error handling. I'll leave that to you since this is only a portion of the overall script, once you put all the other pieces together.

--  # 
--  #   Get the target information from theTargetURL in Google Chrome.
--  #   
--  #   Do this in the background, so get the 'tab id' and 'window id' of
--  #   the target URL ('theTargetURL') if it exists, and process accordingly. 

set theTargetURL to "http://192.168.1.1/0.1/gui/#/"

tell application "Google Chrome"
    if running then
        set theWindowList to every window
        repeat with thisWindow in theWindowList
            set theTabList to every tab of thisWindow
            repeat with thisTab in theTabList
                if theTargetURL is equal to (URL of thisTab as string) then

                    --  #   Get the targeted content of the web page which contains the information.
                    --  #   Note that document.getElementById(elementID) can only query one
                    --  #   element at a time, therefore call it twice, once with each elementID.

                    set rawWiFi24HTML to thisTab execute javascript "document.getElementById('wifi-24').innerHTML;"
                    set rawWiFi5HTML to thisTab execute javascript "document.getElementById('wifi-5').innerHTML;"

                    tell current application

                        --  #   Process the 'rawWiFi24HTML' and 'rawWiFi5HTML' variables.
                        --  #   Setup some temporary files to handle the processing.

                        set rawHTML to "/tmp/rawHTML.tmp"
                        set rawIP to "/tmp/rawIP.tmp"
                        set rawDBM to "/tmp/rawDBM.tmp"

                        --  #   Combine the value of  the'rawWiFi24HTML' and 'rawWiFi5HTML' variables into the 'rawHTML' temporary file.

                        do shell script "echo " & quoted form of rawWiFi24HTML & " > " & rawHTML & "; echo " & quoted form of rawWiFi5HTML & " >> " & rawHTML

                        --  # Process the 'rawHTML' into the 'rawIP' and 'rawDBM' temporary files.
                        --  # These files will contain comma delimited content of the targeted info.

                        do shell script "grep 'IP:' " & rawHTML & " | sed -e 's:</span>.*$::g' -e 's:^.*>::g' | tr '\\12' ',' > " & rawIP & "; grep 'device.parsedSignalStrength' " & rawHTML & " | sed -e 's: dBM.*$::g' -e 's:^.*\"::g' | tr '\\12' ',' > " & rawDBM

                        -- Process 'rawIP' and 'rawDBM' temporary files into 'theIPAddressList' and 'theSignalStrengthList' lists.

                        set theIPAddressList to my makeListFromCSVFile(rawIP)
                        set theSignalStrengthList to my makeListFromCSVFile(rawDBM)

                        --  #   Clean up, remove temporary files.

                        do shell script "rm /tmp/raw*.tmp"

                    end tell

                end if
            end repeat
        end repeat
    end if
end tell

--  # Handler used to create a list from a CSV file.

on makeListFromCSVFile(thisFile)
    set thisFilesContents to (read thisFile)
    set AppleScript's text item delimiters to {","}
    set thisList to items 1 thru -2 of text items of thisFilesContents as list
    set AppleScript's text item delimiters to {}
    return thisList
end makeListFromCSVFile




--  #########################################
--  #   
--  #   Checking the output of the code:
--  #   
--  #   The following commands are here just to show the contents
--  #   of the two lists displayed in a default list box, and then a way 
--  #   both lists might be combined, the output of which is logged.
--  #   
--  #   This of course can be removed after testing is finished.
--  #   
tell current application
    --  #
    choose from list theIPAddressList & theSignalStrengthList
    --  #
    repeat with i from 1 to (count of theIPAddressList)
        log "IP: " & item i of theIPAddressList & " @ " & item i of theSignalStrengthList & " Dbm"
    end repeat
    --  #
end tell
--  #   
--  #########################################

I do have to say that generally speaking, one is not supposed to parse HTML with tools like grep and sed, however, for certain pages, like in this use case, it's pretty safe to do. Although, if it breaks, it's not hard to fix.