Breathe new life into your old home router
Viewing Router Data
Next, we want to create a simple Common Gateway Interface (CGI) Ash web page to view custom router data and pass the data to another device, such as a Raspberry Pi Node-RED server (Figure 7).
For this project, it's best to use dynamic data. If you don't have external data, then dynamic data from the router's system load data can be used (Figure 8). The command-line statement
cat /proc/loadavg
will show the router's one-, five-, and 15-minute load averages.
By adding some AWK code, you can extract each of the data points directly. For example, to get the first data value, enter the following code:
## Show router load averages cat /proc/loadavg 0.36 0.28 0.16 1/50 3307 ## get the first data point cat /proc/loadavg | awk '{print $1}' 0.36
Custom Router Web Page
OpenWRT runs the uHTTPd web server for its router configuration. This web server can also be used for custom CGI web pages that can use Ash scripting. You will find the OpenWRT custom CGI pages in the /www/cgi-bin
directory. Listing 4 shows an example CGI page, test.cgi
. This example shows the previous load average values along with some system summary information from the Linux monitoring tool vmstat
.
Listing 4
Router Web CGI Example
01 #!/bin/ash 02 # 03 # test.cgi - show system load averages and vmstat 04 # 05 06 echo "Content-type: text/html" 07 echo "" 08 echo "<!DOCTYPE html> 09 <html> 10 <head><title>Router Points</title> 11 </head> 12 <body> 13 <h1>Router CGI Page</h1><hr>" 14 15 # -- show router system load averages -- 16 echo "<h2> System Load Averages </h2>" 17 echo "1 minute load:" 18 cat /proc/loadavg |awk '{print $1}' 19 echo "<br>5 minute load:" 20 cat /proc/loadavg |awk '{print $2}' 21 echo "<br>15 minute load:" 22 cat /proc/loadavg |awk '{print $3}' 23 24 # -- show vmstat -- use <pre> formatting 25 echo "<h2> Vmstat Results </h2> <pre>" 26 vmstat 27 echo "</pre></body></html>"
CGI web pages use Ash/Bash echo
statements to output HTML code. It is important to start the page by echo
-ing out "Content-type: text/html"
with an added new line (lines 6-7). For this example, including HTML heading tags such as <h2>
in the echo
string improves the presentation (lines 16 and 25).
The output from Ash/Bash statements such as
cat /proc/loadavg |awk '{print $1}'
will be shown directly on the web page.
Using the HTML <pre>
tag provides a pre-formatted fixed format for the output from the vmstat
monitoring utility (lines 25-27).
After creating the CGI page, the final step involves setting the file's execution rights as follows:
chmod +x test.cgi
You can now access your custom web page at http://router_ip/cgi-bin/test.cgi. Figure 9 shows the test CGI web page.
Connecting to a Raspberry Pi
For our final project, we want to pass the data from the router to a Raspberry Pi. You could modify this project to pass data to a Home Assistant node or any PC on your home network.
The simplest protocol for passing data is to use TCP sockets. This approach doesn't require any added software to be loaded on either the router or on the Rasp Pi.
You can use the Bash nc
(or netcat) to both send and receive TCP messages. To create an nc
test, open two terminal windows: one on the router and the other on the Raspberry Pi (Figure 10).
To set up a listener on the Rasp Pi, define the Rasp Pi's IP address with a port (1234 in this example). The -l
option sets listening mode, and the -k
option will keep the connection open for multiple messages.
On the router sender side, an echo
message is piped to the nc
command with the Rasp Pi's IP address and port.
Next, you need to periodically send dynamic data out via TCP. Listing 5 shows an Ash script file that uses our earlier Ash/AWK code to get the router's load averages and then pipes the values to a TCP listener every two seconds.
Listing 5
Sending Router Data to a TCP Socket
#!/bin/ash # send_loadavg.sh - Send Router Load Averages via TCP # - send 1 min avg to port 1111 # - send 5 min avg to port 1115 while true do cat /proc/loadavg | awk '{printf $1}'| nc 192.168.1.11 1111 cat /proc/loadavg | awk '{printf $2}'| nc 192.168.1.11 1115 sleep 2 done
Node-RED [5] is a great visual programming environment that comes preinstalled on the Raspberry Pi Raspbian image. To get TCP messages from the script in Listing 5 into Node-RED, two tcp in
nodes can be configured with the required port numbers. To show the data graphically, two dashboard ui_gauge
nodes can be connected to the outputs of the tcp in
nodes. Figure 11 shows the Node-RED logic and web dashboard for the two router load average points.
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.