需求
IT同学每个季度需要统计集团所有打印机的打印量,提供给供应商,根据打印量计算费用。 其中:
- 黑白打印机 只需统计总打印量1个数据。
- 彩色打印机 分为彩色大纸张、彩色小纸张、黑白大纸张、黑白小纸张,需要统计这4个数据。
集团打印机数十台,人工统计耗时长、效率低,因此写个脚本进行统计。
思路
通过Fping判断打印机IP地址存活性,对在线的打印机进行数据统计,非在线的记录下SN和IP地址,写到csv文件。
环境准备
- 安装Fping
wget https://fping.org/dist/fping-5.2.tar.gz
tar zxvf fping-5.2.tar.gz
sudo ./configure
sudo make
sudo make install
默认安装路径:/user/local/sbin/
脚本实现
import csv
import os
from datetime import datetime
date = datetime.now().date()
#需要的oid
sn_oid = '.1.3.6.1.4.1.1602.1.2.1.4.0'
name_oid = 'SNMPv2-MIB::sysDescr.0'
count_pri_oid = 'mib-2.43.10.2.1.4.1.1'
color_big_oid = '.1.3.6.1.4.1.1602.1.11.1.3.1.4.122'
color_small_oid = '.1.3.6.1.4.1.1602.1.11.1.3.1.4.123'
black_big_oid = '.1.3.6.1.4.1.1602.1.11.1.3.1.4.112'
black_small_oid = '.1.3.6.1.4.1.1602.1.11.1.3.1.4.113'
#黑白打印机
iplist = ['192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x']
#彩色打印机
iplist_color = ['192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x',
'192.x.x.x']
header = ("序列号", "打印量", "彩色大", "彩色小", "黑白大", "黑白小", "IP地址")
data = []
for ip in iplist:
res = os.system("/usr/local/sbin/fping %s -a" %ip)
if res == 0:
sn_data = os.popen("snmpwalk -v 1 -c public %s %s" %(ip,sn_oid))
count_pri_data = os.popen("snmpwalk -v 1 -c public %s %s" %(ip,count_pri_oid))
sn = sn_data.read().split('"')[1]
count_pri = count_pri_data.read().split("Counter32:")[-1]
data.append((sn, count_pri, "", "", "", "",ip))
else:
data.append((sn, "", "", "", "", "", ip))
for ip in iplist_color:
res = os.system("/usr/local/sbin/fping %s -a" %ip)
if res == 0:
sn_data = os.popen("snmpwalk -v 1 -c public %s %s" %(ip,sn_oid))
color_big = os.popen("snmpwalk -v 1 -c public %s %s" %(ip,color_big_oid))
color_small = os.popen("snmpwalk -v 1 -c public %s %s" %(ip,color_small_oid))
black_big = os.popen("snmpwalk -v 1 -c public %s %s" %(ip,black_big_oid))
black_small = os.popen("snmpwalk -v 1 -c public %s %s" % (ip, black_small_oid))
sn = sn_data.read().split('"')[1]
color_big = color_big.read().split("Counter32:")[-1]
color_small = color_small.read().split("Counter32:")[-1]
black_big = black_big.read().split("Counter32:")[-1]
black_small = black_small.read().split("Counter32:")[-1]
data.append((sn, "", color_big, color_small, black_big, black_small, ip))
else:
data.append((sn, "", "", "", "", "", ip))
with open("/Users/zhenning/Desktop/print-%s.csv" %date, "w", encoding="GBK", newline="") as f:
file = csv.writer(f)
file.writerow(header)
for row in data:
file.writerow(row)
- 效果展示
标签:打印机,oid,佳能,color,ip,打印,192,black,sn From: https://blog.51cto.com/magic3/12017106