获取交换机arp表
编写ARP获取脚本
#!/bin/bash
start_time=$(date +%s)
echo $(date)" shell脚本:组件2开始" >> ipmac.log
snmpwalk -v 2c -c jasolar@2020! 172.30.3.252 1.3.6.1.4.1.2011.5.25.123.1.17.1 | grep Hex-STRING | awk -F: '{print $3,$4}' | sed 's/\.1\.32 \= Hex\-STRING//g' | sed 's/\.4\.68\.67\.79\.84 = Hex-STRING//g' | sed 's/\.2\.79\.65 \= Hex\-STRING//g' | sed 's/\.4\.90\.74\.79\.84 \= Hex\-STRING//g' | grep -v '00 00 00 00 00 00' | awk -F'.' '{print $11"."$12"."$13"."$14}' > arp.txt
echo $(date)" shell脚本:组件2结束,组件1开始" >> ipmac.log
snmpwalk -v 2c -c jasolar@2020! 172.30.3.253 1.3.6.1.4.1.2011.5.25.123.1.17.1 | grep Hex-STRING | awk -F: '{print $3,$4}' | sed 's/\.1\.32 \= Hex\-STRING//g' | sed 's/\.4\.68\.67\.79\.84 = Hex-STRING//g' | sed 's/\.2\.79\.65 \= Hex\-STRING//g' | sed 's/\.4\.90\.74\.79\.84 \= Hex\-STRING//g' | grep -v '00 00 00 00 00 00' | awk -F'.' '{print $11"."$12"."$13"."$14}' >> arp.txt
echo $(date)" shell脚本:组件1结束,立体库开始" >> ipmac.log
snmpwalk -v 2c -c jasolar@2020! 172.30.3.244 1.3.6.1.4.1.2011.5.25.123.1.17.1 | grep Hex-STRING | awk -F: '{print $3,$4}' | sed 's/\.1\.32 \= Hex\-STRING//g' | sed 's/\.4\.68\.67\.79\.84 = Hex-STRING//g' | sed 's/\.2\.79\.65 \= Hex\-STRING//g' | sed 's/\.4\.90\.74\.79\.84 \= Hex\-STRING//g' | grep -v '00 00 00 00 00 00' | awk -F'.' '{print $11"."$12"."$13"."$14}' >> arp.txt
echo $(date)" shell脚本:立体库结束,电池2开始" >> ipmac.log
snmpwalk -v 2c -c jasolar@2020! 172.30.3.250 1.3.6.1.4.1.56813.5.25.123.1.17.1 | grep Hex-STRING | awk -F: '{print $3,$4}' | sed 's/\.1\.32 \= Hex\-STRING//g' | sed 's/\.4\.68\.67\.79\.84 = Hex-STRING//g' | sed 's/\.2\.79\.65 \= Hex\-STRING//g' | sed 's/\.4\.90\.74\.79\.84 \= Hex\-STRING//g' | grep -v '00 00 00 00 00 00' | awk -F'.' '{print $11"."$12"."$13"."$14}' >> arp.txt
echo $(date)" shell脚本:电池2结束,电池1开始" >> ipmac.log
snmpwalk -v 2c -c jasolar@2020! 172.30.3.251 1.3.6.1.4.1.2011.5.25.123.1.17.1 | grep Hex-STRING | awk -F: '{print $3,$4}' | sed 's/\.1\.32 \= Hex\-STRING//g' | sed 's/\.4\.68\.67\.79\.84 = Hex-STRING//g' | sed 's/\.2\.79\.65 \= Hex\-STRING//g' | sed 's/\.4\.90\.74\.79\.84 \= Hex\-STRING//g' | grep -v '00 00 00 00 00 00' | awk -F'.' '{print $11"."$12"."$13"."$14}' >> arp.txt
echo $(date)" shell脚本:电池1结束,单体汇聚开始" >> ipmac.log
snmpwalk -v 2c -c jasolar@2020! 172.30.3.243 1.3.6.1.4.1.2011.5.25.123.1.17.1 | grep Hex-STRING | awk -F: '{print $3,$4}' | sed 's/\.1\.32 \= Hex\-STRING//g' | sed 's/\.4\.68\.67\.79\.84 = Hex-STRING//g' | sed 's/\.2\.79\.65 \= Hex\-STRING//g' | sed 's/\.4\.90\.74\.79\.84 \= Hex\-STRING//g' | grep -v '00 00 00 00 00 00' | awk -F'.' '{print $11"."$12"."$13"."$14}' >> arp.txt
echo $(date)" shell脚本:单体汇聚结束,包材库开始" >> ipmac.log
snmpwalk -v 2c -c jasolar@2020! 172.30.3.249 1.3.6.1.4.1.2011.5.25.123.1.17.1 | grep Hex-STRING | awk -F: '{print $3,$4}' | sed 's/\.1\.32 \= Hex\-STRING//g' | sed 's/\.4\.68\.67\.79\.84 = Hex-STRING//g' | sed 's/\.2\.79\.65 \= Hex\-STRING//g' | sed 's/\.4\.90\.74\.79\.84 \= Hex\-STRING//g' | grep -v '00 00 00 00 00 00' | awk -F'.' '{print $11"."$12"."$13"."$14}' >> arp.txt
end_time=$(date +%s)
execution_time=$((end_time - start_time))
echo "shell脚本:消耗时间:"$execution_time"s" >> ipmac.log编写Python脚本将获取的数据进行拆分,写入数据库
import re
import cx_Oracle
import datetime
import subprocess
import os
# 创建日志文件
if os.path.exists("ipmac.log"):
f = open("ipmac.log", "a+")
f.write("\n" + "--" * 30)
else:
f = open("ipmac.log", "w+")
# 调用arp.sh脚本,并执行
shell_script = "arp.sh"
f.write("\n" + str(datetime.datetime.now()) + " " + "执行arp.sh脚本")
print(str(datetime.datetime.now()) + " 执行arp.sh脚本获取ip与mac对应表需要一定时长,耐心等等")
# 执行ARP.SH脚本
result = subprocess.run(["sh", shell_script], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.stdout.decode('utf-8'))
print(result.stderr.decode('utf-8'))
f.write("\n" + str(datetime.datetime.now()) + " " + "arp.sh脚本执行完成")
print(str(datetime.datetime.now()) + " arp.sh脚本执行完成")
# 将数据写入数据库
# 连接数据库,需要提前安装Oracle Instant Client
connection = cx_Oracle.connect(user="hik", password="Jiaen#2020", dsn="172.30.64.45/ORCLPDB1")
# 创建游标
cursor = connection.cursor()
print(str(datetime.datetime.now()) + " 数据库链接成功")
f.write("\n" + str(datetime.datetime.now()) + ' ' + str(connection))
f.write("\n" + str(datetime.datetime.now()) + ' ' + str(cursor))
print(str(datetime.datetime.now()) + " 获取数据开始写入ip_mac_log表")
# 读取文件
with open("arp.txt", "r") as file:
line = file.readlines()
# print(line[0])
for ip in line:
# IP正则表达式
ip_pattern = re.compile(
r'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b')
# 匹配IP地址
ip_address = ip_pattern.findall(ip)[0]
# 截取MAC地址
mac = ip[-19:]
# 写入数据到ip_mac_log表
ip_UsedLog_SQL = "insert into ip_mac_log (ip_address,mac,find_time) \
values (:ip_address,:mac,:find_time)"
cursor.execute(ip_UsedLog_SQL, [ip_address, mac, datetime.datetime.now()])
connection.commit()
f.write(str(datetime.datetime.now()) + ' ' + str(ip_address) + ' ' + str(mac))
print(str(datetime.datetime.now()) + " 执行结束")
file.close()
f.close()
connection.close()