Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.30 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function help() {
  4.  
  5.         echo "Usage: $0 [ DIR | files ]"
  6.         echo "Options:"
  7.         echo "    DIR - root directory where are subdirs for labs"
  8.         echo "    files - files to be converted"
  9.         echo "Examples:"
  10.         echo "    $0 \"./CCNP/Configs for Trouble Tickets\""
  11.         echo "    $0 ./Lab101-ALS1-TT-A-Cfg.txt Laby1-R3-TT-B-Cfg.txt"
  12. }
  13.  
  14. # $1 ... path to original config file
  15. # $2 ... original FastEthernet interface number (0/1, 1/1/1)
  16. # $3 ... IOU Ethernet interface number
  17. FastEthToEth() {
  18.     if [ "$#" -ne 3 ]; then
  19.         echo "Invalid args in FastEthToEth function call"
  20.         exit 1
  21.     fi
  22.  
  23.     oldiface=$(sed 's#/#\\/#' <<< $2)
  24.     newiface=$(sed 's#/#\\/#' <<< $3)
  25.  
  26.     sed -i 's/FastEthernet'$oldiface'/Ethernet'$newiface'/g' "$1"
  27. }
  28.  
  29. # $1 path to config file
  30. # $2 interface number to delete from config
  31. delete_FastEth() {
  32.  
  33.  
  34.     iface=$(sed 's#/#\\/#' <<< $2)
  35.     sed -i '/interface FastEthernet'$iface'/,/!/d' "$1"
  36. }
  37.  
  38. # $1 path to config file
  39. # $2 interface number to delete from config
  40. delete_GigaEth() {
  41.  
  42.     iface=$(sed 's#/#\\/#' <<< $2)
  43.     sed -i '/interface GigabitEthernet'$iface'/,/!/d' "$1"
  44. }
  45.  
  46. # $1 ... path to original config file
  47. parse_file() {
  48.  
  49.  
  50.     dn=$(dirname "$1")
  51.  
  52.     # Create new dir in iou_configs
  53.     if [ ! -d "./iou_configs/$dn" ]; then
  54.         mkdir -p "./iou_configs/$dn"
  55.     fi
  56.  
  57.     fn=$(basename "$1")
  58.     # Copy original to iou_configs dir
  59.     cp "$1" "./iou_configs/$dn/$fn"
  60.  
  61.     new_file_path="./iou_configs/$dn/$fn"
  62.  
  63.     # Mappings of Lab interfaces to IOU interfaces
  64.     # TODO
  65.     # 1. Delete unused FastEthernet && GigabitEthernet ifs
  66.     # 2. Rename some of the FastEthernet to corresponding unused Ethernet ifs
  67.     if [[ $1 == *"-DLS1-"* ]]; then
  68.         FastEthToEth "$new_file_path" 0/1 0/1
  69.         FastEthToEth "$new_file_path" 0/2 0/0
  70.         FastEthToEth "$new_file_path" 0/3 0/3
  71.         FastEthToEth "$new_file_path" 0/4 0/2
  72.         FastEthToEth "$new_file_path" 0/5 1/0
  73.         FastEthToEth "$new_file_path" 0/6 1/1
  74.     elif [[ $1 == *"-DLS2-"* ]]; then
  75.         FastEthToEth "$new_file_path" 0/1 0/1
  76.         FastEthToEth "$new_file_path" 0/2 0/0
  77.         FastEthToEth "$new_file_path" 0/3 0/3
  78.         FastEthToEth "$new_file_path" 0/4 0/2
  79.         FastEthToEth "$new_file_path" 0/5 1/0
  80.         FastEthToEth "$new_file_path" 0/18 1/1
  81.     elif [[ $1 == *"-ALS1-"* ]]; then
  82.         delete_FastEth "$new_file_path" 0/5
  83.         delete_FastEth "$new_file_path" 0/6
  84.         delete_FastEth "$new_file_path" 0/7
  85.         delete_FastEth "$new_file_path" 0/8
  86.         delete_FastEth "$new_file_path" 0/9
  87.         delete_FastEth "$new_file_path" 0/10
  88.         delete_FastEth "$new_file_path" 0/11
  89.         delete_FastEth "$new_file_path" 0/12
  90.         delete_FastEth "$new_file_path" 0/13
  91.         delete_FastEth "$new_file_path" 0/14
  92.         delete_FastEth "$new_file_path" 0/15
  93.         delete_FastEth "$new_file_path" 0/16
  94.         delete_FastEth "$new_file_path" 0/17
  95.         delete_FastEth "$new_file_path" 0/19
  96.         delete_FastEth "$new_file_path" 0/20
  97.         delete_FastEth "$new_file_path" 0/21
  98.         delete_FastEth "$new_file_path" 0/22
  99.         delete_FastEth "$new_file_path" 0/23
  100.         delete_FastEth "$new_file_path" 0/24
  101.         delete_GigaEth "$new_file_path" 0/1
  102.         delete_GigaEth "$new_file_path" 0/2
  103.         FastEthToEth   "$new_file_path" 0/1 0/1
  104.         FastEthToEth   "$new_file_path" 0/2 0/0
  105.         FastEthToEth   "$new_file_path" 0/3 0/3
  106.         FastEthToEth   "$new_file_path" 0/4 0/2
  107.         FastEthToEth   "$new_file_path" 0/18 1/1
  108.  
  109.     elif [[ $1 == *"-R1-"* ]]; then
  110.         echo TODO
  111.     elif [[ $1 == *"-R2-"* ]]; then
  112.         echo TODO
  113.     elif [[ $1 == *"-R3-"* ]]; then
  114.         echo TODO
  115.     else
  116.         echo "Invalid path $1"
  117.     fi
  118. }
  119.  
  120. ###### MAIN ######
  121.  
  122. # If no args supplied, print help and exit
  123. if [ "$#" -eq 0 ]; then
  124.     help
  125.     exit 1
  126. fi
  127.  
  128. # If not created already, create folder for storing result configs
  129. if [ ! -d "./iou_configs" ]; then
  130.     mkdir "./iou_configs"
  131. fi
  132.  
  133. # If dir is supplied as arg, process it recursively
  134. if [ -d "$1" ]; then
  135.     echo
  136. # If list of files is supplied as args, process them sequentialy
  137. else
  138.     for file in "$@"; do
  139.         if [ -f "$file" ]; then
  140.             parse_file "$file"
  141.         fi
  142.     done
  143. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement