views:

75

answers:

2

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
+1  A: 

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
Dennis Williamson
The IFS='|' is not able to be parsed:Run Results : machine_connect=prpm@sp204|LC1_R11_LCP|LC1_R12_LCP|LC1_FR15_LCP|LC1_R16_LCP+ acb.sh[10]: keys="${fields[*]:1}": bad substitution
Kimi
fields=($i) is also not wrorking field=$i is workingwhy so ?
Kimi
What version of ksh?
Dennis Williamson
Version M-11/16/88i
Kimi
A: 

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
Joice