Java – Writing An Automated Telnet Client
By admin on Dec 22, 2007 in Java, Programming
Telnet is a common protocol that we normally used to connect to a remote server, especially in Linux or Unix environment. To programmatically automate a telnet session so that we can execute or retrieve information from a remote server can be easily done using Apache Commons Net.
Below is the code that can be used to do a ps -ef on the remote Linux/Unix server, and retrieve the output.
import org.apache.commons.net.telnet.TelnetClient;
import java.io.InputStream;
import java.io.PrintStream;public class AutomatedTelnetClient {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String prompt = “#”;public AutomatedTelnetClient(String server, String user, String password) {
try {
// Connect to the specified server
telnet.connect(server, 23);// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());// Log the user on
readUntil(“login: “);
write(user);
readUntil(“Password: “);
write(password);// Advance to a prompt
readUntil(prompt + ” “);
}
catch (Exception e) {
e.printStackTrace();
}
}public void su(String password) {
try {
write(“su”);
readUntil(“Password: “);
write(password);
prompt = “#”;
readUntil(prompt + ” “);
}
catch (Exception e) {
e.printStackTrace();
}
}public String readUntil(String pattern) {
try {
char lastChar = pattern.charAt(pattern.length() – 1);
StringBuffer sb = new StringBuffer();
boolean found = false;
char ch = (char) in.read();
while (true) {
System.out.print(ch);
sb.append(ch);
if (ch == lastChar) {
if (sb.toString().endsWith(pattern)) {
return sb.toString();
}
}
ch = (char) in.read();
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}public void write(String value) {
try {
out.println(value);
out.flush();
System.out.println(value);
}
catch (Exception e) {
e.printStackTrace();
}
}public String sendCommand(String command) {
try {
write(command);
return readUntil(prompt + ” “);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}public void disconnect() {
try {
telnet.disconnect();
}
catch (Exception e) {
e.printStackTrace();
}
}public static void main(String[] args) {
try {
AutomatedTelnetClient telnet = new AutomatedTelnetClient(“127.0.0.1”,
“username”,
“password”);
telnet.sendCommand(“ps -ef “);
telnet.disconnect();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
You need to modify the prompt variable so that it matches what you configured in the server.
I used it together with Twitter for remote server monitoring so that I can connect and execute commands on the servers behinds firewall from the DMZ.
Note: This is potential security threat if not used properly.
subpop | Feb 13, 2008 | Reply
Cool! fantastic! thanks
James | Dec 17, 2008 | Reply
Okay I need code that will compile. I got the source code for org.apache.commons.net.telnet.TelnetClient, saved it as a file and it does not compile. Not sure what I’m doing wrong.
James Huang | Mar 30, 2009 | Reply
It is a great job. One command is working properly. I tried to add several cammands in a loop, it only stay in the first command. I am not sure if this is the prompt. On SunOS, the prompt should be PS1 setting in the profile something like PS1=”`/usr/bin/uname -n`($LOGNAME)$PWD > “, so the prompt should be /usr/bin/uname -n`($LOGNAME)$PWD >. Could you please help?
Thanks!
Raja M | Jul 6, 2009 | Reply
It is a great job. One command is working properly. I tried to add several commands in a loop, it only stay in the first command.can you help me pls
Abdullah | Aug 20, 2009 | Reply
Thank you it is great work.
Guys I run commands in loop and it is working fine for me
Julio | Feb 12, 2010 | Reply
Hi Abdullah, can you tell me how can run commands in loop ????
I tried to do that, but I don’t know how.
thanks in advanced.
Julio