Newbie has rough time with ready to use code

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 1384 Views - Last Post: 25 October 2017 - 12:45 PM Rate Topic: -----

#1 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Newbie has rough time with ready to use code

Posted 19 September 2017 - 07:52 AM

I am trying to work with SNMP and write a program. I have a new professional satellite receiver at work and decided to contro it and take measurements via SNMP. I started searching the Internet, found material to study, searched various web pages etc.

For some reason, there was not much. However, I found something useful. There are a few dll files and code examples. I decided to leave VB and start with C#. That means that I have to learn C# (I started watching video in youtube).

Anyway, in the end I managed to send GET requests. Take a look at the following web page.

https://www.snmpshar...om/?page_id=111

The only thing I had to do was to change the IP address. I changed it from 127.0.0.1 to 192.168.192.21 (the IP of the satellite receiver) and it did work. I also changed the last OID (1.3.6.1.2.1.1.5.0) to an OID of the satellite receiver and strted getting measurements.

The next step was the SET requests. I used the code of the following page.
https://www.snmpshar...om/?page_id=114

I suppose that it's the following part of the code that has to be changed.
 // Prepare target
UdpTarget target = new UdpTarget((IPAddress)new IpAddress("some-host-name"));


I changed it as follows.
// Prepare target
UdpTarget target = new UdpTarget(("192.168.192.21")new IpAddress("some-host-name"));  //Original// UdpTarget target = new UdpTarget((IPAddress)new IpAddress("some-host-name"));


Unfortunately, I got the follwoing messages.
Posted Image

Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: Newbie has rough time with ready to use code

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,141
  • Joined: 05-May 12

Re: Newbie has rough time with ready to use code

Posted 19 September 2017 - 09:37 AM

You needed to replace the string "some-host-name", not the class IPAddress.

I don't understand why you even need the cast. Calling new IPAddress() will return an IPAddress. No need to cast.
Was This Post Helpful? 0
  • +
  • -

#3 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 20 September 2017 - 11:09 AM

View PostSkydiver, on 19 September 2017 - 07:37 PM, said:

You needed to replace the string "some-host-name", not the class IPAddress.


Do you mean this?
UdpTarget target = new UdpTarget((IPAddress)new 192.168.192.21("some-host-name"));


It still does not work.

Posted Image

I used double quota but I failed again.
UdpTarget target = new UdpTarget((IPAddress)new "192.168.192.21"("some-host-name"));

Posted Image


View PostSkydiver, on 19 September 2017 - 07:37 PM, said:

I don't understand why you even need the cast. Calling new IPAddress() will return an IPAddress. No need to cast.
I do not understand what you are talking about.
I am a newbie, the code was taken from a webpage.
Was This Post Helpful? 0
  • +
  • -

#4 eazyyzae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-February 13

Re: Newbie has rough time with ready to use code

Posted 20 September 2017 - 12:07 PM

View PostSkydiver, on 19 September 2017 - 09:37 AM, said:

You needed to replace the string "some-host-name", not the class IPAddress.

I don't understand why you even need the cast. Calling new IPAddress() will return an IPAddress. No need to cast.


I think

UdpTarget target = new UdpTarget((IPAddress)new IpAddress("192.168.x.x"));

obv not x.x then numbers I can't remember right now but I think thats what it is.... Maybe
Was This Post Helpful? 0
  • +
  • -

#5 eazyyzae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-February 13

Re: Newbie has rough time with ready to use code

Posted 20 September 2017 - 12:18 PM

Maybe hostname like "google.com" lol I should never have posted that I don't know :sleep1:
Was This Post Helpful? 0
  • +
  • -

#6 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 25 September 2017 - 03:40 PM

I did it! Don't ask me how,haven't understood. :bananaman: :bananaman:

The original code is in the following link.
https://www.snmpshar...om/?page_id=114

The final code is as follows.

using System;
using SnmpSharpNet;
using System.Net;

namespace CSharpSet
{
    class Program
    {
        static void Main(string[] args)
        {
            IpAddress ipaddr = new IpAddress("192.168.192.21");
            Console.WriteLine("IpAddress: {0}", ipaddr);
            Console.ReadLine();
            
            // Prepare target
            UdpTarget target = new UdpTarget((IPAddress)new IpAddress(ipaddr));  //Original// UdpTarget target = new UdpTarget((IPAddress)new IpAddress("some-host-name"));
            // Create a SET PDU
            Pdu pdu = new Pdu(PduType.Set);
            ///// Set sysLocation.0 to a new string
            ///pdu.VbList.Add(new Oid("1.3.6.1.2.1.1.6.0"), new OctetString("Some other value"));
            ///// Set a value to integer
            ///pdu.VbList.Add(new Oid("1.3.6.1.2.1.67.1.1.1.1.5.0"), new Integer32(500));
            // Set a value to unsigned integer
            pdu.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(10600000));
            // Set Agent security parameters
            AgentParameters aparam = new AgentParameters(SnmpVersion.Ver2, new OctetString("private"));
            // Response packet
            SnmpV2Packet response;
            try
            {
                // Send request and wait for response
                response = target.Request(pdu, aparam) as SnmpV2Packet;
            }
            catch (Exception ex)
            {
                // If exception happens, it will be returned here
                Console.WriteLine(String.Format("Request failed with exception: {0}", ex.Message));
                target.Close();
                Console.WriteLine("Checkpoint one");
                Console.ReadLine();
                return;
            }
            // Make sure we received a response
            if (response == null)
            {
                Console.WriteLine("Error in sending SNMP request.");
                Console.WriteLine("Checkpoint two");
                Console.ReadLine();
            }
            else
            {
                // Check if we received an SNMP error from the agent
                if (response.Pdu.ErrorStatus != 0)
                {
                    Console.WriteLine(String.Format("SNMP agent returned ErrorStatus {0} on index {1}",
                      response.Pdu.ErrorStatus, response.Pdu.ErrorIndex));
                    Console.WriteLine("Checkpoint three");
                    Console.ReadLine();
                }
                else
                {
                    // Everything is ok. Agent will return the new value for the OID we changed
                    Console.WriteLine(String.Format("Agent response {0}: {1}",
                      response.Pdu[0].Oid.ToString(), response.Pdu[0].Value.ToString()));
                    Console.WriteLine("Checkpoint four");
                    Console.ReadLine();
                }
            }
            Console.ReadLine();
            
        }
    }
}


The result.
Posted Image
Was This Post Helpful? 0
  • +
  • -

#7 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 594
  • View blog
  • Posts: 2,492
  • Joined: 13-June 14

Re: Newbie has rough time with ready to use code

Posted 26 September 2017 - 10:43 AM

View PostArchangelos, on 25 September 2017 - 11:40 PM, said:

I did it! Don't ask me how,haven't understood. :bananaman:/> :bananaman:/>


Then please ask questions about what it is you don't understand. I think it's important to understand what you're doing. The attempts you made earlier on in this thread suggest some lack of understanding of classes and types generally, so it would be a good idea to sort that out.
Was This Post Helpful? 0
  • +
  • -

#8 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 26 September 2017 - 11:11 AM

View Postndc85430, on 26 September 2017 - 08:43 PM, said:

The attempts you made earlier on in this thread suggest some lack of understanding of classes and types generally, so it would be a good idea to sort that out.
To be honest, it's the first time I work with C#. I would prefer to do it in VB. However, it was easier to try SNMP with C#.

I have started my homework via youtube. Although I have a little experience in programming I decided to start from lesson 1, hello world, variables etc.


View Postndc85430, on 26 September 2017 - 08:43 PM, said:

I think it's important to understand what you're doing.
Well, I was not totally clueless otherwise I would not succeed even in this small step.

There is something I do not get.
This code did not work, there were error messages.

UdpTarget target = new UdpTarget((IPAddress)new IpAddress("192.168.192.21")); 


However, the following worked.
IpAddress ipaddr = new IpAddress("192.168.192.21");
UdpTarget target = new UdpTarget((IPAddress)new IpAddress(ipaddr)); 


I do not get it. I could not use the string directly into the UDPtarget command but I had to assign the string value to a variable and use the variable into the UDPtarget.

Prior to my succeess I had even used the IP in integer format.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6380
  • View blog
  • Posts: 25,779
  • Joined: 12-December 12

Re: Newbie has rough time with ready to use code

Posted 26 September 2017 - 11:14 AM

What was the error message?
Was This Post Helpful? 0
  • +
  • -

#10 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 27 September 2017 - 02:23 AM

View Postandrewsw, on 26 September 2017 - 09:14 PM, said:

What was the error message?
I wish I knew. I read your message and I attempted to reconstruct the problem.

I changed the code but it worked.Posted Image

The following worked.
IpAddress ipaddr = new IpAddress("192.168.192.21");
UdpTarget target = new UdpTarget((IPAddress)new IpAddress(ipaddr));



I was expecting the following not to work. I did the change in order to answer to the question of andrewsw (the error message).
UdpTarget target = new UdpTarget((IPAddress)new IpAddress("192.168.192.21"));

It worked.Posted Image I can't understand.

PS: I am a newbie, not an idiot.
Was This Post Helpful? 0
  • +
  • -

#11 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 27 September 2017 - 03:32 AM

View PostArchangelos, on 27 September 2017 - 12:23 PM, said:

PS: I am a newbie, not an idiot.
I mean I don't understand how I got so much confused.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6380
  • View blog
  • Posts: 25,779
  • Joined: 12-December 12

Re: Newbie has rough time with ready to use code

Posted 27 September 2017 - 03:56 AM

As you cannot reproduce the error message we'll have to assume that the version of the code that didn't work is somehow different to the version that works, or even that the error was from a preceding line. (It could have been as simple as a typo between IpAddress and IPAddress.)
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,141
  • Joined: 05-May 12

Re: Newbie has rough time with ready to use code

Posted 27 September 2017 - 03:59 AM

Very likely you either had another copy of your code running or you have a slow anti-virus program (*cough* McAfee, *cough* AVG, *cough* Symantec) preventing a newer version of your code from compiling and overwriting the older version. This is why we were asking you what the error was.

Anyway, this code should also compiler without the unnecessary cast:
UdpTarget target = new UdpTarget(new IpAddress("192.168.192.21"));


Was This Post Helpful? 0
  • +
  • -

#14 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 05:47 AM

The whole thing confused me in the begining.



For example, let's say that we need an integer variable named integer1. The syntax (depending on the language) should look like the following:

int integer1 or
Dim integer1 as int

UdpTarget target = new UdpTarget((IPAddress)new IpAddress("192.168.192.21"));

target is of type UDPtarget and the word UDPtarget has to be written twice. I suppose there is a reason in this, a reason I still do not know.

Anyway, I have done one more step. I reduced the size of the code by removing all the error handling stuff. I know, wasn't the best choice, I have to deal with the error handling later.

For now, I have a small piece of code that works. I changed the names of the variables and it still works.
            UdpTarget MRD4400IP = new UdpTarget((IPAddress)new IpAddress("192.168.192.21"));
            Pdu pduMRD4400 = new Pdu(PduType.Set);
            SnmpV2Packet response;
            AgentParameters MRD4400param = new AgentParameters(SnmpVersion.Ver2, new OctetString("private"));
            

            //SET command
            //.1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1: dvbs2xinConnLoOffset (OID 355)
            pduMRD4400.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(10600000));
            response = MRD4400IP.Request(pduMRD4400, MRD4400param) as SnmpV2Packet;
            Console.WriteLine(String.Format("Agent response {0}: {1}",
                     response.Pdu[0].Oid.ToString(), response.Pdu[0].Value.ToString()));
            Console.ReadLine();

Was This Post Helpful? 0
  • +
  • -

#15 Archangelos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 22-August 17

Re: Newbie has rough time with ready to use code

Posted 23 October 2017 - 05:56 AM

I have a question.

Take a look at the following.

pduMRD4400.VbList.Add(new Oid(".1.3.6.1.4.1.9986.3.19.1.2.27.1.4.1.8.2.1"), new Integer32(10600000));
response = MRD4400IP.Request(pduMRD4400, MRD4400param) as SnmpV2Packet;


The pduMRD4400 seems to have a structure similar to an array. In the initial code, the .VbList.Add is used many times and the response command is executed once. I suppose it's the only way this thing can work.

Have I understood right? If yes, is there any way to empty this array?

PS: someone may tell me about the RTFM method (Read The F****** Manual). Well, I am trying to do so in order not to be much disturbing. I visited the website of the developer of the snmp module, I wandered around the API pages, I searched about the PDU thing but I could not understand much.

Look what I found.
SNMP PDU class that is the bases for all SNMP requests and replies. It is capable of processing SNMPv1 GET, GET-NEXT, REPLY and SNMPv2 GET, GET-NEXT, GET-BULK, REPLY, V2TRAP, INFORM and REPORT PDUs.


Pdu pdu = new Pdu();
pdu.Type = PduType.Get;
pdu.VbList.AddVb("1.3.6.1.2.1.1.1.0");
pdu.VbList.AddVb("1.3.6.1.2.1.1.2.0");

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3