Centos7 下安装Python-netsnmp模块

先装一下python-devel 模块,执行命令 yum install python-devel  ,不然在make netsnmp 的过程中会出现如下错误:

netsnmp/client_intf.c:1:20: fatal error: Python.h: No such file or directory
#include <Python.h>
^
compilation terminated.
error: command 'gcc' failed with exit status 1
make: *** [pythonmodules] Error 1

下载setuptools-0.6c11-py2.7.egg:https://pypi.python.org/pypi/setuptools/0.6c11#downloads
下载net-snmp-5.7.3.tar.gz:http://www.net-snmp.org/download.html

1) 安装setuptools

1.   chmod +x setuptools-0.6c11-py2.7.egg
2.   ./setuptools-0.6c11-py2.7.egg


2)安装net-snmp

1.   tar -zxvf net-snmp-5.7.3.tar.gz
2.   cd net-snmp-5.7.3
3.   ./configure --with-python-modules
如果想指定安装目录,则需要使用--prefix参数,如:
./configure --prefix=/usr/local/netsnmp --with-python-modules
命令敲完回车,生成makefile过程中,会让用户填写相关信息,其中第一个参数:
Default version of SNMP to use (3): 2
注:此处默认SNMP版本填2,不要填2c,因为2就代表2c,如果输入2c,后期编译会报错。(如果直接按回车,不填写任何数字,后面编译也会报错。后面makefile过程中的提示用户填写的参数,如果不想指定,直接按回车即可。)
4.   make (时间很长)
5.   make install
完成安装之后,就可以试用snmpwalk等工具了。但python中仍无法导入netsnmp模块。下面继续:
3)安装python netsnmp模块
上面的netsnmp安装时未手动指定目录,所以默认安装在/usr/local下了。对应的库文件在/usr/local/lib目录下。
在安装python netsnmp模块时,因模块安装需要,需先增加库文件路径,否则会报错,提示找不到模块: ImportError: libnetsnmp.so.30: cannot open shared object file: No such file or directory
1.   echo "/usr/local/lib" >> /etc/ld.so.conf
2.   ldconfig
3.   cd python
4.   python setup.py build
5.   python setup.py test
出现如下图所示则说明编译测试成功:

服务器版Linux最后可能会出现如下图所示的编码问题,可以忽略:

6.   python setup.py install
4)测试导入python netsnmp模块

猜你喜欢

转载自www.cnblogs.com/zhukaijian/p/13203315.html