可能使用awk或sed的Bash输出

时间:2017-11-17 11:37:17

标签: bash awk sed scripting grep

我有一个如下所示的文件。

输入文件样本

$ 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

我有以下脚本

#!/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

脚本输出一个看起来像这样的文件

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'

但我也需要输出以下内容我不知道如何到达那里。

(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;睡眠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;睡眠1; echo $ rt_command_18;睡眠1; echo $ rt_command_19;睡眠1; echo $ rt_command_20; sleep 1; echo $ rt_command_21;睡眠1; echo $ rt_command_22; sleep 1; echo $ rt_command_23; sleep 1; echo $ rt_command_24;睡眠1; echo"退出" )| ssh $ rt_host

1 个答案:

答案 0 :(得分:0)

这个问题的答案非常简单:

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'

但这几乎肯定不是你想要做的。

如果可能,您应该使用$rt_host为ssh设置公钥/私钥认证,这将取消每次登录的要求。

如果完全无法设置密钥身份验证,则需要回退到expect。举个例子:

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"