zoukankan      html  css  js  c++  java
  • 批量抓取cisco设备配置脚本编写(expect/sed/awk/shell)

    应同事需求自行编写了第一个脚本,中间遇到一些坑。

    需求,要求抓取设备“show ip interface brief”信息和“show interface des”描述信息。并且要求VLAN与描述信息在一个文件中显示出来。其实抓取工作很简单,难点在于字符处理中遇到时很多坑,比如关键字"More“抓取之后会有类似于“^[[7m--More--^[[m^M”的多余字符,是因为系统版本原因产生的。要用sed将其处理为awk可以匹配的格式。

    先上内容。

    1,抓取“show ip interface brief”信息(show inter des命令直接替换)。

    #!/usr/bin/expect

    #set ip
    set ipaddress [lindex $argv 0]

    #set user
    set name [lindex $argv 1]

    #set passwd
    set passwd [lindex $argv 2]

    #set timeout
    set timeout 2

    #ssh process
    spawn ssh $name@$ipaddress

    #进行判断
    expect {
    "yes/no" { send "yes\r" }
    "*assword:" { send "$passwd\r";exp_continue}
    "*# " { send "show ip inter bri\r" }

    while (1) {
    expect {
    "More--" { send " " }
    "*#" { break }
         }
    }
    expect eof

    2,字符处理脚本

    用户名密码文件的格式为:

    ip1 user1 passwd1

    ip2 user2 passwd2

    脚本如下:

    #!/bin/bash
    p=`pwd`
    for i in `awk '{print $1}' $p/passwd`                           #取文件中第一列IP
    do
    j=`grep $i $p/passwd | awk '{print $2}'`
    k=`grep $i $p/passwd | awk '{print $3}'`                   #j,k分别为取到的用户名,密码
    echo "正在抓取"$i"的信息"
    expect $p/ssh-brief.exp $i $j $k | grep Vlan > $p/brief/$i-bri.log                #将ip,j,k作为参数传给expect登录并取配置存入文件中
        if [ $? -eq 0 ];then
          echo $i"地址文件抓取成功,下一步抓取描述文件。"
        else
          echo $i"地址文件抓取失败,即将退出脚本"
        exit
        fi
    expect $p/ssh-des.exp $i $j $k | grep Vlan > $p/des/$i-des.log
        if [ $? -eq 0 ];then
          echo $i"描述文件抓取成功成功"
        else
          echo $i"描述文件抓取失败,即将退出脚本"
        exit
        fi

    echo "处理"$i"的地址文件"

    sed -i 's/^.*Vlan/Vlan/g' $p/brief/$i-bri.log                #由于系统不同的原因产生了^[[7m--More--^[[m^M等多余字符这里对多余的字符进行处理。
        if [ $? -eq 0 ];then
          echo $i"地址文件处理成功"
        else
          echo $i"地址文件处理失败,即将退出脚本"
          exit
        fi
    echo "处理"$i"的描述文件"
    sed -i 's/^.*Vlan/Vlan/g' $p/des/$i-des.log
        if [ $? -eq 0 ];then
          echo $i"描述文件处理成功"
        else
          echo $i"描述文件处理失败,即将退出脚本"
          exit
        fi
    echo "生成"$i"文件"
    awk '{if(NR==FNR){i++;a1[i]=$1;a2[i]=$2;a3[i]=$3}else{j++;b1[j]=$1;b2[j]=$2" "$3" "$4}}END{for(m=0;m<=i;m++)for(n=0;n<=j;n++){if(a1[m]==b1[n]){print a1[m],a2[m],a3[m],b2[n]}}}' $p/brief/$i-bri.log $p/des/$i-des.log > $p/log/$i.log     #处理两个文件件合并为一个文件,以Vlan为标识对两个文件相同Vlan匹配后生成新文件
        if [ $? -eq 0 ];then
          sed -i "s/$(echo -e '\015')//g" $/log/$i.log                     #去除新生成文件中的^M字符。

            if [ $? -eq 0 ];then
              echo $i"---------------------------------------------------------------------成功"
        else
          echo "生成"$i"文件失败,即将退出。"
          exit
        fi

    done

  • 相关阅读:
    使用 Dockerfile 定制镜像
    UVA 10298 Power Strings 字符串的幂(KMP,最小循环节)
    UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)
    LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)
    LeetCode Number of Islands 岛的数量(DFS,BFS)
    LeetCode Triangle 三角形(最短路)
    LeetCode Swap Nodes in Pairs 交换结点对(单链表)
    LeetCode Find Minimum in Rotated Sorted Array 旋转序列找最小值(二分查找)
    HDU 5312 Sequence (规律题)
    LeetCode Letter Combinations of a Phone Number 电话号码组合
  • 原文地址:https://www.cnblogs.com/feigerlan/p/8024244.html
Copyright © 2011-2022 走看看