Skip to main content

How To Generate Random IP Address Using JAVA ?

Today I'll Show You How To Generate Random IP Address...

But First Why I Want This...Right...?

Ans : If You Want Test Your Code That Whether It Is Recognizing The Machine By It’s IP Address Then You Had To Use This Code To Generate Random IP Address.


So Here We Goooo....


private String generateIPAddress(int p1, int p2, int p3) {

    StringBuilder sb = null;

Random r1 = new Random();
    Random r2 = new Random();
 Random r3 = new Random();
 Random r4 = new Random();



    String ip1 = Integer.toString(b1);
    String ip2 = Integer.toString(b2);
    String ip3 = Integer.toString(b3);
    String ip4 = Integer.toString(b4);
    
    String b1 = IpConverter.longToIp(r1.nextLong());
    String b2 = IpConverter.longToIp(r2.nextLong());
    String b3 = IpConverter.longToIp(r3.nextLong());
    String b4 = IpConverter.longToIp(r4.nextLong());

    //Now the IP is b1.b2.b3.b4
    sb = new StringBuilder();
    sb.append(ip1).append(".").append(ip2).append(".").append(ip3).append(".").append(ip4);
    // System.out.println(sb);

    return sb.toString();

}

The Random Method is used to generate random number in java,In this case we have to generate four
hence

Random r1 = new Random();
    Random r2 = new Random();
 Random r3 = new Random();
 Random r4 = new Random();

We need to convert our generated number in an IP String

String ip1 = Integer.toString(b1);
    String ip2 = Integer.toString(b2);
    String ip3 = Integer.toString(b3);
    String ip4 = Integer.toString(b4);

The numbers generated may be higher than 255 hence we use String to IP converter which keeps the value 0 to 255 because the IPv4 has the limit of 0 to 255 only .

String b1 = IpConverter.longToIp(r1.nextLong());
    String b2 = IpConverter.longToIp(r2.nextLong());
    String b3 = IpConverter.longToIp(r3.nextLong());
    String b4 = IpConverter.longToIp(r4.nextLong());
Now we Append the converted individual string and return it as the single IP address

sb = new StringBuilder();
    sb.append(ip1).append(".").append(ip2).append(".").append(ip3).append(".").append(ip4);
    return sb.toString();
And You Have Done :)

Comments

Post a Comment

plz add ur comment here

Popular posts from this blog

Upcoming Book- Kali Linux Revealed: Mastering the Penetration Testing Distribution

More exciting news! In the past year, Kali Linux Official Team has been working internally on an Official Kali Linux book – Kali Linux Revealed: Mastering the Penetration Testing Distribution . This is the first official Kali book from Offsec Press , and is scheduled for release on June 5th, 2017 . Kali Linux Revealed will be available in both hard copy and online formats. Keeping the Kali Linux spirit, the online version of the book will be free of charge, allowing anyone who wishes to hone their skills and improve their knowledge of Kali to do so at no cost. This book, together with our official Kali documentation site will encompass the body of knowledge for the Kali Linux Certified Professional   (KLCP) .

Kali Linux: A complete InfoSec Distro

BackTrack has always been a popular choice when it comes to security and penetration testing. This open source distro has gained a lot of popularity and was rolled till version 5 with frequent changes to the variants. This project from Offensive Security has been moved from Ubuntu to Debian and the operating system is now renamed as Kali Linux. If you have installed a fresh copy of Kali Linux, then you might have noticed some awesome security and penetration tools under Kali Linux drop down menu list. These tools are powerful and yet efficient to use. If you go through the menu list of security tools, you might have noticed that few like Wireless Attacks, Forensics and Reverse Engineering is added, which is quite impressive. One more section has been added in the list, ie Hardware Hacking. One more thing that you would be happy to see is Arduino kit on the list. Arduino is a single-board microcontroller designed to make the process of using electronics in multi-disciplinary projects mo...

EOF of Kali Linux 1.0

Kali Sana (ver. 2.0) has been out there couple of months ago and the response was great. Kali Moto (ver. 1.0) has been given 2 months of grace period to date the version to 2.0. To upgrade, edit the souces.list file and put the following entries. cat >> EOF < /etc/apt/sources.list deb http://http.kali.org/kali sana main non-free contrib deb http://security.kali.org/kali-security/ sana/updates main contrib non-free EOF Then, run the following command to do upgrade Kali Moto to Kali Sana. apt-get update apt-get dist-upgrade # get a coffee, or 10. reboot