首页 技术 正文
技术 2022年11月7日
0 收藏 793 点赞 315 浏览 3602 个字

<!–
.h2cls {
background: #6fa833 none repeat scroll 0 0 !important;
border-radius: 5px !important;
color: #fff;
font-family: “微软雅黑”,”宋体”,”黑体”,Arial;
margin-bottom: 5px;
padding-left: 15px;
text-shadow: 2px 2px 3px #222;
}
h3 {
background-color: #f5f5f5;
border-left: 13px solid #6fa833;
color: #6fa833;
padding: 5px;
margin: 15px auto 2px;
}
p {
margin: 10px auto;
text-indent: 0;
}
code {
background-color: #f5f5f5 !important;
border: 1px solid #ccc !important;
border-radius: 3px !important;
display: inline-block;
font-family: “Courier New”,sans-serif !important;
font-size: 12px !important;
height: 20px;
line-height: 1.8;
margin: 0 5px;
padding: 0 5px !important;
vertical-align: middle;
}
–>

Snmp协议应用-扫描局域网内打印机

Snmp协议简单介绍

简单网络管理协议(SNMP),由一组网络管理的标准组成,包含一个应用层协议(application layer protocol)、数据库模型(database schema)和一组资源对象。该协议能够支持网络管理系统,用以监测连接到网络上的设备是否有任何引起管理上关注的情况。该协议是互联网工程工作小组(IETF,Internet Engineering Task Force)定义的internet协议簇的一部分。SNMP的目标是管理互联网Internet上众多厂家生产的软硬件平台,因此SNMP受Internet标准网络管理框架的影响也很大。SNMP已经出到第三个版本的协议,其功能较以前已经大大地加强和改进了。

详细信息可网上查找,参考http://www.cnblogs.com/zhangsf/archive/2013/08/26/3283124.html

代码部分

代码在Linux虚拟机(1cpu、1GB内存)上运行较慢,将for替换为Parallel后速度有所提升。

public class ScanIpAddress
{
CountdownEvent countdown = null;
int region = ;
string[] oids = null;
public Dictionary<string, List<string>> dicOid = null;
public ScanIpAddress(int region,string[] oids)
{
this.region = region;
this.oids = oids;
this.dicOid = new Dictionary<string, List<string>>();
}
private bool getSnmp(string host)
{
bool result = false;
/* Get an SNMP Object
*/
SimpleSnmp snmpVerb = new SimpleSnmp(host, , "public", , );
if (!snmpVerb.Valid)
{
Console.WriteLine("Seems that IP or comunauty is not cool");
return result;
} //Oid varbind = new Oid(OID); Dictionary<Oid, AsnType> snmpDataS = snmpVerb.Get(SnmpVersion.Ver1, this.oids);
if (snmpDataS != null)
{
List<string> oidList = new List<string> ();
foreach (var item in snmpDataS)
{
oidList.Add(item.Value.ToString());
}
this.dicOid.Add(host, oidList);
result = true;
} return result;
} private void p_PingCompleted(object sender, PingCompletedEventArgs e)
{
string ip = (string)e.UserState;
if (e.Reply != null && e.Reply.Status == IPStatus.Success)
{
/* PRINTER-PORT-MONITOR-MIB - 1.3.6.1.4.1.2699
* ppmPrinterIEEE1284DeviceId: 1.3.6.1.4.1.2699.1.2.1.2.1.1.3
*/
getSnmp(ip);
}
else if (e.Reply == null)
{
Console.WriteLine("Pinging {0} failed. (Null Reply object?)", ip);
}
countdown.Signal();
} public void ScanPrinters()
{
Console.WriteLine("ScanPrinters");
countdown = new CountdownEvent();
Stopwatch sw = new Stopwatch();
sw.Start();
string ipBase = "192.168.{0}.{1}";
Parallel.For(, , (i) =>
{
string ip = string.Format(ipBase, region, i);
Console.WriteLine(ip);
Ping p = new Ping();
p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
countdown.AddCount();
p.SendAsync(ip, , ip);
});
//for (int i = 0; i <= 255; i++)
//{
// string ip = string.Format(ipBase, region, i);
// Console.WriteLine(ip);
// Ping p = new Ping();
// p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
// countdown.AddCount();
// p.SendAsync(ip, 100, ip);
//}
countdown.Signal();
countdown.Wait();
sw.Stop();
Console.WriteLine("共耗时(毫秒):" + sw.ElapsedMilliseconds);
Console.WriteLine("Printer scan finished");
}
}

调用部分:

    class Program
{
public static string oIDProductName = "1.3.6.1.2.1.25.3.2.1.3.1";// "1.3.6.1.2.1.43.5.1.1.16.1";
public static string oIDSerialNumber = "1.3.6.1.2.1.43.5.1.1.17.1";
static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add(oIDProductName);
list.Add(oIDSerialNumber); ScanIpAddress sip = new ScanIpAddress(, list.ToArray());
sip.ScanPrinters();
if (sip.dicOid != null && sip.dicOid.Count > )
{
foreach (var item in sip.dicOid)
{
Console.WriteLine(item.Key);
List<string> oidList = item.Value;
string printName = string.Empty;
foreach (var str in oidList)
{
printName += str.Replace(" ", "");
Console.WriteLine(str);
}
string [] arr = item.Key.Split('.');
//CupsPrint.AddPrint(arr[2] + arr[3], item.Key);
}
}
Console.ReadLine();
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,906
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727