将变量输入到awk脚本

时间:2017-07-20 09:47:28

标签: shell awk

我正在开发一个从网卡中检测CDP信息的脚本。该脚本目前可以在我的计算机上运行,​​但我想让它可供其他人使用。该脚本当前运行,因为在代码中它具有我的网卡名称。我想在运行脚本之前询问网卡名称(可能列出可用的名称)。我的代码是:

#!/usr/bin/awk -f

function red(s) {
    printf "\033[1;31m" s "\033[0m "
}

function green(s) {
     printf "\033[1;32m" s "\033[0m "
}

function blue(s) {
    printf "\033[1;34m" s "\033[0m "
}

BEGIN{

cmd = "tcpdump -nvi enp0s25 -s 1500 ether dst 01:00:0c:cc:cc:cc -c 1"
while ((cmd | getline) > 0) {
str="Device-ID:Cisco IOS Software:Port-ID:VTP:Address:VLAN:Duplex:VoIP:"
split(str,s,":")

for(i=1;i<=length(s);i++){
    if ($0 ~ s[i] && s[i]){

            if (i==1){            
            print "\n"; 
            print red("Device Name: ") green($7);
            }   

            else if (i==2){                          
            print red("Software Version: ") green($0) ;
                }

            else if (i==3){                      /*Port*/
            print red($1 ": ") green($7);
                }
        else if (i==4){                             
            print red($1 " " $2 " " $3 ": ") green($9);
                }

            else if (i==5){                             
            print red("IP Address: ") green($9);
                }

        else if (i==6){                                 
            print red("VLAN: ") green($9);
                }

        else if (i==7){                                
            print red("DUPLEX: ") green($7);
                }

            else if (i==8){                                 
            print red("Voice VLAN: ") green($13);
                }

        else{ 
                s[i]=0;print "   "}
                }  
            }
        }
}       

如您所见,它使用我的NIC运行命令 enp0s25 。我需要将这个变量设置为用户输入的变量(可能只有一次)。最好的方法是枚举卡并让用户使用数字选择他想要的卡。我没有IDEA如何做到这一点。

2 个答案:

答案 0 :(得分:1)

通过在awk中编写对tcpdump的调用来尝试使用awk作为shell,你正在为自己的生活更加艰难。想一想 - 你正在编写一个shell脚本来调用awk调用shell来调用tcpdump,而不是只调用shell调用tcpdump并将输出传递给awk。

只需将您的shell脚本编写为:

tcpdump ... enp0s25 ... | awk 'script'

然后你可以用明显的方式调整它:

echo "enter a nic: "
IFS= read -r nic
tcpdump ... "$nic" ... | awk 'script'

答案 1 :(得分:0)

#!/usr/bin/awk -f 

function red(s) {
printf "\033[1;31m" s "\033[0m "
 }

function green(s) {
printf "\033[1;32m" s "\033[0m "
}

function blue(s) {
  printf "\033[1;34m" s "\033[0m "
 }

 function detectNic( ) {
    printf "Enter the Ethernet card to listen or press enter to listen 
         to Default:";getline nic<"/dev/tty"
      if (nic == ""){

        command = "cat /var/defaultnic"

        nic = command | getline
    print "Default network card to listen: " $nic       
    return $nic

        }
    else{   
    return nic
    }

 }
BEGIN{

nic = detectNic();
cmd = "tcpdump -nv -s 1500 ether dst 01:00:0c:cc:cc:cc -c 1 -i"

while ((cmd nic | getline) > 0) {
str="Device-ID:Cisco IOS Software:Port-ID:VTP:Address:VLAN:Duplex:VoIP:"
split(str,s,":")

for(i=1;i<=length(s);i++){
    if ($0 ~ s[i] && s[i]){

    #DEVICE ID
            if (i==1){            
            print "\n"; 
            print red("Device Name: ") green($7);
            }   

#SOFTWARE VERSION      
            else if (i==2){                          
            print red("Software Version: ") green($0) ;
                }

 #PORT      
            else if (i==3){                      
            print red($1 ": ") green($7);
                }
     #VTP DOMAIN    
        else if (i==4){                             
            print red($1 " " $2 " " $3 ": ") green($9);
                }

     #IP ADDRESS
            else if (i==5){                             
            print red("IP Address: ") green($9);
                }

    #CURRENT VLAN     
        else if (i==6){                                 
            print red("VLAN: ") green($9);
                }

       #DUPLEX
        else if (i==7){                                
            print red("DUPLEX: ") green($7);
                }

      #VOICE VLAN
            else if (i==8){                                 
            print red("Voice VLAN: ") green($13);
                }

        else{ 
                s[i]=0;print "   "}
                }  
            }
        }
}