1

I have a script in which I am reading from a csv file where it contains SourceIp, DestinationIP,Source Ports, Destination Ports.

First I am reading the sourceIp and trying to do ssh over it (I am able to do it successfully), here I am trying to get a pseudo terminal and want to execute a for loop that will iterate over the sourcePorts(hyphen separated) and destinationPorts.

Input File Content :

10.X.X.9,10.X.X.23,8140-61613,1521-1524-1525-1526-1530-1531-8140-61613

Script :

export lastSourceIP=""
export lastDestinationIP=""
export fqdn=""
export sourceFqdn=""
x=0
export username="sjain";
export location="/home/sjain/poc";
export baseLocation="10.X.X.9"
while IFS="," read f1 f2 f3 f4
do
        x=$(($x+1))
        TMP=$(mktemp)
        TMP2=$(mktemp)
        echo "Source IP        : $f1"       
        echo "Destination IP   : $f2"
        echo "Source Ports             : $f3"
        echo "Destination Ports             : $f4"
export sourceIP=$f1
export destIP=$(echo "$f2" | tr -d '\n')
export port=$(echo "$f3" | tr -d '\n')
export destinationPorts=$(echo "$f4" | tr -d '\n')
ssh -t -t $username@$sourceIP 'bash -s' <<ENDSSH
(IFS='-'; for sourceP in $port; do 
 (for destinationP in $destinationPorts; do
    echo "$sourceP" - "$destinationP"
 done;) 
done;)
exit 
ENDSSH

done < ipaddress.csv

But when I am executing this script , it is not printing sourceIP - DestinationIp values. Here , is the output that I am getting on console.

Output:

    Source IP        : 10.X.X.9
    Destination IP   : 10.X.X.23
    Source Ports             : 8140-61613
    Destination Ports             : 1521-1524-1525-1526-1530-1531-8140-61613
    tcgetattr: Inappropriate ioctl for device
    (IFS='-'; for sourceP in 8140-61613; do
     (for destinationP in 1521-1524-1525-1526-1530-1531-8140-61613; do
        echo "" - ""
     done;)
    done;)
    exit
    [[email protected] ~]$ (IFS='-'; for sourceP in 8140-61613; do
    >  (for destinationP in 1521-1524-1525-1526-1530-1531-8140-61613; do
    >     echo "" - ""
    >  done;)
    > done;)
     -
    [[email protected] ~]$ exit
    exit
    Connection to 10.X.X.9 closed

.

Expected Output :

8140-1521
8140-1524
.... and so on

Please help where I am doing wrong.

1 Answer 1

5

You need to ensure the loop vars are derefenced on the remote host:

ssh -t -t $username@$sourceIP 'bash -s' <<ENDSSH
 IFS='-'
 for sourceP in $port; do 
  for destinationP in $destinationPorts; do
    echo "\$sourceP" - "\$destinationP"
  done 
 done
ENDSSH

Using all the parentheses launches a subshell for each loop iteration, which is a lot of extra work you don't need.

You don't need to export all your variables, only the ones that need to be in the environment for child processes.

This destIP=$(echo "$f2" | tr -d '\n') is no different from destIP=$f2 -- assuming there are no "internal" newlines. $(command substitution) removes trailing newlines.

5
  • thanks glenn, i removed the parentheses and now it is printing the value , but it seems like that loop has not been executed properly (executed only once).It considered "8140-61613" and "1521-1524-1525-1526-1530-1531-8140-61613" as one string , I mean it didn't tokenize it using "-". This is output i got 8140-61613 - 1521-1524-1525-1526-1530-1531-8140-61613. But I am expecting output in the format as I mentioned in the qns. Commented Mar 24, 2014 at 17:53
  • It seems like IFS is not overriding the value to "-". Commented Mar 24, 2014 at 18:01
  • Yes, that's true, because you're setting IFS on the remote host but substituting the variable locally. Do this: remove the IFS line and use ${port//-/ } (same for the other var) Commented Mar 24, 2014 at 18:41
  • i tried this , but this time it removed the hyphen and collapsed all the values into one string like this. 814061613 - 152115241525152615301531814061613 Commented Mar 24, 2014 at 20:13
  • 1
    make sure there's a space between the last slash and the close brace. Commented Mar 24, 2014 at 20:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.