SNMPv3/pygal制图/smtplib发邮件
SNMPv3
- SNMPv3在路由器端的配置
这个我都没配置过,还得现学现卖
这个链接说的是SNMPv3的基本配置
这个链接说的是SNMPv3的view命令,用于管理员可以看到哪些层级的内容
1) 配置
snmp-server group READONLY v3 priv read VIEWSTD access 98
snmp-server view VIEWSTD iso included
snmp-server community galileo RO 98
snmp-server user <user> READONLY v3 auth sha <password> priv aes 128 <key>
上面的命令的意思是,snmp-server view VIEWSTD iso included是创建一个view, 这个view里只能看iso级和iso级以下的(include),创建了一个group叫READONLY, 版本v3,权限是读,VIEWSTD是和刚刚的view设置绑定, access 98是调用名字为98的ACL做访问控制。最后是创建一个v3独有的user,user有名字,也有认证用的password,也有加密用的password,把它绑到READONLY组
2) 注意snmp-server user 命令打完之后,在running config里是看不到的,用以下命令看
router1#sho snmp user
User name: pysnmp
Engine ID: 8000000903001C6A7AAF5768
storage-type: nonvolatile active
Authentication Protocol: SHA
Privacy Protocol: AES128
Group-name: READONLY
- snmp_helper.snmp_get_oid_v3(a_device, snmp_user, oid=the_oid)
1) 变化一
去除了community的赋值,取而代之的是username, auth_key, encrypt_key. 并创建snmp_user元组
2) 变化二
他跟普通的v2版本的snmp_get_oid的最大的不同就是,中间多了一个叫user的参数,也就是刚刚在路由器上的最后一条命令创造的那个user
3) 变化三
a_device 在赋值的时候,以往是a_device = (ip_addr, commu, port), 现在是a_device = (ip_addr, port), 减少了中间的commu
4) 总体的一个代码是这样的
import snmp_helper
'''
router1#sho snmp user
User name: pysnmp
Engine ID: 8000000903001C6A7AAF5768
storage-type: nonvolatile active
Authentication Protocol: SHA
Privacy Protocol: AES128
Group-name: READONLY
'''
OID = '1.3.6.1.2.1.1.5.0'
ip_addr = '184.105.247.70'
port = 161
snmp_user = 'pysnmp'
auth_key = 'galileo1'
encrypt_key = 'galileo1'
a_user = (snmp_user, auth_key, encrypt_key) #组成user元组
a_device = (ip_addr, port) #组成device元组
snmp_data = snmp_helper.snmp_get_oid_v3(a_device, a_user, OID)
output = snmp_helper.snmp_extract(snmp_data)
print output
- 利用snmp获取interface相关数据
通过思科的工具查到和interface description相关的OID是‘1.3.6.1.2.1.2.2.1.2’,再通过snmpwalk命令到具体的设备上去查具体的端口信息
(applied_python)[-class3]$ cat snmp_walk_interface_descrp.txt
(applied_python)[- class3]$ snmpwalk -Os -c galileo -v 1 184.105.247.70 1.3.6.1.2.1.2.2.1.2
ifDescr.1 = STRING: FastEthernet0
ifDescr.2 = STRING: FastEthernet1
ifDescr.3 = STRING: FastEthernet2
ifDescr.4 = STRING: FastEthernet3
ifDescr.5 = STRING: FastEthernet4
ifDescr.6 = STRING: Null0
ifDescr.7 = STRING: Vlan1
ifDescr.11 = STRING: Vlan127
ifDescr.13 = STRING: Vlan157
(applied_python)[class3]$ snmpwalk -Os -c galileo -v 1 184.105.247.70 1.3.6.1.2.1.2.2.1.2.5
ifDescr.5 = STRING: FastEthernet4
(applied_python)[class3]$
- for循环一次读取多个OID
#! /usr/bin/env python
import snmp_helper
snmp_oids = (
('sysName', '1.3.6.1.2.1.1.5.0', None),
('sysUptime', '1.3.6.1.2.1.1.3.0', None),
('ifDescr_fa4', '1.3.6.1.2.1.2.2.1.2.5', None),
('ifInOctets_fa4', '1.3.6.1.2.1.2.2.1.10.5', True),
('ifInUcastPkts_fa4', '1.3.6.1.2.1.2.2.1.11.5', True),
('ifOutOctets_fa4', '1.3.6.1.2.1.2.2.1.16.5', True),
('ifOutUcastPkts_fa4', '1.3.6.1.2.1.2.2.1.17.5', True),
)
ip_addr = '184.105.247.70'
port = 161
snmp_user = 'pysnmp'
auth_key = 'galileo1'
encrypt_key = 'galileo1'
a_user = (snmp_user, auth_key, encrypt_key)
a_router = (ip_addr, port)
for desc, each_oid, is_count in snmp_oids:
snmp_data = snmp_helper.snmp_get_oid_v3(a_router, a_user, oid=each_oid)
output = snmp_helper.snmp_extract(snmp_data)
print "%s %s" % (desc, output)
注意上面这段代码中的for循环中的这个 is_count,虽然他不发挥什么作用,我们在for中并不引用它,但是它也必须在for a,b,c in snmp_oids中出现,因为snmp_oid这个tuple的每个元素,都是三个元素组成的小tuple,如果把is_count去掉,会出现“ValueError: too many values to unpack”
- 每隔一段时间运行一下上面那个for循环
#! /usr/bin/env python
import snmp_helper
import sched, time
from datetime import datetime
snmp_oids = (
('sysName', '1.3.6.1.2.1.1.5.0', None),
('sysUptime', '1.3.6.1.2.1.1.3.0', None),
('ifDescr_fa4', '1.3.6.1.2.1.2.2.1.2.5', None),
('ifInOctets_fa4', '1.3.6.1.2.1.2.2.1.10.5', True),
('ifInUcastPkts_fa4', '1.3.6.1.2.1.2.2.1.11.5', True),
('ifOutOctets_fa4', '1.3.6.1.2.1.2.2.1.16.5', True),
('ifOutUcastPkts_fa4', '1.3.6.1.2.1.2.2.1.17.5', True),
)
ip_addr = '184.105.247.70'
port = 161
snmp_user = 'pysnmp'
auth_key = 'galileo1'
encrypt_key = 'galileo1'
a_user = (snmp_user, auth_key, encrypt_key)
a_router = (ip_addr, port)
#s = sched.scheduler(time.time, time.sleep)
def loop_the_oids(snmp_oids):
for desc, each_oid, is_count in snmp_oids:
snmp_data = snmp_helper.snmp_get_oid_v3(a_router, a_user, oid=each_oid)
output = snmp_helper.snmp_extract(snmp_data)
print "%s %s" % (desc, output)
def print_current_time():
print datetime.now()
for time_range in range(1,13):
print "time %d" % (time_range*10)
loop_the_oids(snmp_oids)
time.sleep(10)
本来是想用 sched来打印时间戳的,但是后来觉得用time.sleep和print一个累加的数字就可以实现下一步要用的横坐标了
Graph from SNMP data
我觉得这有点重复造轮子了,因为这个完全可以通过Cacti实现,而且Cacti实现的又特别好
- pygal
pip install pygal即可
1) 初始化一个object,line_chart = pygal.Line()
2) 初始化横坐标 line_chart.x_labels = [‘0’, ‘10’, ‘20’, ‘40’]
3) 初始化一条数据折线
fa4_in_packets = [] 一个数组,这个数组在视频里Kirk是直接手写的,但是也可以从上一个实验的结果里提取,这也是这次的练习作业,还是有点难度
line_chart.add(‘InPackets’, fa4_in_packets)
4) 保存为svg格式的图片
line_chart.render_to_file(‘test.svg’)
svg格式的图片需要用浏览器打开
Sending Email
- smtplib
python自带的一个库,使用的方式很简单,代码在最后
- from email.mime.text import MIMEText
这个MIMEText是个什么呢,Wikipedia的解释如下
Multipurpose Internet Mail Extensions (MIME) is an Internet standard that extends the format of email to support: Text in character sets other than ASCII. Non-text attachments: audio, video, images, application programs etc.
- 代码部分
这个password要注意,这个是在网易邮箱设置里,找到SMTP相关那一栏,点进去勾选IMAP/SMTP 和 POP3/SMTP的时候,会让你设置一个单独的密码,网易把这个码称为邮箱客户端授权码,在第三方客户端(也就是我们今天的脚本)中发邮件的时候,用这个码代替原来的163邮箱的密码
import smtplib
from email.mime.text import MIMEText
recipient1 = "balabala@163.com"
mail_host1 = "smtp.163.com"
mail_password1 = "*****"
mail_sender1 = "balabala@163.com"
def send_email(recipient, subject, message, mail_sender, mail_host, mail_password):
try:
smtp_conn = smtplib.SMTP()
smtp_conn.connect(mail_host)
smtp_conn.login(mail_sender, mail_password)
print "conn created"
message = MIMEText(message)
message['To'] = recipient
message['Subject'] = subject
message['From'] = mail_sender
smtp_conn.sendmail(mail_sender, recipient, message.as_string())
smtp_conn.close()
print "Your message has been sent"
return True
except Exception, e:
return False
print "oops something wrong"
send_email(recipient1, "Test", "sent by script", mail_sender1, mail_host1, mail_password1)