opening Telnet Command Session
Hi,
I want to automate the telnet login in XP where in need the telnet session open in a different window. For this I am using "Process/ProcessBuilder" and then tried to write into the OutputStream of the telnet process. But I am not getting the desired result. Need your help.
The code looks like :
Code :
// cmd is " CMD.EXE /C start telnet <ip>"
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
processBuilder.directory(new File(System.getenv("temp")));
processBuilder.redirectErrorStream(true);
Process proc = processBuilder.start();
System.out.println("Trying to pass the telnet user ID for the login: promt..");
OutputStream out = proc.getOutputStream();
out.write("user\n".getBytes());
out.close();
System.out.println("Trying to pass the telnet password for the password: promt..");
OutputStream out = proc.getOutputStream();
out.write("pass\n".getBytes());
out.close();
//Some other command sequence....
The telnet session comes up then other data passed to the child process doesn't seem to have any apparent effect. It is required for me to remain the telenet window open.....
Any suggestion?
Re: opening Telnet Command Session
Hello voyager. Welcome to the Java Programming Forums :)
Can you please post all of your code for me to compile? Thanks.
Re: opening Telnet Command Session
Thank you JavaPF,
For a quick response :)
For testing the below code will simulate the things:
Code :
import java.util.StringTokenizer;
import java.io.*;
class TelnetTest {
public static void main(String args[]) {
String ip = "<give an IP address to connect>";//or pass it as an argument
String strCMD = "CMD.EXE /C start telnet " + ip;
StringTokenizer st = new StringTokenizer(strCMD);
String[] cmd = new String[st.countTokens()];
int total = st.countTokens();
for (int i = 0; i < total; i++) {
cmd[i] = st.nextToken(" ");
}
try {
ProcessBuilder processBuilder = new ProcessBuilder(cmd);
processBuilder.directory(new File(System.getenv("temp")));
processBuilder.redirectErrorStream(true);
Process proc = processBuilder.start();
/*
* Now the child process is on and waiting for user ID
*/
System.out.println("Trying to pass the telnet user ID for the login: promt..");
OutputStream out = proc.getOutputStream();//This should give me the access to stdout of the child
out.write("user\n".getBytes());//provide a valid user name
out.flush();
System.out.println("Trying to pass the telnet password for the password: promt..");
out.write("pass\n".getBytes());
out.close();
int status = proc.waitFor();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
Here I am not trying to use InputStream because I expect it to be visible in the telnet window.
Thanks again....
Re: opening Telnet Command Session
Hello voyager,
I haven't really played with the ProcessBuilder before so I'm not sure whats going on. Where did you get this code example from?