Skip to content

Commit

Permalink
Added LANcount and backupFolderConfig
Browse files Browse the repository at this point in the history
Added additional LANcount function to provide a hopefully more efficient and also more compatible version.

Also added a configuration entry in fritzBoxShellConfig for the folder for storing FritzBox backups.
  • Loading branch information
jhubig committed Dec 21, 2024
1 parent 561ded7 commit f31c18c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
86 changes: 83 additions & 3 deletions fritzBoxShell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ while [[ $# -gt 0 ]]; do
-F|--outputfilter)
OutputFilter="$2"
shift ; shift
;;
--backupConfFolder)
backupConfFolder="$2"
shift ; shift
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
Expand Down Expand Up @@ -531,6 +535,80 @@ WLANGUESTstatistics() {
fi
}

### ----------------------------------------------------------------------------------------------------- ###
### -------------------------------- FUNCTION LANcount - TR-064 Protocol -------------------------------- ###
### ----------------------------------------------------------------------------------------------------- ###


LANcount() {
# TR-064 service information
SERVICE="urn:dslforum-org:service:Hosts:1"
CONTROL_URL="/upnp/control/hosts"

total_hosts=$(curl -s -k -m 5 --anyauth -u "$BoxUSER:$BoxPW" "http://$BoxIP:49000$CONTROL_URL" \
-H 'Content-Type: text/xml; charset="utf-8"' \
-H "SoapAction:$SERVICE#GetHostNumberOfEntries" \
-d "<?xml version='1.0' encoding='utf-8'?>
<s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>
<s:Body>
<u:GetHostNumberOfEntries xmlns:u='$SERVICE'></u:GetHostNumberOfEntries>
</s:Body>
</s:Envelope>" | grep NewHostNumberOfEntries | awk -F">" '{print $2}' | awk -F"<" '{print $1}')
# echo "Total number of hosts: $total_hosts"

# 2. Loop through devices and filter for Ethernet
# Count Ethernet devices
ethernet_count=0

# Maximal parallel processes
max_parallel=10
pids=() # Array for process IDs

# Loop through all hosts and query them in parallel
for ((i=0; i<total_hosts; i++)); do
# Query the interface for each device
(
interface_type=$(curl -s -k -m 5 --anyauth -u "$BoxUSER:$BoxPW" "http://$BoxIP:49000$CONTROL_URL" \
-H "Content-Type: text/xml; charset=\"utf-8\"" \
-H "SoapAction:$SERVICE#GetGenericHostEntry" \
-d "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<s:Envelope s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">
<s:Body>
<u:GetGenericHostEntry xmlns:u=\"$SERVICE\"><NewIndex>$i</NewIndex></u:GetGenericHostEntry>
</s:Body>
</s:Envelope>" | grep NewInterfaceType | awk -F">" '{print $2}' | awk -F"<" '{print $1}')

if [[ "$interface_type" == "Ethernet" ]]; then
# Count Ethernet connections
echo 1 >> /tmp/ethernet_count.tmp
fi
) &

# Store process IDs
pids+=($!)

# If the number of background processes reaches the limit, we wait for the first process
if (( ${#pids[@]} >= max_parallel )); then
# Wait for one of the running processes
wait "${pids[0]}"
# Remove the first process from the array
pids=("${pids[@]:1}")
fi
done

wait

# Sum of Ethernet connections
ethernet_count=$(wc -l < /tmp/ethernet_count.tmp)

# Print result
echo "Number of Ethernet connections: $ethernet_count"

# Delete temporary file
rm /tmp/ethernet_count.tmp

}

### ----------------------------------------------------------------------------------------------------- ###
### -------------------------------- FUNCTION LANstate - TR-064 Protocol -------------------------------- ###
### ----------------------------------------------------------------------------------------------------- ###
Expand Down Expand Up @@ -982,9 +1060,9 @@ confBackup() {
# File Downlaod
dt=$(date '+%Y%m%d_%H%M%S');

$(curl -s -k "$curlOutput1" -o "${dt}_SicherungEinstellungen.export" --anyauth -u "$BoxUSER:$BoxPW")
if [ -e "${dt}_SicherungEinstellungen.export" ]; then
echo "File successfully downloaded: ${dt}_SicherungEinstellungen.export"
$(curl -s -k "$curlOutput1" -o "$backupConfFolder${dt}_SicherungEinstellungen.export" --anyauth -u "$BoxUSER:$BoxPW")
if [ -e "${backupConfFolder}${dt}_SicherungEinstellungen.export" ]; then
echo "File successfully downloaded: ${backupConfFolder}${dt}_SicherungEinstellungen.export"
fi

}
Expand Down Expand Up @@ -1040,6 +1118,7 @@ DisplayArguments() {
echo "| | totalConnectionsLAN | Number of total connected LAN clients (incl. full Mesh) |"
echo "|-----------------|---------------------------|-----------------------------------------------------------------------------|"
echo "| LAN | STATE | Statistics for the LAN easily digestible by telegraf |"
echo "| LAN | COUNT | Total number of connected devices through ethernet |"
echo "| DSL | STATE | Statistics for the DSL easily digestible by telegraf |"
echo "| WAN | STATE | Statistics for the WAN easily digestible by telegraf |"
echo "| WAN | RECONNECT | Ask for a new IP Address from your provider |"
Expand Down Expand Up @@ -1111,6 +1190,7 @@ else
fi
elif [ "$option1" = "LAN" ]; then
if [ "$option2" = "STATE" ]; then LANstate "$option2";
elif [ "$option2" = "COUNT" ]; then LANcount "$option2";
else DisplayArguments
fi
elif [ "$option1" = "DSL" ]; then
Expand Down
3 changes: 3 additions & 0 deletions fritzBoxShellConfig.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
[[ -z "$RepeaterIP" ]] && RepeaterIP="fritz.repeater"
[[ -z "$RepeaterUSER" ]] && RepeaterUSER="" #Usually on Fritz!Repeater no User is existing. Can be left empty.
[[ -z "$RepeaterPW" ]] && RepeaterPW="YourPassword"

# In case you are using the Fritz!Box Backup export this path is used to download the backup - kepp empty if you want to use the same directory than the script
[[ -z "$backupConfFolder" ]] && backupConfFolder="" # Dont forget the "/" at the end!!!

0 comments on commit f31c18c

Please sign in to comment.