hari
hari

Reputation: 1377

Telnet Connection on java console

I am working on eclipse in a Linux OS and this is what i want to do (in the java console only)-

  1. Connect to another remote machine via (currently) telnet client
  2. execute a simple command in that remote system(something like ls)

Is that possible? I'm sure Runtime.getRuntime.exec() wont work. So used the commons.net jar file. Here is my code snippet

public static void testMount() throws Exception {
    String osName = "";
    Scanner sc = new Scanner(System.in);
    TelnetClient telnet = new TelnetClient();
    System.out.println("Operating System: ");
    osName = sc.next();
    System.out.println(osName);
    String volumeToMount = "";
    String mountPoint = "";
    String ipAddress = "";
    int port = 23;

    if (osName.equalsIgnoreCase("Linux")) {
        // Linux
        ipAddress = "1.2.3.4"; //
        telnet.connect(ipAddress, port);
        volumeToMount = "/dev/hda1";
        mountPoint = "/data/Temp";
    } 
    mountFileSystem(volumeToMount, mountPoint);
}

If you have an existing example or if could modify my code, I'd be thankful to you if you share it over here!

Upvotes: 1

Views: 2719

Answers (2)

MarcoS
MarcoS

Reputation: 13574

With Apache Commons Net TelnetClient you must use the InputStream and OutputStream returned by the object (see getInputStream() and getOutputStream() method) to read data and send data (commands). A nice example is available here (see section Telnet and Commons/NET).

Upvotes: 4

AlexR
AlexR

Reputation: 115398

Why are you implementing Telnet yourself? There are several libraries that do it very well. Take a look on Jakarta Net package: http://commons.apache.org/net/

Upvotes: 0

Related Questions