Printable Version of Topic

Click here to view this topic in its original format

Linuxhelp _ Technical Support _ loop port variable to connect to multiple computers

Posted by: aristosv May 8 2016, 10:51 AM

I have an SSH server that is used by client computers to create reverse SSH tunnels on. Each client computer is connected on a different port, and to connect from the SSH server on the client computer, that port is used.

I have a bash script that is supposed to connect on all the client computers, get some information and create an html report. My problem is that the script only connects on the first port in the list. It does not continue to process all the clients on the rest of the ports list.

Any ideas what could I be doing wrong?

CODE
#!/bin/bash

function get_stats {
    local port=$1

    local cpu="$(ssh -o StrictHostKeyChecking=no user@localhost -p $port echo $[100-$(vmstat|tail -1|awk '{print $15}')]"%")"
    local dsk="$(ssh -o StrictHostKeyChecking=no user@localhost -p $port df -h /dev/mmcblk0p2 | awk 'NR>1{print $5}')"
    local mem="$(ssh -o StrictHostKeyChecking=no user@localhost -p $port free | grep -e-/+ | awk '{print $3/($3+$4) * 100.0 ''}' | cut -d '.' -f1)%"
    local tmp="$(ssh -o StrictHostKeyChecking=no user@localhost -p $port /opt/vc/bin/vcgencmd measure_temp | sed -e 's/temp=//')"
    local mda="$(ssh -o StrictHostKeyChecking=no user@localhost -p $port /bin/ls -R -l /user/media | /usr/bin/wc -l)"

    echo     '<tr>'
    echo         '<td>Client on port $port</td>'
    echo         "<td align="center">$cpu</td>"
    echo         "<td align="center">$dsk</td>"
    echo         "<td align="center">$mem</td>"
    echo         "<td align="center">$tmp</td>"
    echo         "<td align="center">$mda</td>"
    echo     '</tr>'
}

    echo '<html>'
    echo '<head>'
    echo '<title>Report</title>'
    echo '</head>'
    echo '<body>'
    echo '<table border="1" align="center">'
    echo     '<tr>'
    echo         '<td></td>'
    echo         '<td>CPU Usage</td>'
    echo         '<td>Disk Usage</td>'
    echo         '<td>Memory Usage</td>'
    echo         '<td>Temperature</td>'
    echo         '<td>Media Count</td>'
    echo     '</tr>'

portfile='ports'

while read port; do
    get_stats $port
done <$portfile

    echo '</table>'
    echo '</body>'
    echo '</html>'

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)