当我尝试运行代码时。我遇到了错误。
错误是
Traceback (most recent call last): File "A2.py", line 2, in <module> from easysnmp import Session ImportError: No module named 'easysnmp'
注意:我遇到了以上错误。尽管如此,我已经安装了easysnmp模块。
代码是
#!/usr/bin/python
from easysnmp import Session
import argparse
import time
parser = argparse.ArgumentParser(description='probe')
parser.add_argument('cred',help='credentials')
parser.add_argument('freq',type=float,help='enter frequency')
parser.add_argument('samples',type=int,help='enter samples')
parser.add_argument('oid',nargs='+',help='enter oid')
args=parser.parse_args()
t=1/args.freq
s=args.samples
cred1=args.cred
ip,port,comm=cred1.split(":")
count=0
session=Session(hostname=ip,remote_port=port,community=comm, version=2,timeout=2,retries=1)
args.oid.insert(0, '1.3.6.1.2.1.1.3.0')
old=[]
out1=[]
t4=0
while (count!=s):
t1=time.time()
new = session.get(args.oid)
t2=time.time()
if len(new)==len(old):
newtime=float(new[0].value)/100
oldtime=float(old[0].value)/100
if args.freq > 1:
tdiff = newtime-oldtime
if args.freq <= 1:
tdiff1 = t1-t4
if tdiff!=0:
tdiff = int(tdiff1)
else:
tdiff = int(t)
for i in range(1,len(args.oid)):
if new[i].value!="NOSUCHINSTANCE" and old[i].value!="NOSUCHINSTANCE":
a=int(new[i].value)
b=int(old[i].value)
if a>=b:
out=(a-b)/tdiff
out1.append(out)
if a<b and new[i].snmp_type=="COUNTER64":
out=((2**64+a)-b)/tdiff
out1.append(out)
if a<b and new[i].snmp_type=="COUNTER32":
out=((2**32+a)-b)/tdiff
out1.append(out)
else:
print t1, "|"
count=count+1
if len(out1)!=0:
sar = [str(get) for get in out1]
print int(t1) ,'|', ("|" . join(sar))
old = new[:]
t4=t1
del out1[:]
t3=time.time()
if t-t3+t1>0:
time.sleep(t-t3+t1)
else:
time.sleep(0.0)
答案 0 :(得分:0)
我的猜测是您为python 3安装了模块,并使用了python 2或相反的方式。
尝试
pip install easysnmp
或
pip3 install easysnmp
答案 1 :(得分:0)
尝试将import easysnmp
放在代码的顶部,在类似的情况下它为我解决了问题!