背景:
笔者在尝试用Zabbix 5.0自定义监控项监控网络设备时遇到一些问题,现在尝试用Python脚本来实现功能。
本篇文章参照CentOS7下安装python3.8 - xiejava - 博客园进行Python环境的部署,参考王印老师的《网络工程师的Python之路——网络运维自动化实战》安装第三方模块Paramiko与Netmiko。
感谢笔耕不辍的布道者。
此篇文章做笔记备忘。
——————————
本人环境版本:
[root@zabbix-server ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
Centos7自带Python2,查看如下:
[root@zabbix-server ~]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
如上,为2.7.5版本,但笔者需要3以上版本进行学习。
安装依赖:
[root@zabbix-server ~]# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
[root@zabbix-server ~]# yum install libffi-devel -y
下载、安装Python源码包:
[root@zabbix-server ~]# wget https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tgz
//下载
[root@zabbix-server ~]# tar -zxvf Python-3.8.12.tgz
//解压
[root@zabbix-server ~]# cd Python-3.8.12/
[root@zabbix-server Python-3.8.12]#
//进入解压目录编译并安装
[root@zabbix-server Python-3.8.12]# ./configure
[root@zabbix-server Python-3.8.12]# make&&make install
--------
......
Looking in links: /tmp/tmp7mebzoh2
Processing /tmp/tmp7mebzoh2/setuptools-56.0.0-py3-none-any.whl
Processing /tmp/tmp7mebzoh2/pip-21.1.1-py3-none-any.whl
Installing collected packages: setuptools, pip
Successfully installed pip-21.1.1 setuptools-56.0.0
--------
建立软链接:
Python软链接修改:
[root@zabbix-server Python-3.8.12]# python -V
Python 2.7.5
//当前仍默认显示2.7.5版本
[root@zabbix-server Python-3.8.12]# which python
/usr/bin/python
[root@zabbix-server Python-3.8.12]# which python3
/usr/local/bin/python3
//查看默认版本与新版本的路径
[root@zabbix-server Python-3.8.12]# mv /usr/bin/python /usr/bin/python.bak
[root@zabbix-server Python-3.8.12]# ln -s /usr/local/bin/python3 /usr/bin/python
//将Python3的路径添加至Python2上
[root@zabbix-server Python-3.8.12]# python -V
Python 3.8.12
//再次查看版本,已经是3.8.12
Pip软链接修改:
[root@zabbix-server Python-3.8.12]#which pip3
/usr/local/bin/pip3
[root@zabbix-server Python-3.8.12]# ln -s /usr/local/bin/pip3 /usr/bin/pip
//建立pip到pip3的软链接
[root@zabbix-server Python-3.8.12]# pip -V
--------
pip 21.1.1 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)
--------
//CentOS7的默认Python版本(2.7)没有安装Pip
配置Yum:
[root@zabbix-server Python-3.8.12]# vim /usr/libexec/urlgrabber-ext-down
//进入文件后将第一行'#! /usr/bin/python'修改为'#! /usr/bin/python2'
[root@zabbix-server Python-3.8.12]# vim /usr/bin/yum
//进入文件后将第一行'#! /usr/bin/python'修改为'#! /usr/bin/python2'
“对于网络工程师来说,最常用的Python第三方模块无疑是用来SSH登录网络设备的Paramiko和Netmiko。首先使用命令pip3.8 install Paramiko和pip .8 instal Netmiko来分别安装它们。”
(王印老师《网络工程师的Python之路——网络运维自动化实战》,P98)
先安装Paramiko内部依赖的组件Pycrypto:
[root@zabbix-server ~]# pip3.8 install pycrypto
--------
Successfully installed pycrypto-2.6.1
WARNING: Running pip as root will break packages and permissions. You should install packages reliably by using venv: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 21.1.1; however, version 22.0.4 is available.
You should consider upgrading via the '/usr/local/bin/python3.8 -m pip install --upgrade pip' command.
--------
如上,有2个告警。解决两种Warning:
通过建立Python虚拟环境以解决:
[root@zabbix-server ~]# python3 -m venv tutorial-env
[root@zabbix-server ~]# source tutorial-env/bin/activate
(tutorial-env) [root@zabbix-server ~]# pip3.8 install pycrypto
--------
Successfully installed pycrypto-2.6.1
WARNING: You are using pip version 21.1.1; however, version 22.0.4 is available.
You should consider upgrading via the '/root/tutorial-env/bin/python3 -m pip install --upgrade pip' command.
--------
//如上,关于'Running pip as root will...'的Warning已经没有了
(tutorial-env) [root@zabbix-server ~]# /root/tutorial-env/bin/python3 -m pip install --upgrade pip
--------
equirement already satisfied: pip in ./tutorial-env/lib/python3.8/site-packages (22.0.4)
--------
//通过执行Warning分号里的内容更新pip
再次逐一安装组件:
(tutorial-env) [root@zabbix-server ~]# pip3.8 install pycrypto
Requirement already satisfied: pycrypto in ./tutorial-env/lib/python3.8/site-packages (2.6.1)
(tutorial-env) [root@zabbix-server ~]# pip3.8 install Paramiko
(tutorial-env) [root@zabbix-server ~]# pip3.8 install Netmiko
验证:
(tutorial-env) [root@zabbix-server ~]# python
Python 3.8.12 (default, May 8 2022, 00:35:13)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
/root/tutorial-env/lib/python3.8/site-packages/paramiko/transport.py:236: CryptographyDeprecationWarning: Blowfish has been deprecated
"class": algorithms.Blowfish,
>>> import netmiko
>>>
//如上,没有报错,说明安装成功
注:退出虚拟环境命令如下:
(tutorial-env) [root@zabbix-server ~]# deactivate