Search code examples
c#javanetwork-programminginterfacevirtual

Create virtual network interface programmatically


Is it possible to add a virtual network interface with Java or C#. I need to set a bunch of IPs to my computer for a data mining app and I don't have enought NICs (I don't want to buy more yet).

I need full control of virtual cards from the app (create, delete, set IP, maybe redirect traffic).


Solution

  • I am not sure you have a pure Java solution for this.

    You have several alternatives.
    1. Use a script that will do that for you, writing this solution in perl\shell will be 4-5 lines.
    2. You can open a ssh connection to your machine and run the commands from Java. I have such utility that uses trilead-ssh2.

    To get it using maven you can use:

        <dependency>
            <groupId>com.trilead</groupId>
            <artifactId>trilead-ssh2</artifactId>
            <version>build213-svnkit-1.3-patch</version>
        </dependency>
    

    For example, this way you should open a connection:

    public static Connection newConnectionWithPassword(String host, String username, String passwd) {
        Connection newConn = new Connection(host);
        try {
            newConn.connect(); // Ignoring ConnectionInfo returned value.
            newConn.authenticateWithPassword(username, passwd);
            return newConn;
        } catch (IOException ioe) {
            newConn.close();
            ioe.printStackTrace();
            return null;
        }
    }
    

    Then to run your command you can :

    this.session = conn.openSession();
    this.session.execCommand(this.cmd);
    

    You can use an OS command to create your virtual interfaces from java now. BTW, You can test the results using NetworkInterface.class , that can query a netweork interface on your machine.

    Good luck.