Get Target Ip Address From SNMP4j Trap Receiver - java

I create snmp trap receiver using snmp4j. I managed to receive the pdu and process it. I need the targeted ip address as my machine have multiple ip addresses. I could thinking either two ways:
Retrieve entire command in my trap receiver which is "snmpset -v 1 -c M xx.xxx.x.x 1.3.6.1.4.1.161.3.6.37.2.1.3.2 i 2" so that I can get the ip address by removing other strings." But the only thing I could retrieve is only the variable, value and pdu type.
Using method to retrieve the targeted ip address. But couldn't find one. I used event.getTransportMapping().getListenAddress() but getting 0.0.0.0/161 as I need to listen to many ip address. Used event.getPeerAddress() but getting ip address of the sender's machine.
Really appreciate for any kind of help. Thanks in advance.

Solved by:
UdpAddress udpHostAdress = (UdpAddress) cmdRespEvent.getTransportMapping().getListenAddress();
String ipAdd = udpHostAdress.getInetAddress().getHostAddress();
System.out.println("Target IP = " + ipAdd);

If you are trying to handle security of the trap receiver, you are wrong here use snmp v3 to handle this. v3 has security mechanism called Engine ID.

Related

Port-forwarding a Multicast IP

I've spent some time learning about UDP connections, particularly with Multicast Sockets in Java.
I was able to make a simple Multicast Socket "group chat" on my local network, but I've since been trying to expand this to work beyond my local network.
In attempts to achieve this, I port-forwarded a Class D IP address on my router in order to allow other people to access my Multicast group from outside my network.
However, when trying to connect to my "group chat" via my public IP and specified port (during the port-forwarding), I would receive the following error message...
Exception in thread "main" java.net.SocketException: Not a multicast address
at java.net.MulticastSocket.joinGroup(MulticastSocket.java:310)
...
This error makes some sense, given that my public IP isn't a class D address. But since I port-forwarded a multicast address to the specified port on my router, shouldn't this problem not occur?
Here's the relevant part of my code...
InetAddress group = InetAddress.getByName("192.___.___.___"); // my public IP
MulticastSocket socket = new MulticastSocket(1234); // the port-forwarded port
socket.joinGroup(group);
Where had I gone wrong here, and how could I get to fixing this issue?
A multicast address is between 224.0.0.0 - 239.255.255.255 with different sub-ranges within for different scenarios. More here: https://en.wikipedia.org/wiki/Multicast_address
So by attempting to join a group at 192.x.y.z, that's an invalid multicast address. That's why you get the exception thrown.
I could be mistaken, I doubt most consumer/home NAT, much less ISPs support multicast traffic. (Begs the questions - whatever happened to the MBONE - I thought that would have taken off and been the solution for everything.)
It sounds like what you need is a proxy program that intercepts multicast traffic and tunnels it to a proxy on a different network running the same code. The proxy in turn, takes the tunnelled packet and redirects back to a multicast\broadcast group.
You might have better luck with broadcast sockets instead of multicast.

Get ip from udp url/address in tracker

In Java, or more generally, if I have a tracker announce url like: udp://tracker.coppersurfer.tk:6969, how can I acquire the ip address to add to the constructor of a DataGramPacket that requires an InetAddress?
Or am I missing something and this information is available somewhere else in a .torrent file?
In the BitTorrent specification I can only find how it is done with http addresses but all torrents I find use udp for their trackers.
It's close to impossible to run a popular public http tracker today. as they take too much resources to run. When a tracker gets to popular, it has to use only UDP.
BitTorrent UDP trackers are specified in BEP15 - UDP Tracker Protocol.
To get the IP-address for the URL, use DNS.
Answer from Get IP address with URL string? (Java) by Victor Stafusa:
Try this:
InetAddress address = InetAddress.getByName(new URL(urlString).getHost());
To get the raw IP:
String ip = address.getHostAddress();

Java Socket on Different Machine Does Not Work

I've tried many examples on web and one of them is this:
http://zerioh.tripod.com/ressources/sockets.html
All of the server-client socket examples work fine when they are tested with 127.0.0.1
BUT it never ever EVAR works on two different computers with actual raw real IP address ("could not connect to host" on telnet and "connection timed out" when tested on java client - the server program just waits for connection)
Note:
Firewall is turned off for sure
IP address from ipconfig didn't work
IP address from myipaddress.com (which is totally different for no reason than that from ipconfig) didn't work
What is it that I'm missing?
If I can only figure this out...
Try binding on 0.0.0.0. This tells your socket to accept connections on every IP your local can accept upon.
Based on the comment where the the following snippet of code is mentioned:
requestSocket = new Socket("10.0.0.5", 2004); // ip from ipconfig
it would be better to use the hostname instead of the IP address in the constructor, as the two-parameter Socket constructor with a String argument expects the hostname as the String, and not an IP address. A lookup of the IP address is then performed on the provided hostname.
If you need to pass in an IP address, use the two-parameter constructor that accepts the InetAddress as an argument. You can then provide a raw IP address to the InetAddress.getByAddress method, as shown in the following snippet:
InetAddress addr = InetAddress.getByAddress(new byte[]{10,0,0,5});
You'll need to be careful when specifying arguments via the byte array, as bytes are signed in Java (-127 through +128), and numbers beyond this range (but valid octets of IP addresses) may have to be specified using Integer.byteValue.
Finally, it should be noted that it is important to specify the IP address of the remote machine, as visible to the client. The IP address listed at myipaddress.com may be the address of a proxy, as that is the public IP of your entire network as visible to the host server at myipaddress.com. Therefore, you ought to be specify the IP address of the remote machine that is visible to your machine and not myipaddress.com.

How to send request from virtual IP address to a server?

Can any one tell me how could i send request to a server using different IP address. Let me explain the requirement; I have to create n no of different virtual IP address in my machine, and then I have to sent request to a different server using those individual virtual IP address. Like one request from one virtual IP address , one from another virtual IP address. Can anyone tell me how could I achieve this programatically ? I am developing my code in java so if you have any code or concept on this please share with me.
It's done the same way in Java as any other language: you bind the socket to the address you want to connect from, before you connect.
Socket s = new Socket();
/*
* Change the 192.168.0.1 to whatever IP address you want the connection
* to come from. If your IP address is stored in an InetAddress object,
* you can use that too, instead of a string.
*/
SocketAddress from = new InetSocketAddress("192.168.0.1", 0);
SocketAddress to = new InetSocketAddress("192.168.0.2", 80);
s.bind(from);
s.connect(to);

How to fetch all DNS entries from JAVA application?

As of now, I'm using the below code to get DNS name of the given IPAddress. Instead of fetching it for each IPAddress in the network, I want to fetch all the DNS entries (IPAddress - HostName mapping) from the DNS Server in one go. Is it possible? If so, how to do it?
InetAddress addr = InetAddress.getByName(address);
dnsname = addr.getCanonicalHostName().trim();
From a public DNS server, there is no way to pull out all the data it holds. Enumerating all the IP addresses one by one is the only solution.
If you have a special relationship with the DNS server (for instance, it is managed by your employer), you may request from the DNS administrator a right to transfer the whole zone (the DNS request known as AXFR). They may authorize your IP address or gives you a TSIG key to authentify yourself.
Then, you will have to find a way to do a zone transfer (possibly with TSIG authentication) in Java. Using these keywords, I find some code and documentation. Use a code search engine like Google Code Search or Krugle to find examples of use.
[DNS experts will probably scream "Use zone walking on NSEC" but most DNS zones are not signed with NSEC.]

Categories

Resources