aru
Regular Contributor
I'm currently learning how to display information on a webpage through a router, but I'm not quite sure yet how to easily link it to a menu. For now, I can only access it directly by typing the URL http://192.168.168.254/user20.asp
The map below displays the latest 100 source data entries read from /tmp/mnt/usbkey/skynet/skynet.log
By displaying the locations of IP addresses on the map, users can visually understand the geographical distribution of network activities.
The following software packages are pre-installed in cmdline:
Skynet app
Create an user20.py script in the /jffs/addons/myaddon directory:
Please paste the following script:
Please note that everyone's USB names may vary, it might be necessary to modify the path to /tmp/mnt/usbkey/skynet/skynet.log in the script. and the execution result of the addon-maps.py script will be directly saved into /tmp/var/wwwext/user20.asp. The addon provides only 20 page links, which can be checked using ls -al /www/user* to see if they are occupied.
You may need to manually adjust the following user20.py script as indicated in red according to your router environment:
user1-20.asp displays red if unoccupied and white if occupied. To modify the script to output a filename other than user20.asp if it's already taken.
Change the file permissions of user20.py to make it executable.
Then, execute the /jffs/addons/myaddon/user20.py file in cmdline. The output will be as follows:
Finally, after logging into the router, enter the user20.asp URL to view it, for example, http://192.168.168.254/user20.asp
With the above practice, I can independently add the webpage information I want to display.
Note: The ip2geotools script calls https://nominatim.openstreetmap.org/search , which is a library used for parsing geographic locations from IP addresses. I'm not certain about the free query limit of Nominatim. If you are using the free version, there may be some restrictions, such as limits on the number of IP addresses parsed per day, or there may be slower service. However, if you use the paid version, you may experience faster parsing speeds and larger quotas. If you have a large number of query requirements, please refer to the official website or documentation of https://operations.osmfoundation.org/policies/nominatim/ to get detailed information about usage restrictions.
How to mount your own webpage onto the router menu steps as below:
Edit the /tmp/menuTree.js file
Search for the following crucial keywords highlighted in red. You'll notice that all other addon menus are uniformly integrated here (as indicated in blue).
You simply need to insert your own link in a single line (as shown in green).
After editing, simply remount the menu using the following cmdline to complete the process.
Done!
How to remove this script:
Remove the main script.
Remove dependent packages, but I'm not sure if other scripts will use them. If there is any impact, please reinstall them. (Please note the removal sequence.)
Or uninstall all dependencies installed via pip and Python. Only proceed if you are absolutely certain that no other packages rely on Python and pip.
Edit the /tmp/menuTree.js file
Although it will automatically disappear after restarting the router, if you want to see the removal effect immediately.
Please delete the green part as below:
Remount the menu using the following cmdline to complete the process.
The map below displays the latest 100 source data entries read from /tmp/mnt/usbkey/skynet/skynet.log
By displaying the locations of IP addresses on the map, users can visually understand the geographical distribution of network activities.
The following software packages are pre-installed in cmdline:
Skynet app
Python app#amtm -> i -> 2
#opkg install python3
#opkg install python3-pip
#pip install folium
#pip install ip2geotools
Create an user20.py script in the /jffs/addons/myaddon directory:
#mkdir /jffs/addons/myaddon
#nano -w /jffs/addons/myaddon/user20.py
Please paste the following script:
Code:
#!/tmp/mnt/usbkey/entware/bin/python
import re
import folium
from collections import Counter
from ip2geotools.databases.noncommercial import DbIpCity
from concurrent.futures import ThreadPoolExecutor
# Read the last 100 lines of the file
print("Reading the last 100 lines of the file...")
with open("/tmp/mnt/usbkey/skynet/skynet.log", "r") as file:
last_lines = file.readlines()[-100:]
# Extract source IP addresses from the last 100 lines
source_ips = []
for line in last_lines:
match = re.search(r"SRC=([0-9.]+)", line)
if match:
source_ips.append(match.group(1))
# Count the occurrences of each source IP address
ip_counter = Counter(source_ips)
# Create a basic map
print("Creating a basic map...")
mymap = folium.Map(location=[30, 0], zoom_start=2)
# Function to get location information for an IP address
def get_location(ip):
try:
print(f"Processing IP: {ip}...")
response = DbIpCity.get(ip, api_key='free')
return response
except Exception as e:
print("Error processing IP:", ip, "-", e)
return None
# Process IP addresses using concurrent futures
print("Processing IP addresses using concurrent futures...")
with ThreadPoolExecutor() as executor:
futures = {executor.submit(get_location, ip): ip for ip in ip_counter.keys()}
for future in futures:
ip = futures[future]
response = future.result()
if response is not None and response.latitude is not None and response.longitude is not None:
count = ip_counter[ip]
radius = count * 5
color = 'red' if count > 1 else 'blue' # If count is greater than 1, color is red, else blue
tooltip_text = f"IP: {ip}, Count: {count}, City: {response.city}, Country: {response.country}, Latitude: {response.latitude}, Longitude: {response.longitude}, Region: {response.region}"
folium.CircleMarker(location=[response.latitude, response.longitude], radius=radius, color=color,
fill=True, fill_color=color, tooltip=tooltip_text).add_to(mymap)
# Save the map as an ASP file
print("Saving the map as an ASP file...")
mymap.save("/tmp/var/wwwext/user20.asp")
print("Processing complete.")
Please note that everyone's USB names may vary, it might be necessary to modify the path to /tmp/mnt/usbkey/skynet/skynet.log in the script. and the execution result of the addon-maps.py script will be directly saved into /tmp/var/wwwext/user20.asp. The addon provides only 20 page links, which can be checked using ls -al /www/user* to see if they are occupied.
You may need to manually adjust the following user20.py script as indicated in red according to your router environment:
#!/tmp/mnt/usbkey/entware/bin/python
.
with open("/tmp/mnt/usbkey/skynet/skynet.log", "r") as file:
.
mymap.save("/tmp/var/wwwext/user20.asp")
user1-20.asp displays red if unoccupied and white if occupied. To modify the script to output a filename other than user20.asp if it's already taken.
Change the file permissions of user20.py to make it executable.
#chmod 775 /jffs/addons/myaddon/user20.py
Then, execute the /jffs/addons/myaddon/user20.py file in cmdline. The output will be as follows:
Finally, after logging into the router, enter the user20.asp URL to view it, for example, http://192.168.168.254/user20.asp
With the above practice, I can independently add the webpage information I want to display.
Note: The ip2geotools script calls https://nominatim.openstreetmap.org/search , which is a library used for parsing geographic locations from IP addresses. I'm not certain about the free query limit of Nominatim. If you are using the free version, there may be some restrictions, such as limits on the number of IP addresses parsed per day, or there may be slower service. However, if you use the paid version, you may experience faster parsing speeds and larger quotas. If you have a large number of query requirements, please refer to the official website or documentation of https://operations.osmfoundation.org/policies/nominatim/ to get detailed information about usage restrictions.
Nominatim Usage Policy (aka Geocoding Policy)
operations.osmfoundation.org
How to mount your own webpage onto the router menu steps as below:
Edit the /tmp/menuTree.js file
#nano -w /tmp/menuTree.js
Search for the following crucial keywords highlighted in red. You'll notice that all other addon menus are uniformly integrated here (as indicated in blue).
{
menuName: "Addons",
index: "menu_Addons",
tab: [
{url: "user2.asp", tabName: "Unbound"},
{url: "user3.asp", tabName: "dn-vnstat"},
{url: "user4.asp", tabName: "connmon"},
{url: "user5.asp", tabName: "ntpMerlin"},
{url: "user7.asp", tabName: "scMerlin"},
{url: "javascript:var helpwindow=window.open('/ext/shared-jy/redirect.htm','_bl>
{url: "user8.asp", tabName: "Sitemap"},
{url: "NULL", tabName: "__INHERIT__"}
]
}
You simply need to insert your own link in a single line (as shown in green).
{
menuName: "Addons",
index: "menu_Addons",
tab: [
{url: "user20.asp", tabName: "Skynet-Maps"},
{url: "user2.asp", tabName: "Unbound"},
{url: "user3.asp", tabName: "dn-vnstat"},
{url: "user4.asp", tabName: "connmon"},
{url: "user5.asp", tabName: "ntpMerlin"},
{url: "user7.asp", tabName: "scMerlin"},
{url: "javascript:var helpwindow=window.open('/ext/shared-jy/redirect.htm','_bl>
{url: "user8.asp", tabName: "Sitemap"},
{url: "NULL", tabName: "__INHERIT__"}
]
}
After editing, simply remount the menu using the following cmdline to complete the process.
#umount /www/require/modules/menuTree.js && mount -o bind /tmp/menuTree.js /www/require/modules/menuTree.js
Done!
How to remove this script:
Remove the main script.
#rm /jffs/addons/myaddon/user20.py
Remove dependent packages, but I'm not sure if other scripts will use them. If there is any impact, please reinstall them. (Please note the removal sequence.)
pip uninstall ip2geotools
pip uninstall folium
opkg remove python3-pip
opkg remove python3
Or uninstall all dependencies installed via pip and Python. Only proceed if you are absolutely certain that no other packages rely on Python and pip.
pip freeze | xargs pip uninstall -y
opkg remove $(opkg list-installed | awk '/python/{print $1}') --force-removal-of-dependent-packages
Edit the /tmp/menuTree.js file
#nano -w /tmp/menuTree.js
Although it will automatically disappear after restarting the router, if you want to see the removal effect immediately.
Please delete the green part as below:
{
menuName: "Addons",
index: "menu_Addons",
tab: [
{url: "user20.asp", tabName: "Skynet-Maps"},
.
Remount the menu using the following cmdline to complete the process.
#umount /www/require/modules/menuTree.js && mount -o bind /tmp/menuTree.js /www/require/modules/menuTree.js
Last edited: