简体   繁体   中英

Bash output using awk or sed possibly

I have a file that looks like the below.

INPUT FILE SAMPLE

$ more showportlist
1/1
1/2
1/3
1/4
2/1
2/2
2/4
3/1
3/2
3/3
3/4
4/1
4/2
4/3

And I have the script below

#!/bin/bash

cat ports.csv | awk -F: '{print $1}' | sort -u > showportlist
#echo "$var2" > BNGIP
echo "proc_sendcommands()" > showportlist2
echo "{" >> showportlist2
echo 'rt_host=$1' >> showportlist2
echo 'rt_username=$USERNAME' >> showportlist2
echo 'rt_password=$PASSWORD' >> showportlist2
cat showportlist | while read LINE; do
    echo "rt_command_$((i++))='show circuit $LINE summary | grep bound'" >> showportlist2
done

The script outputs a file that looks like this

proc_sendcommands()
{
    rt_host=$1
    rt_username=$USERNAME
    rt_password=$PASSWORD
    rt_command_0='show circuit 1/1 summary | grep bound'
    rt_command_1='show circuit 1/2 summary | grep bound'
    rt_command_2='show circuit 1/3 summary | grep bound'
    rt_command_3='show circuit 1/4 summary | grep bound'
    rt_command_4='show circuit 2/1 summary | grep bound'
    rt_command_5='show circuit 2/2 summary | grep bound'
    rt_command_6='show circuit 2/4 summary | grep bound'
    rt_command_7='show circuit 3/1 summary | grep bound'
    rt_command_8='show circuit 3/2 summary | grep bound'
    rt_command_9='show circuit 3/3 summary | grep bound'
    rt_command_10='show circuit 3/4 summary | grep bound'
    rt_command_11='show circuit 4/1 summary | grep bound'
    rt_command_12='show circuit 4/2 summary | grep bound'
    rt_command_13='show circuit 4/3 summary | grep bound'
    rt_command_14='show circuit 4/4 summary | grep bound'
    rt_command_15='show circuit 5/1 summary | grep bound'
    rt_command_16='show circuit 5/2 summary | grep bound'
    rt_command_17='show circuit 5/3 summary | grep bound'
    rt_command_18='show circuit 5/4 summary | grep bound'
    rt_command_19='show circuit 6/1 summary | grep bound'
    rt_command_20='show circuit 6/2 summary | grep bound'
    rt_command_21='show circuit 6/3 summary | grep bound'
    rt_command_22='show circuit 6/4 summary | grep bound'
    rt_command_23='show circuit 9/1 summary | grep bound'
    rt_command_24='show circuit 9/2 summary | grep bound'

But I also need it to output the below I am just not sure how to get there.

(sleep 1; echo $rt_username; sleep 1; echo $rt_password; sleep 1;echo $rt_command_0;sleep 1;echo $rt_command_1;sle

ep 10;echo $rt_command_2;sleep 1;echo $rt_command_3;sleep 1; echo $rt_command_4;sleep 1; echo $rt_command_5;sleep

10; echo $rt_command_6;sleep 1; echo $rt_command_7;sleep 1;echo $rt_command_8;sleep 1;echo $rt_command_9;sleep

5;echo $rt_command_10;sleep 1;echo $rt_command_11;sleep 1;echo $rt_command_12;sleep 1;echo $rt_command_13;sleep 1;

echo $rt_command_14;sleep 1;echo $rt_command_15;sleep 1;echo $rt_command_16;sleep 1; echo $rt_command_17;sleep 1; echo $rt_command_18;sleep 1; echo $rt_command_19;sleep 1; echo $rt_command_20;sleep 1; echo $rt_command_21;sleep 1; echo $rt_command_22;sleep 1; echo $rt_command_23;sleep 1; echo $rt_command_24;sleep 1; echo "exit" ) | ssh $rt_host

The answer to the question is quite trivial:

typeset -j i
echo -n '(sleep 1; echo $rt_username; sleep 1; echo $rt_password; sleep 1;'
j=0
while [ $j -lt $i ] ; do
    j=j+1
    echo -n "$rt_command_$j;sleep1;"
done
echo 'echo "exit" ) | ssh $rt_host'

but this is almost certainly not what you want to do.

If it is possible, you should set-up public/private key authentication for ssh with your $rt_host which will remove the requirement to login every time.

If setting up key-authentication is absolutely impossible, you will need to fall-back to expect . As an example:

spawn ssh -l username target_host
expect "ord\\: "
send "secret_password\r"
set handle [ open file_with_commands r ]
while { ! [eof $handle] } {
    gets $handle buf
    expect "\\$"
    send "$buf\r"
}
send "exit\r"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM