其他分享
首页 > 其他分享> > macos – 如何编写从路由器的Web界面下载Wi-Fi网络状态的AppleScript?

macos – 如何编写从路由器的Web界面下载Wi-Fi网络状态的AppleScript?

作者:互联网

目标:

我想创建一个AppleScript .scpt文件来执行以下操作:

>访问路由器的网页(即其IP地址).
>从此网页中获取当前连接到两个网络的IP地址. (通过“两个网络”,我指的是单独的2.4Ghz和5.0Ghz无线网络.)
>从网页中,获取每个连接设备的相应Dbm信号强度(即,每个连接的IP地址).

执行:

我想要一个AppleScript列表对象包含所有IP地址:

>例如:theListOfIPs包含{“192.168.0.1”,“192.168.0.”,“192.168.0.3”,“192.168.0.4”,“192.168.0.5”,“192.168.0.6.”,“192.168.0.7”} .

(我没有必要区分连接到2.4GHz网络的IP和连接到5.0GHz网络的IP.所有IP应该只包含在ListOfIPs中.)

并且,一个AppleScript列表对象包含其相应的信号强度:

>例如:theListOfTheirSignalStrengths包含{“0”,“ – 75”,“ – 40”,“0”,“0”,“ – 63”,“ – 72”}.

笔记:

>我希望所有这些都能在“幕后”完成.不引人注意是关键,因为脚本需要定期检查路由器的网站以获取网络更新.
>最终,对网络状态的更改将写入.txt日志文件,并在满足特定条件时显示警报通知.我知道如何编写这些组件的代码;我需要帮助实际将数据导入脚本.
>首选浏览器:谷歌浏览器

背景:

之前我使用过shell命令curl,以便将给定网站的未删节HTML源代码导入AppleScript,作为文本对象.我理解,遗憾的是,无法将所有JavaScript元素作为单个文本对象同样或方便地转换为AppleScript.

相反,必须通过某个标识符(如id,class,tag或name)单独获取每个JavaScript元素.这会使事情变得更复杂(因为您不能简单地解析AppleScript中的所有内容).

通过使用Chrome的Inspect功能和Chrome的JavaScript控制台Elements pane,我确定了相关的JavaScript标识符.包含所有IP地址的两个JavaScript元素ID以及它们的信号强度是wifi-24和wifi-5.

有人可以教我如何正确编写必要的JavaScript代码,然后解析生成的HTML文本,以隔离我想要的基本网络数据吗?

解决方法:

根据讨论情况,这应该处理问题的原始范围.

注意:这是示例代码,并且不包含太多(如果有)错误处理.我将把它留给你,因为这只是整个剧本的一部分,一旦你将所有其他部分组合在一起.

--  # 
--  #   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
--  #   
--  #########################################

我不得不说,一般来说,不应该使用像grep和sed这样的工具来解析HTML,但是,对于某些页面,就像在这个用例中一样,它是非常安全的.虽然,如果它破裂,它并不难修复.

标签:javascript,google-chrome,macos,text,applescript
来源: https://codeday.me/bug/20190818/1693208.html