简体   繁体   English

使用Pysnmp使用SNMP获取打印机状态

[英]Get printer status with SNMP using Pysnmp

I try to get status from my printer using SNMP protocol The problem is, I've never used the SNMP and I have trouble understanding how can I get my status like ( PAPER OUT, RIBBON OUT, etc... ). 我尝试使用SNMP协议从打印机获取状态。问题是,我从未使用过SNMP,并且无法理解如何获取状态(如PAPER OUT,RIBBON OUT等)。

I configured my printer to enable the SNMP protocol using the community name "public" 我将打印机配置为使用社区名称“ public”启用SNMP协议
I presume SNMP messages are sent on the port 161 我假设SNMP消息在端口161上发送

I'm using Pysnmp because I want to integrate the python script in my program to listen to my printer and display status if there is a problem with the printer. 我使用Pysnmp是因为我想将python脚本集成到程序中,以侦听打印机并在打印机出现问题时显示状态。

For now I've tried this code : 现在,我已经尝试了以下代码:

import socket
import random
from struct import pack, unpack
from datetime import datetime as dt
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.proto.rfc1902 import Integer, IpAddress, OctetString


ip = '172.20.0.229'
community = 'public'
value = (1,3,6,1,2,1,25,3,5,1,2)

generator = cmdgen.CommandGenerator()
comm_data = cmdgen.CommunityData('server', community, 1)  # 1 means version SNMP v2c
transport = cmdgen.UdpTransportTarget((ip, 161))

real_fun = getattr(generator, 'getCmd')
res = (errorIndication, errorStatus, errorIndex, varBinds) \
    = real_fun(comm_data, transport, value)

if not errorIndication is None or errorStatus is True:
    print "Error: %s %s %s %s" % res
else:
    print "%s" % varBinds

The IP address is the IP of my printer The problem is the OID: I don't know what to put in the OID field because I have trouble understanding how does OID work. IP地址是我的打印机的IP问题是OID:我不知道要在OID字段中输入什么,因为我无法理解OID的工作原理。

I found this page but I'm not sure it fits with all printers ==> click here 我找到了此页面,但是我不确定它是否适合所有打印机==> 单击此处

You need your printer specific MIB file in common case. 通常情况下,您需要打印机特定的MIB文件。 Eg, printer in my office seems to be not support both oids by your link. 例如,我的办公室中的打印机似乎不支持您的链接同时支持这两种OID。 Also you can use snmpwalk to get available oids and values on your printer and if you somehow understand which values you need, you can use it for specific instance of your printer. 您也可以使用snmpwalk获取打印机上可用的oid和值,如果您以某种方式了解所需的值,则可以将其用于打印机的特定实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM