Dear Members,
I am trying to create a GUI which will accept an IP ( in a JText Field ) and will connect to a TELNET server.
It will then issue a couple of telnet commands (like username, password and another command ) and then it will close.
I am trying to make use of the Java Cookbook's Telnet Client program (Section 16.9) and accommodate it to run with a simple input field and button in a GUI.
TELNET Program:
import java.net.*; import java.io.*; /** * Telnet - very minimal (no options); connect to given host and service */ public class Telnet { String host; int portNum; public static void main(String[] argv) { new Telnet().talkTo(argv); } private void talkTo(String av[]) { if (av.length >= 1) host = av[0]; else host = "localhost"; if (av.length >= 2) portNum = Integer.parseInt(av[1]); else portNum = 23; System.out.println("Host " + host + "; port " + portNum); try { Socket s = new Socket(host, portNum); // Connect the remote to our stdout new Pipe(s.getInputStream(), System.out).start(); // Connect our stdin to the remote new Pipe(System.in, s.getOutputStream()).start(); } catch(IOException e) { System.out.println(e); return; } System.out.println("Connected OK"); } } /* This class handles one half of a full-duplex connection. * Line-at-a-time mode. */ class Pipe extends Thread { BufferedReader is; PrintStream os; /** Construct a Pipe to read from "is" and write to "os" */ Pipe(InputStream is, OutputStream os) { this.is = new BufferedReader(new InputStreamReader(is)); this.os = new PrintStream(os); } /** Do the reading and writing. */ public void run() { String line; try { while ((line = is.readLine()) != null) { os.print(line); os.print("\r\n"); os.flush(); } } catch(IOException e) { throw new RuntimeException(e.getMessage()); } } }
My problem is that I do not know how to combine the TELNET program which is a console input with a button that will read a user given text.
What I am trying to achieve:
private void JB_updateActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String user_IP; //Get IP from Java Text Field user_IP = JTF_Input.getText(); // Do nothing if text field is empty // Or Print out IP if (!"".equals(user_IP)) { //Start Telnet client and pass IP //Pass Username password //Pass command //Receive Confirmation //Disconnect on successful confirmation } }