Shell script issue

Hello all,
I am looking for this kind of output-

This output is generated by following script. issue is somehow signal strength is not displayed. can some one help me to fix this script ? I tried to paste in code format. some how its not formatting correctly so image is pasted.

You expect people to read a screenshot instead of text ?

Hard to C&P the code and test it...

3 Likes

So, this is a grep or awk inquiry - and not OpenWrt-specific. Someone should be able to answer before possible marked as off topic (from OpenWrt). I'm not a guru with those commands.

Does the command produce the correct answer at the system prompt?

Absolutely impossible to do anything with the image. Here is how to format as code:

<triple-bacticks>
<your-code>
<triple-backticks>

Visually looks like this:

Mind the newlines.

Or copy-paste to pastebin and share a link.

I tested this just for you: https://omnifile.co/ocr/
Seems to have some issues with single quotes though. :sweat_smile: First one was worse though, didn't even recognize hashes.

replace the 8th line starting with '''SIGNAL''' with the following line

SIGNAL=$(echo $line | awk {'print $2'})
1 Like

this worked.. awesome.

Note - I tried pasting code in text (code) format, some how its getting broken. now trying triple quote and various format. Here is entire code intext format. Every thing below this line is part of shell script.

#!/bin/sh

Output directory and file (root path of the web server)

OUTPUT_FILE="/www/clients.html"
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')

Generate the HTML header

cat > $OUTPUT_FILE << EOF

Connected Devices body { font-family: Arial, sans-serif; margin: 20px; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { border: 1px solid #ddd; padding: 12px; text-align: left; } th { background-color: #f2f2f2; } .timestamp { color: #666; margin-bottom: 20px; } .unknown { color: #999; font-style: italic; } .good-signal { color: green; } .medium-signal { color: orange; } .poor-signal { color: red; }

Connected Devices

Last Updated: $TIMESTAMP
EOF

Get list of wireless interfaces

for INTERFACE in $(iwinfo | grep ESSID | cut -d' ' -f1); do
# Get clients for this interface
iwinfo $INTERFACE assoclist | while read -r line; do
if echo "$line" | grep -q "dBm"; then
# Extract MAC and signal strength
MAC=$(echo "$line" | awk '{print $1}')

        SIGNAL=$(echo $line | awk {'print $2'})
        # Get hostname and IP from DHCP leases
        DHCP_INFO=$(grep -i "$MAC" /tmp/dhcp.leases)
        if [ ! -z "$DHCP_INFO" ]; then
            IP=$(echo "$DHCP_INFO" | awk '{print $3}')
            HOSTNAME=$(echo "$DHCP_INFO" | awk '{print $4}')
            [ "$HOSTNAME" = "*" ] && HOSTNAME=$(echo "$MAC" | sed 's/://g')
        else
            IP="Unknown"
            HOSTNAME=$(echo "$MAC" | sed 's/://g')
        fi
        
        # Format signal strength with color coding
        if [ ! -z "$SIGNAL" ]; then
            if [ $SIGNAL -ge -50 ]; then
                SIGNAL_DISPLAY="<span class='good-signal'>${SIGNAL} dBm</span>"
            elif [ $SIGNAL -ge -70 ]; then
                SIGNAL_DISPLAY="<span class='medium-signal'>${SIGNAL} dBm</span>"
            else
                SIGNAL_DISPLAY="<span class='poor-signal'>${SIGNAL} dBm</span>"
            fi
        else
            SIGNAL_DISPLAY="<span class='unknown'>No Signal</span>"
        fi
        
        # Add row to the HTML table
        echo "<tr><td>$HOSTNAME</td><td>$IP</td><td>$SIGNAL_DISPLAY</td></tr>" >> $OUTPUT_FILE
    fi
done

done

Generate the HTML footer

cat >> $OUTPUT_FILE << EOF

Device Name IP Address Signal Strength

EOF

Set proper permissions

chmod 644 $OUTPUT_FILE

to bad you totally screwed up the formating in the post. would you be so kind and fix that, please? kthxbye

Not triple quote. Triple backtick.

1 Like

It would be great if you kindly post the whole final script, formatted as already asked... I'm adjusting it for my dumb AP (i.g. no /tmp/dhcp.leases) and pretty close to the goal, but having trouble with the html output formatting (not a developer here...). Thanks a lot.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.