- 求职 : Linux运维
- 论坛徽章:
- 3
|
本帖最后由 donalds2008 于 2014-12-03 11:26 编辑
1.你是如何学习Python的?看书?看视频?还是?
主要是通过看电子书,所以希望楼主赏本纸质书拜读拜读😊。看过python学习手册,python cookbook。感觉py自带的手册不错,很系统,可惜没时间翻,用到具体模块时翻翻。
2.你工作中有用到Python吗?主要用来做什么?
主要在网络设备资源管理方面写过一些脚本,py里的pexpect、re、mysqldb等模块功能强大,可以实现设备交互、信息提取、结构化存储等功能,快速支撑业务需求。
3.你觉得Python跟其他脚本语言比优势在哪?
对bash,pl,py略懂,也都写过一些工具。个人感觉,py的优势包括:
1)语法简洁。缩进的编码规则使得编码可读性比较高,辅以必要注释,即使较久的脚本也能短时间内进入当时的编码逻辑,加以优化扩展。虽然好的编码规范也能让写出可读性好的pl脚本,但是在先天性方面显然py更佳,也适合团队协作。
2)代码执行效率较高。与bash比,自然有优势。一般开始时侯,主体功能先用bash写完,如果脚本超过100行或者有效率提升、扩展要求,用py重构,特别是一些通用功能以类方式重新整理。
3)自带模块丰富。pl虽有强大的cpan,但是逐一安装模块较麻烦,py则自带许多模块,能省去不少时间。
4.谈谈Python在自动化运维中的应用。
很多开源工具如ganglia,Hadoop都支持py,因此用处广泛。在自动化运维方面,如监控项扩展、报警通知方式、定制报表等方面,都大有发挥空间。
5.给出一个你自己写的Python脚本(最好有详细注释)。
这是我写的一个ETL模块,比较简单,主要用于分析网络设备交互日志,提取关键数据,并入库。
最后面附有模块测试的代码。- #!/usr/bin/python
- """
- Script File: port_etl.py
- Description: The file is a etl module, which used to analyse equiment information, and save this into
- Database.
- Author: Donalds2008
- Last Update: 2014.5.16
- """
- import re, sys, os
- import pexpect, MySQLdb
- class port_etl():
- #构造函数
- def __init__(self, log_file, ip_re, port_re, vlan_re):
- self.log_file = log_file
- if not self.check_file( self.log_file ):
- print( '%s does not exists!!\n' % self.log_file)
- sys.exit()
- #导入文件
- self.raw_file = os.path.splitext( self.log_file )[0]
- self.vlan_file = '%s.vlan' % self.raw_file
-
- #构造正则式
- self.ip_rx = re.compile( ip_re )
- self.port_rx = re.compile( port_re )
- self.vlan_rx = re.compile( vlan_re )
- #检查文件是否存在
- def check_file( self, file ):
- return True if os.path.exists( file ) else False
- #使用正则式匹配,并输出
- def match_write(self):
- print('matching...')
- self.vlan_fh = open( self.vlan_file, 'w' )
- ( ip, port, vlan) = (None, None, None)
- for line in open( self.log_file, 'r' ):
- match = self.ip_rx.search( line )
- if match != None:
- ip = match.group(1)
- match = self.port_rx.search( line )
- if match != None:
- port = match.group(1).replace(',', ' ')
- match = self.vlan_rx.search( line )
- if match != None:
- vlan = match.group(1)
- self.vlan_fh.write( '%s,%s,%s\n' % ( ip, port, vlan ) )
- self.vlan_fh.close()
- print('match finished.')
- #写入数据库
- def db_write(self):
- host_db= '127.0.0.1'
- usr_db = 'usr'
- pwd_db = 'pwd'
- sel_db = 'mydb'
- print('loading...')
- con = MySQLdb.connect( host=host_db, user=usr_db, passwd=pwd_db, db=sel_db)
- cur = con.cursor()
- last_ip = None
- for line in open( self.vlan_file, 'r'):
- line = line.strip('\n')
- (ip, port, vlan) = line.split(',')
- if ip != last_ip:
- cur.execute( "delete from t_ip where ip = '%s'" % ip )
- last_ip = ip
- cur.execute( "insert into t_ip values('%s', '%s', '%s')" % (ip, port, vlan) )
- con.commit()
- cur.close()
- con.close()
- self.vlan_fh.close()
- print('load finished.')
- def __del__(self):
- self.vlan_fh.close()
- #模块测试
- if __name__ == '__main__':
- etl = port_etl( r'/log/a.txt',\
- r'Connected to ([\d\.]+)\.', \
- r'interface xx([\d\/]+)', \
- r'multipvc .+ vlan (\d+)')
- etl.match_write()
- etl.db_write()
- del etl
复制代码 |
|