# -*- coding: utf-8 -*-
"""
LICENSE:
AREDN_topology.py reads all the mesh status pages available on a given network
and builds a list of connections that can be pasted into https://flowchart.fun
Copyright (C) 2021 Patrick Truchon.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
VERSION:
* v2021.11.12: First working copy.
"""
import fnmatch
import urllib.request
# User Input
print('Enter the URL of your mesh status page, or simply press Enter to use')
print(' http://VA7FI-HAP-1.local.mesh/cgi-bin/mesh')
MYURL = input() or "http://VA7FI-HAP-1.local.mesh/cgi-bin/mesh"
print("Calculating time...")
# Format URL so that NODE name is in upper case:
MYSTATION = (MYURL.partition("http://")[-1].partition(".local")[0]).upper()
MYURL = "http://" + MYSTATION + ".local.mesh/cgi-bin/mesh"
# Read the content of my page
MYPAGE = urllib.request.urlopen(MYURL).readlines()
# Extract list of URLS for other stations' mesh status pages seen on my page.
URLS = []
i = 0
for i in range(len(MYPAGE)):
line = MYPAGE[i].decode('ascii', 'ignore')
if fnmatch.fnmatch(line, "*local.mesh*/'>*"):
url = (line.partition("")[0]) + \
"cgi-bin/mesh"
if url[7:10] == "VE7" or url[7:10] == "VA7": #Include BC stations only
URLS.append(url)
# Add my URL to the list
URLS.append(MYURL)
URLS.sort() # sort list of websites
# Create list of NODES from list of URLS
NODES = []
for i in range(len(URLS)):
NODE = (URLS[i].partition("http://")[-1].partition(".local")[0])
NODES.append(NODE)
print("It should take about " + str(round(len(URLS)/38, 1)) + \
" minutes to complete.")
print()
# Get NODES connected to each station given by i
i = 0
for i in range(len(URLS)):
#for i in range(8,11):
# Load mesh status page for ith station
page = urllib.request.urlopen(URLS[i]).readlines()
# Get the name of the ith station NODE from the URL
NODE = (URLS[i].partition("http://")[-1].partition(".local")[0])
print(NODE)
j = 0
k = len(page) # set this large for now
conns = []
for j in range(len(page)):
#reset the conntype to empty (it could be 'dtd', 'tun', or empty)
conntype = ""
line = page[j].decode('ascii', 'ignore') #ignore weird characters
# Connected NODES are located on lines after "Current Neibhbors"
if fnmatch.fnmatch(line, "*Current Neighbors*"):
k = j
if fnmatch.fnmatch(line, "*local.mesh*/'>*") and j > k:
if fnmatch.fnmatch(line, "*(*"):
conntype = (line.partition("(")[-1].\
partition(")")[0])+":"
conn = (line.partition("http://")[-1].partition(".local")[0])
if conn in NODES:
fconn = " "+conntype +"("+conn+")"
else:
fconn = " "+conntype +conn
print(fconn)
print()
print(str(i+1)+" NODES have been analyzed.")
print("You can now copy and paste them into: https://flowchart.fun ")