Java uses telnet to connect to the AIX server

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
[size=x-large]import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

import org.apache.commons.net.telnet.TelnetClient;

/**
* Using Apache net open source package, use the telnet method to obtain the AIX host information
*
* @version 1.2
*/
public class NetTelnet {

// The Telnet object
private TelnetClient telnet = new TelnetClient();

private InputStream in;

private PrintStream out;

// Prompt. Each server is not the same note changes
private String prompt = "#";

// Telnet port
private String port;

// The user
private String user;

// Cipher
private String password;

// IP address
private String ip;

public NetTelnet(String ip1, String user1, String password1, String port1) {

try {
// The AIX host IP
this.ip = ip1;
this.password = password1;
this.user = user1;
this.port = port1;
telnet.connect(ip, Integer.parseInt(port));
System.out.println("To obtain the input stream...");
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// Sign in
readUntil("login: ");
write(user);
readUntil("Password: ");
write(password);
readUntil(prompt);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Read results
*
* @param pattern
* @return
*/
public String readUntil(String pattern) {
try {
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
InputStreamReader br = new InputStreamReader(in);
char ch = (char) br.read();
while (true) {
System.out.print(ch);
sb.append(ch);
if (ch == lastChar) {
if (sb.toString().endsWith(pattern)) {
return sb.toString();
}
}
ch = (char) br.read();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("return null");
return null;
}

/**
* Write
*
* @param value
*/
public void write(String value) {
try {
out.println(value);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Send a command string
*
* @param command
* @return
*/
public String sendCommand(String command) {
try {
write(command);
return readUntil(prompt);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* Close the connection
*
*/
public void disconnect() {
try {
telnet.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
try {
System.out.println("Started the implementation of telnet......");
NetTelnet telnet = new NetTelnet("10.1.66.19", "root", "root",
"23");

System.out.println("Start sending hostname commands");
String result = telnet.sendCommand("cd zy");
System.out.println("Display the results");
System.out.println(result);
result = telnet.sendCommand("sh print.sh");
System.out.println("Display the results");
System.out.println(result);
// Finally, be sure to turn off the
telnet.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
[/size]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Cash at December 08, 2013 - 2:39 AM