- vừa được xem lúc

C#

0 0 5

Người đăng: Vũ Bùi

Theo Viblo Asia

using System.Net; using System.Net.NetworkInformation; using SnmpSharpNet; using System.Collections.Generic;

Console.Write("Nhập địa chỉ IP thiết bị: "); string ipAddress = Console.ReadLine();

try { IPAddress ip = IPAddress.Parse(ipAddress); OctetString community = new OctetString("172.20.158.29"); // Thay đổi nếu cần IPEndPoint endPoint = new IPEndPoint(ip, 161);

// Lấy thông tin tên thiết bị và địa chỉ MAC
var pdu = new Pdu(PduType.Get);
pdu.VbList.Add(".1.3.6.1.2.1.1.5.0"); // sysName
pdu.VbList.Add(".1.3.6.1.2.1.2.2.1.6"); // ifPhysAddress var agentParams = new AgentParameters(community);
var target = new UdpTarget((IPAddress)endPoint.Address, endPoint.Port, 2000, 1);
SnmpV2Packet response = (SnmpV2Packet)target.Request(pdu, agentParams); if (response != null && response.Pdu != null)
{ foreach (Vb v in response.Pdu.VbList) { switch (v.Oid.ToString()) { case ".1.3.6.1.2.1.1.5.0": Console.WriteLine("Tên thiết bị: " + v.Value.ToString()); break; case ".1.3.6.1.2.1.2.2.1.6": Console.WriteLine("Địa chỉ MAC: " + v.Value.ToString()); break; } } // Lấy thông tin cổng kết nối (sử dụng SimpleSnmp để Walk) var snmp = new SimpleSnmp(target); Oid rootOid = new Oid(".1.3.6.1.2.1.2.2.1.2"); // ifDescr IDictionary<Oid, AsnType> resultTable = snmp.Walk(rootOid); if (resultTable != null) { foreach (var entry in resultTable) { if (entry.Value != null && entry.Value.GetType() != typeof(SnmpSharpNet.NoSuchInstance)) { Console.WriteLine("Port: " + entry.Key.ToString().Substring(24) + ", Mô tả: " + entry.Value.ToString()); } else { Console.WriteLine("Lỗi khi lấy thông tin cổng: " + entry.Key.ToString().Substring(24)); } } } else { Console.WriteLine("Lỗi khi lấy thông tin cổng."); } // Kiểm tra trạng thái online bool isOnline = CheckOnlineStatus(ipAddress); Console.WriteLine("Trạng thái: " + (isOnline ? "Online" : "Offline")); }
else
{ Console.WriteLine("Không nhận được phản hồi từ thiết bị hoặc PDU null.");
}

} catch (Exception ex) { Console.WriteLine("Lỗi: " + ex.Message); }

static bool CheckOnlineStatus(string ipAddress) { try { using (Ping ping = new Ping()) { PingReply reply = ping.Send(ipAddress); return reply.Status == IPStatus.Success; } } catch { return false; } } erros: Severity Code Description Project File Line Suppression State Details Error CS1503 Argument 1: cannot convert from 'SnmpSharpNet.UdpTarget' to 'string' ComponentConsole Error CS7036 There is no argument given that corresponds to the required parameter 'rootOid' of 'SimpleSnmp.Walk(SnmpVersion, string)' ComponentConsole

Bình luận

Bài viết tương tự

- vừa được xem lúc

Các loại tham chiếu Nullable trong C# (Phần 1)

1. Giới thiệu. C# 8.0 giới thiệu kiểu tham chiếu nullable và kiểu tham chiếu non-nullable cho phép bạn đưa ra các lựa chọn quan trọng về thuộc tính cho các biến kiểu tham chiếu:.

0 0 46

- vừa được xem lúc

Command pattern qua ví dụ !

Command pattern là gì . Command pattern khá phổ biến trong C #, đặc biệt khi chúng ta muốn trì hoãn hoặc xếp hàng đợi việc thực hiện một yêu cầu hoặc khi chúng ta muốn theo dõi các hoạt động. Hơn nữa, chúng ta có thể hoàn tác tác chúng. .

0 0 187

- vừa được xem lúc

Hiểu Liskov Substitution Principle qua ví dụ !

Liskov Substitution Principle là gì . Nguyên tắc đóng mở xác đinh rằng các instance của lớp con có thể thay thế được instance lớp cha mà vẫn đảm bảo tính đúng đắn của chương trình.

0 0 30

- vừa được xem lúc

Creating custom Controls Wpf

Introduction. Wpf/winforms provides various controls like Buttons, Textbox, TextBlock, Labels etc.

0 0 50

- vừa được xem lúc

[P1] Chọn công nghệ nào để xây dựng website?

Hiện nay nhu cầu phát triển website, app tăng rất cao do xu hướng "số hóa 4.0" trong và ngoài nước.

0 0 80

- vừa được xem lúc

Kiểu dữ liệu trong C#

Dẫn nhập. Ở bài BIẾN TRONG C# chúng ta đã tìm hiểu về biến và có một thành phần không thể thiếu khi khai báo biến – Đó là kiểu dữ liệu.

0 0 30