CISCO XR copy configuration to or from TFTP by SNMP

遇到一个这样的需求,想使用SNMP的方式去copy设备的配置到TFTP server。查了点资料发现真存在这种方式,虽然感觉这种功能很鸡肋,但还是记录以下以备查询。

Snmpwalk/snmpset

Snmpwalk是一个很好用的小工具,平常查OID值的时候经常会遇到,Centos的话直接用yum进行安装就行了。

yum -y install net-snmp net-snmp-utils
snmpwalk -v 2c -c cisco 10.75.49.13 1.3.6.1.2.1.1.3.0

SNMP copy配置这个功能使用的是CISCO-CONFIG-COPY-MIB,目前在大部分的IOS / IOS XR上看起来都是支持的,且需要rw权限。

MIB Object NameMIB Object NameValues used in the example
ccCopyEntryRowStatus1.3.6.1.4.1.9.9.96.1.1.1.1.141:active
2:notInService
3:notReady
4:createAndGo
5:createAndWait
6:destroy
ccCopyProtocol1.3.6.1.4.1.9.9.96.1.1.1.1.21:tftp
2:ftp
3:rcp
4:scp
5:sftp
ccCopySourceFileType1.3.6.1.4.1.9.9.96.1.1.1.1.31.networkFile
2.iosFile
3.startupConfig
4.runningConfig
5.terminal
ccCopyDestFileType1.3.6.1.4.1.9.9.96.1.1.1.1.41.networkFile
2.iosFile
3.startupConfig
4.runningConfig
5.terminal
ccCopyServerAddress1.3.6.1.4.1.9.9.96.1.1.1.1.5
ccCopyFileName1.3.6.1.4.1.9.9.96.1.1.1.1.6
ccCopyState1.3.6.1.4.1.9.9.96.1.1.1.1.101:waiting
2:running
3:successful
4:failed

配置CISCO设备

RP/0/RSP0/CPU0:CORE4-ASR9010-A#show run snmp-server 
snmp-server community public RW

Copy cfg from IOS XR router to tftp

* Create an Entry and Wait(777 is a random session number):
snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.14.777 i 5
* Set Copy protocol to tftp
snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.2.777 i 1
* Set copy to tftp server
snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.3.777 i 4
* Set copy from config  
snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.4.777 i 1
* Set tftp server address
snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.5.777 a 10.75.49.254
* Set destination file name
snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.6.777 s xuxingtest
* Activate the entry 
snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.14.777 i 1
* Once successful, destroy the entry
snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.14.777 i 6

Copy cfg to tftp server Example

[root@xuxing ~]# snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.14.777 i 5
SNMPv2-SMI::enterprises.9.9.96.1.1.1.1.14.777 = INTEGER: 5
[root@xuxing ~]# snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.2.777 i 1
SNMPv2-SMI::enterprises.9.9.96.1.1.1.1.2.777 = INTEGER: 1
[root@xuxing ~]# snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.3.777 i 4
SNMPv2-SMI::enterprises.9.9.96.1.1.1.1.3.777 = INTEGER: 4
[root@xuxing ~]# snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.4.777 i 1
SNMPv2-SMI::enterprises.9.9.96.1.1.1.1.4.777 = INTEGER: 1
[root@xuxing ~]# snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.5.777 a 10.75.49.254
SNMPv2-SMI::enterprises.9.9.96.1.1.1.1.5.777 = IpAddress: 10.75.49.254
[root@xuxing ~]# snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.6.777 s xuxingtest
SNMPv2-SMI::enterprises.9.9.96.1.1.1.1.6.777 = STRING: "xuxingtest"
[root@xuxing ~]# snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.14.777 i 1
SNMPv2-SMI::enterprises.9.9.96.1.1.1.1.14.777 = INTEGER: 1
[root@xuxing ~]# snmpset -v2c -c public 10.75.49.9 .1.3.6.1.4.1.9.9.96.1.1.1.1.14.777 i 6
SNMPv2-SMI::enterprises.9.9.96.1.1.1.1.14.777 = INTEGER: 6

Verify log from router:

login device you will see below syslog:

RP/0/RSP0/CPU0:Dec 10 13:39:34.876 : syslog_dev[95]: mibd_infra[338] PID-13783439: Building configuration. 
RP/0/RSP0/CPU0:Dec 10 13:39:34.876 : syslog_dev[95]: mibd_infra[338] PID-13783439:  
RP/0/RSP0/CPU0:Dec 10 13:39:34.876 : syslog_dev[95]: mibd_infra[338] PID-13783439: 2325 lines built in 1 second 
RP/0/RSP0/CPU0:Dec 10 13:39:34.876 : syslog_dev[95]: mibd_infra[338] PID-13783439: [OK

如果是copy的目的地是ftp server, 你还可以从IOS XR设备上看到如下的ftp trace:

RP/0/RSP0/CPU0:ios#show ftp client trace | in open
Wed Aug  2 08:34:22.141 GMT
Aug  2 08:01:00.923 ftp_fs/events 0/RSP0/CPU0 t4  [FTP:(0x12021c8c):ftp_data] opening data connection
Aug  2 08:01:06.329 ftp_fs/events 0/RSP0/CPU0 t4  [FTP:(557392):ftp_open] connect flag 802  mode 1
Aug  2 08:01:06.329 ftp_fs/events 0/RSP0/CPU0 t4  [FTP:ftp_open_display_path] execute open (guest:*@10.70.79.197;default/10.124.50.38_20230801103013 , 1)
Aug  2 08:01:06.329 ftp_fs/events 0/RSP0/CPU0 t4  [FTP:(0x12021c8c):ftp_open_common] allocated ocb
Aug  2 08:01:06.329 ftp_fs/events 0/RSP0/CPU0 t4  [FTP:(0x12021c8c):ftp_start] opening control connection localvrf default

当然你也可以看到相关MIB的访问时间:

RP/0/RSP0/CPU0:ASR9006-1#show snmp  mib access time | in 1.3.6.1.4.1.9.9.96.1.1.1.1.
Wed Aug  2 08:36:26.167 GMT
1.3.6.1.4.1.9.9.96.1.1.1.1.2             08/02 08:36:20                          0           SET            
1.3.6.1.4.1.9.9.96.1.1.1.1.3             08/02 08:36:20                          0           SET            
1.3.6.1.4.1.9.9.96.1.1.1.1.4             08/02 08:36:20                          0           SET            
1.3.6.1.4.1.9.9.96.1.1.1.1.5             08/02 08:36:20                          0           SET            
1.3.6.1.4.1.9.9.96.1.1.1.1.6             08/02 08:36:20                          0           SET            
1.3.6.1.4.1.9.9.96.1.1.1.1.7             08/02 08:36:20                          0           SET            
1.3.6.1.4.1.9.9.96.1.1.1.1.8             08/02 08:36:20                          0           SET            
1.3.6.1.4.1.9.9.96.1.1.1.1.10            08/02 08:36:26  08/02 08:36:26          0           NEXT           
1.3.6.1.4.1.9.9.96.1.1.1.1.11            08/02 08:35:21                          0           NEXT           
1.3.6.1.4.1.9.9.96.1.1.1.1.14            08/02 08:36:20                          0           SET 

Copy cfg from IOS XR router to ftp

这里我们写了一个copy配置到ftp的脚本:

使用的方法很简单, 事例“./snmpcp2ftp.sh cisco 10.x.x.x 1 user pass 10.x.x.x“

#!/bin/bash

# 检查是否提供了正确的参数
if [ $# -ne 6 ]; then
  echo "使用方法: $0 COMMUNITY IP_ADDR NUM_OF_EXECUTIONS USERNAME PASSWORD SERVER_IP"
  exit 1
fi

COMMUNITY=$1
IP_ADDR=$2
NUM_OF_EXECUTIONS=$3
USERNAME=$4
PASSWORD=$5
SERVER_IP=$6

# 定义函数用于等待snmpwalk输出为3
wait_for_snmpwalk() {
  while true; do
    result=$(snmpwalk -v2c -c "$COMMUNITY" "$IP_ADDR" .1.3.6.1.4.1.9.9.96.1.1.1.1.10.$RANDOM_NUMBER | grep "INTEGER: 3")
    if [ -n "$result" ]; then
      break
    else
      sleep 2
    fi
  done
}

# 循环执行脚本,并输出执行次数
for ((i = 1; i <= $NUM_OF_EXECUTIONS; i++)); do
  echo "第 $i 次执行完毕"

  # 生成一个新的随机数,作为每次执行的常量
  RANDOM_NUMBER=$RANDOM

  # 获取当前时间戳
  CURRENT_TIME=$(date +"%Y%m%d%H%M%S")

  # 拼接新的字符串,形如 "IP_ADDR_时间"
  NEW_VALUE="$IP_ADDR"_"$CURRENT_TIME"

  # 执行脚本中的命令,并使用传递的参数、随机数和新的字符串
  snmpset -v2c -c "$COMMUNITY" "$IP_ADDR" .1.3.6.1.4.1.9.9.96.1.1.1.1.14.$RANDOM_NUMBER i 5
  snmpset -v2c -c "$COMMUNITY" "$IP_ADDR" .1.3.6.1.4.1.9.9.96.1.1.1.1.2.$RANDOM_NUMBER i 2
  snmpset -v2c -c "$COMMUNITY" "$IP_ADDR" .1.3.6.1.4.1.9.9.96.1.1.1.1.3.$RANDOM_NUMBER i 4
  snmpset -v2c -c "$COMMUNITY" "$IP_ADDR" .1.3.6.1.4.1.9.9.96.1.1.1.1.4.$RANDOM_NUMBER i 1
  snmpset -v2c -c "$COMMUNITY" "$IP_ADDR" .1.3.6.1.4.1.9.9.96.1.1.1.1.5.$RANDOM_NUMBER a "$SERVER_IP"
  snmpset -v2c -c "$COMMUNITY" "$IP_ADDR" .1.3.6.1.4.1.9.9.96.1.1.1.1.6.$RANDOM_NUMBER s "$NEW_VALUE"
  snmpset -v2c -c "$COMMUNITY" "$IP_ADDR" .1.3.6.1.4.1.9.9.96.1.1.1.1.7.$RANDOM_NUMBER s "$USERNAME"
  snmpset -v2c -c "$COMMUNITY" "$IP_ADDR" .1.3.6.1.4.1.9.9.96.1.1.1.1.8.$RANDOM_NUMBER s "$PASSWORD"
  snmpset -v2c -c "$COMMUNITY" "$IP_ADDR" .1.3.6.1.4.1.9.9.96.1.1.1.1.14.$RANDOM_NUMBER i 1
  wait_for_snmpwalk

  # 销毁session
  snmpset -v2c -c "$COMMUNITY" "$IP_ADDR" .1.3.6.1.4.1.9.9.96.1.1.1.1.14.$RANDOM_NUMBER i 6
done

Copy file from server to router

tftp to router

snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.14.777 i 5
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.2.777 i 1
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.3.777 i 1
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.4.777 i 4
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.5.777 a 10.7.7.19
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.6.777 s cfg
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.14.777 i 1
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.14.777 i 6

ftp to router

snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.14.777 i 5
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.2.777 i 2
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.3.777 i 1
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.4.777 i 4
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.5.777 a 10.7.7.19
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.6.777 s cfg
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.7.777 s guest
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.8.777 s guest
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.14.777 i 1
snmpset -v2c -c cisco   10.2.163.17 1.3.6.1.4.1.9.9.96.1.1.1.1.14.777 i 6
           

No comments

Comments feed for this article

Reply

Your email address will not be published. Required fields are marked *