예제 #1
0
파일: SnmpTest.cs 프로젝트: blinds52/More
        public void ManuallyVerifyErrorMessages()
        {
            Byte temp;

            Byte[] packet = new Byte[256];

            List <Byte> oidList = new List <Byte>();

            Snmp.ParseOid("1.2.3.4.5.6.7", oidList);
            Snmp.SerializeGet(packet, 0, "public", 0, oidList);

            SnmpPacket snmpPacket = new SnmpPacket();

            //
            // Test that this is a valid packet
            //
            snmpPacket.Deserialize(packet, 0);

            //
            // Invalid ASN.1 Type
            //
            temp      = packet[0];
            packet[0] = 0; // Should be ASN.1 Sequence
            try
            {
                snmpPacket.Deserialize(packet, 0);
                Assert.Fail("Expected exception that did not occur");
            }
            catch (FormatException e) { Console.WriteLine("Caught exception for invalid packet type: '{0}'", e.Message); }
            packet[0] = temp;


            //
            // Truncated packet
            //
            Byte[] packetTooSmall = new Byte[10];
            packetTooSmall[0] = Asn1.TYPE_SEQUENCE;
            packetTooSmall[1] = 100;
            try
            {
                snmpPacket.Deserialize(packetTooSmall, 0);
                Assert.Fail("Expected exception that did not occur");
            }
            catch (FormatException e) { Console.WriteLine("Caught exception for truncated packet: '{0}'", e.Message); }

            //
            // Invalid Version Type
            //
            temp      = packet[2];
            packet[2] = Asn1.TYPE_NULL;
            try
            {
                snmpPacket.Deserialize(packet, 0);
                Assert.Fail("Expected exception that did not occur");
            }
            catch (FormatException e) { Console.WriteLine("Caught exception for invalid snmp version type: '{0}'", e.Message); }
            packet[2] = temp;

            //
            // Invalid Community Type
            //
            temp      = packet[5];
            packet[5] = Asn1.TYPE_INTEGER;
            try
            {
                snmpPacket.Deserialize(packet, 0);
                Assert.Fail("Expected exception that did not occur");
            }
            catch (FormatException e) { Console.WriteLine("Caught exception for invalid community type: '{0}'", e.Message); }
            packet[5] = temp;
        }
예제 #2
0
        static Int32 Main(String[] args)
        {
            Options options = new Options();

            List <String> nonOptionArgs = options.Parse(args);

            if (nonOptionArgs.Count == 0)
            {
                options.PrintUsage();
                return(1);
            }

            String command = nonOptionArgs[0];

            if (nonOptionArgs.Count < 3)
            {
                return(options.ErrorAndUsage("Error: Missing command line arguments"));
            }
            String hostString = nonOptionArgs[1];
            String oidString  = nonOptionArgs[2];

            IPEndPoint endPoint = EndPoints.ParseIPOrResolveHostWithOptionalPort(hostString, 161, DnsPriority.IPv4ThenIPv6);

            List <Byte> oidBytes = new List <Byte>();

            Snmp.ParseOid(oidString, oidBytes);
            Byte[] oid = oidBytes.ToArray();

            if (command.Equals("get", StringComparison.CurrentCultureIgnoreCase))
            {
                if (nonOptionArgs.Count != 3)
                {
                    return(options.ErrorAndUsage("Error: operation 'get' requires 2 arguments but there are {0}", nonOptionArgs.Count - 1));
                }

                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                Byte[] packet = new Byte[Snmp.MaximumBufferForGet(
                                             (UInt32)options.community.ArgValue.Length, (UInt32)oid.Length)];

                UInt32 snmpContentLength = Snmp.SerializeGet(packet, 0, options.community.ArgValue, Snmp.NextID(), oid);

                socket.SendTo(packet, (Int32)snmpContentLength, 0, endPoint);
            }
            else if (command.Equals("set", StringComparison.CurrentCultureIgnoreCase))
            {
                if (nonOptionArgs.Count != 5)
                {
                    return(options.ErrorAndUsage("Error: operation 'set' requires 4 arguments but there are {0}", nonOptionArgs.Count - 1));
                }

                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                Byte[] value = Asn1.ParseValue(nonOptionArgs[3], nonOptionArgs[4]);

                Byte[] packet = new Byte[Snmp.MaximumBufferForSet(
                                             (UInt32)options.community.ArgValue.Length, (UInt32)oid.Length, (UInt32)value.Length)];

                UInt32 snmpContentLength = Snmp.SerializeSet(packet, 0, options.community.ArgValue, Snmp.NextID(), oid, value);

                socket.SendTo(packet, (Int32)snmpContentLength, 0, endPoint);
            }
            else
            {
                Console.WriteLine("Error: Unknown operation '{0}'", command);
                return(1);
            }

            return(0);
        }