0

当我尝试运行代码时。我收到一个错误。

错误是

    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)
4

2 回答 2

2

尝试将代码放在import easysnmp代码的顶部,它在类似情况下为我解决了问题!

于 2019-06-25T15:55:00.553 回答
0

我的疯狂猜测是您安装了 python 3 的模块并使用了 python 2 或其他方式。

尝试

pip install easysnmp

或者

pip3 install easysnmp
于 2019-06-25T15:48:55.697 回答