Я пытаюсь подключиться к удаленной системе Linux из другой системы Linux через telnet, используя код Java, как показано ниже:
public static void main(String[] args) throws InterruptedException
{
// TODO Auto-generated method stub /usr/bin/telnet
try
{
String line, commandInput;
ProcessBuilder telnetProcessBuilder = new ProcessBuilder("/bin/bash");
telnetProcessBuilder.redirectErrorStream(true);
Process telnetProcess = telnetProcessBuilder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(telnetProcess.getInputStream()));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(telnetProcess.getOutputStream()));
commandInput = "telnet <hostname> -l <username>\n";
output.write(commandInput);
output.flush();
commandInput = "<password>\n";
output.write(commandInput);
output.flush();
commandInput = "ls -l\n";
output.write(commandInput);
output.flush();
commandInput = "pwd\n";
output.write(commandInput);
output.flush();
commandInput = "exit\n";
output.write(commandInput);
output.flush();
commandInput = "uname -a\n";
output.write(commandInput);
output.flush();
commandInput = "exit\n";
output.write(commandInput);
output.flush();
while((line = input.readLine())!= null)
System.out.println(line);
//telnetProcess.destroy();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Я могу подключиться к удаленному компьютеру и выполнить команды перед первым выходом, а также выход из удаленного компьютера при первом выходе. Теперь проблема в том, что я не могу выполнить команды после 1-го выхода, даже если его команда выхода из / bin / bash, которая завершит выполнение процесса с кодом 0. И если я уничтожу процесс после 1-го выхода, BufferedReader и BufferedWriter выиграли не работает, что обычно работает, если процесс завершается с кодом 0. Я не уверен, что может привести к неисполнению команд после 1-го выхода. Пожалуйста, дайте мне знать ответ, если кто-нибудь уже сталкивался с ним.
Спасибо
Ashutosh