Kimi
Kimi

Reputation: 324

How to add the number of active netstat connections of different machine?

I've written this code to get the connections from one machine and adding them with the number of connection of the other machine.

This code is not giving any netstat, 0 is coming for the live active connections.

#!/usr/bin/ksh -xvf
Machine_Detail="prpm@sp204|LC1_R11_LCP|LC1_R12_LCP|LC1_FR15_LCP|LC1_R16_LCP         prpm1@sp2048|LC1_R13_LCP|LC1_R14_LCP|LC1_R17_LCP|LC1_R18_LCP"

for i in $Machine_Detail
    do
        machine_connect=$(echo $i | cut -d'|' -f1)
        echo $machine_connect

        ssh $machine_connect
        Conn_count=**$(netstat -an | grep $`echo ${i} | cut -d'|' -f2`| wc -l | sed 's/ //g')**
        Conn_count=$((${Conn_count}+$(netstat -an | grep $`echo ${i} | cut -d'|' -f3` | wc -l | sed 's/ //g')))
        Conn_count=$((${Conn_count}+$(netstat -an | grep $`echo ${i} | cut -d'|' -f4` | wc -l | sed 's/ //g')))
        Conn_count=$((${Conn_count}+$(netstat -an | grep $`echo ${i} | cut -d'|' -f5` | wc -l | sed 's/ //g')))
        Total_Conn_Count=$((${Total_Conn_Count}+${Conn_count}))
        echo $Total_Conn_Count

        exit
    done

Upvotes: 1

Views: 351

Answers (2)

Joice
Joice

Reputation: 1

The following piece of code is working correctly.

#!/usr/bin/ksh -xvf 
    Machine_Detail="prpm@sp204|LC1_R11_LCP|LC1_R12_LCP|LC1_FR15_LCP|LC1_R16_LCP         prpm1@sp2048|LC1_R13_LCP|LC1_R14_LCP|LC1_R17_LCP|LC1_R18_LCP" 
    for i in $Machine_Detail
    do
    machine_connect=$(echo $i | cut -d'|' -f1)
    echo $machine_connect

    Conn_count=$(ssh -f -T $machine_connect "netstat -an | grep "$(echo $(($(echo $i | cut -d'|' -f2))))" | wc -l | sed 's/ //g'")
    Conn_count=$((${Conn_count}+$(ssh -f -T $machine_connect "netstat -an | grep "$(echo $(($(echo $i | cut -d'|' -f3))))" | wc -l | sed 's/ //g'")))
    Conn_count=$((${Conn_count}+$(ssh -f -T $machine_connect "netstat -an | grep "$(echo $(($(echo $i | cut -d'|' -f4))))" | wc -l | sed 's/ //g'")))
    Conn_count=$((${Conn_count}+$(ssh -f -T $machine_connect "netstat -an | grep "$(echo $(($(echo $i | cut -d'|' -f5))))" | wc -l | sed 's/ //g'")))

    Total_Conn_Count=$((${Total_Conn_Count}+${Conn_count}))
    echo $Total_Conn_Count

    done

Upvotes: 0

Dennis Williamson
Dennis Williamson

Reputation: 360555

You need to send the netstat command as an argument of the ssh command so it gets executed on the target machine. You can use egrep to take advantage of the pipe characters to get the count in one step. I'm not sure if you really need sed to delete any spaces.

#!/usr/bin/ksh -xvf
Machine_Detail="prpm@sp204|LC1_R11_LCP|LC1_R12_LCP|LC1_FR15_LCP|LC1_R16_LCP         prpm1@sp2048|LC1_R13_LCP|LC1_R14_LCP|LC1_R17_LCP|LC1_R18_LCP"
for i in $Machine_Detail
do
    saveIFS=$IFS
    IFS='|'
    fields=($i)
    machine_connect=${fields[0]}
    keys="${fields[*]:1}"
    IFS=$saveIFS
    echo $machine_connect
    Conn_count=$(ssh $machine_connect "netstat -an | egrep $keys | wc -l")
    ((Total_Conn_Count += Conn_Count))
done
echo $Total_Conn_Count

Upvotes: 1

Related Questions