免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1243 | 回复: 0
打印 上一主题 下一主题

SNMP入门 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-29 10:51 |只看该作者 |倒序浏览
SNMP入门
作者:Cameron Laird
整理:NSFocus Security Team
出处:siteupdate.magazine
主页:
http://www.nsfocus.com
日期:2002-04-12
☆ 谢谢 stardust  提供的测试环境以及在Perl脚本上的帮
   助
☆ SNMP的历史
SNMP是简单网络管理协议的缩写。
上个世纪80年代中期,网络管理通常使用ping、traceroute、tcpdump以及类似的工
具。当时绝大多数网络规模很小,远没有后来变得那么复杂。1987年, Request For
Comments (RFC) 1024提出了"Simple Gateway Monitoring Protocol"(SGMP)。SGMP
定义了一种平台无关的标准,用于监视网关的状态。大约两年后,RFC 1067提出了
SNMP。
☆ SNMP术语和概念
ASN.1: 一个SNMP消息通过抽象语法记法1(Abstract Syntax Notation, version 1)
       构造。ASN.1是一种形式语言。
BER  : 将ASN.1描述表示成octet strings的基本编码规则
MIB  : 管理信息库。下面是一个例子
        sysUpTime OBJECT-TYPE
            SYNTAX  TimeTicks
            ACCESS  read-only
            STATUS  mandatory
            DESCRIPTION
                "距最后一次系统重启动的时间"
OID  : 对象标识符是唯一的点分数字串,比如".1.3.6.1.2.1.4.3.0"
PDU  : 协议数据单元是SNMP消息中的数据区。
SMI  : 管理信息结构是一套描述SNMP如何访问信息的标准,它用ASN.1定义了MIB。
       SMI要求每个数据项有name、syntax和encoding三部分。名字就是OID。语法
       给出了数据类型,比如"integer"、"string of octets"等等。编码则指明为
       了进行平台无关网络转输如何进行数据的序列化(RPC/XDR中存在类似概念)。
☆ SNMP基础
SNMP是种网络设备之间客户机/服务器模式的简单通信协议。路由器、交换机、打印
机、HUB等等都可以成为SNMP系统中的服务器方。而SNMP系统中的客户机方往往是单
独的一台计算机,轮询网络设备并记录它们所返回的数据。这里允许一台服务器多个
客户机的情形。
SNMP允许你用很少的网络带宽和内存收集很多有用的系统、网络数据。
SNMP提供了一种统一的、跨平台的设备管理办法。
☆ NET-SNMP
可以从
http://net-snmp.sourceforge.net/
获取NET-SNMP的RPM包以及源代码。下载
解压后
su -
cd ucd-snmp-4.2.3
./configure --prefix=/usr   public system.sysDescr.0
应该可以看到一个关于系统的简短描述,类似这样
system.sysDescr.0 = Sun SNMP Agent, Ultra-60
上述命令中的public可以理解为SNMP agent的口令,术语叫做"community string"。
许多网络设备、操作系统都用"public"做为缺省"community string",潜在带来安全
问题。应该修改这个缺省"community string"。
上述命令还可以写成
snmpget  public .1.3.6.1.2.1.1.1.0
"system.sysDescr.0"只是".1.3.6.1.2.1.1.1.0"的另一种表述方式,最终还是要转
换成数字形式的OID(对象标识符)。类似FQDN和IP地址的关系。
DNS系统负责FQDN与IP之间的转换,SNMP通过管理信息库(MIB)完成这种转换。MIB是
一个文本文件,包含很多SNMP相关信息,其中包括OID转换信息。MIBs有公用的,也
有专用的。RFC 1213介绍了sysDescr和许多其它常见OIDs,比如sysUpTime(距最后一
次系统重启动的时间)和sysLocation(设备所在物理位置).
可以用如下命令检查MIBs
snmptranslate -Td -OS system.sysDescr.0
--------------------------------------------------------------------------
.1.3.6.1.2.1.1.1.0
sysDescr OBJECT-TYPE
  -- FROM       SNMPv2-MIB, RFC1213-MIB
  -- TEXTUAL CONVENTION DisplayString
  SYNTAX        OCTET STRING (0..255)
  DISPLAY-HINT  "255a"
  MAX-ACCESS    read-only
  STATUS        current
  DESCRIPTION   "A textual description of the entity.  This value should
            include the full name and version identification of the
            system's hardware type, software operating-system, and
            networking software."
::= { iso(1) org(3) dod(6) internet(1) mgmt(2) mib-2(1) system(1) sysDescr(1) 0 }
--------------------------------------------------------------------------
snmptranslate也可以完成OID到文本描述串的转换,比如
snmptranslate -Td -OS .1.3.6.1.2.1.1.1.0
在一台x86/FreeBSD 4.3-RELEASE上
snmpwalk localhost public interfaces.ifTable.ifEntry.ifDescr
snmpwalk localhost public .1.3.6.1.2.1.2.2.1.2
--------------------------------------------------------------------------
interfaces.ifTable.ifEntry.ifDescr.1 = fxp0
interfaces.ifTable.ifEntry.ifDescr.2 = fxp1
interfaces.ifTable.ifEntry.ifDescr.3 = fxp2
interfaces.ifTable.ifEntry.ifDescr.4 = lo0
--------------------------------------------------------------------------
这个显示顺序与/sbin/ifconfig -l看到的一致。
snmptranslate -Td -OS interfaces.ifTable.ifEntry.ifDescr.1
--------------------------------------------------------------------------
.1.3.6.1.2.1.2.2.1.2.1
ifDescr OBJECT-TYPE
  -- FROM       IF-MIB, RFC1213-MIB
  -- TEXTUAL CONVENTION DisplayString
  SYNTAX        OCTET STRING (0..255)
  DISPLAY-HINT  "255a"
  MAX-ACCESS    read-only
  STATUS        current
  DESCRIPTION   "A textual string containing information about the
            interface.  This string should include the name of the
            manufacturer, the product name and the version of the
            interface hardware/software."
::= { iso(1) org(3) dod(6) internet(1) mgmt(2) mib-2(1) interfaces(2) ifTable(2) ifEntry(1) ifDescr(2) 1 }
--------------------------------------------------------------------------
☆ SNMP编程
NET-SNMP提供了支持SNMP开发的C库,你可以开发自己的SNMP应用程序。然而从Perl、
Python或者Tcl这样的脚本语言开始SNMP编程更容易,它们都带有SNMP开发支持。
--------------------------------------------------------------------------
#! /usr/local/bin/perl
#
# File   : list_if.pl
# Test   : SPARC/Solaris 7
# Author : Laurent Girod  / Philip Morris Products S.A. / Neuchatel / Switzerland
#
use BER;
use SNMP_util "0.92";
# Uptime in absolute value
$BER::pretty_print_timeticks = 0;
my $host = shift @ARGV || &usage;
my $community = shift @ARGV || 'public';
&usage if $#ARGV >= 0;
$host = "$community\@$host" if !( $host =~ /\@/ );
my $oid_name = 'interfaces.ifTable.ifEntry.ifDescr';
print "\nCollecting [$oid_name]\n";
@ret = &snmpwalk( $host, $oid_name );
foreach $desc ( @ret )
{
    ( $oid, $desc ) = split( ':', $desc, 2 );
    print "$oid = $desc\n";
}
sub usage
{
    die "Usage: $0 hostname [community]\n";
}
--------------------------------------------------------------------------
注: 为了运行这个Perl程序,可能你需要下载安装SNMP_Session-0.92.tar.gz以获取
    Perl扩展模块SNMP_util.pm。
这个脚本列举指定主机的网络接口,与/sbin/ifconfig -a看到的顺序一致
# ./list_if.pl public@localhost  
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/95634/showart_2011802.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP