I have a case where I need to telnet to a device and show its configuration .
If I was to run this manually step by step, following are the details:
telnet 10.105.234.56
Trying 10.105.234.56... Connected to 10.105.234.56. Escape character is '^]'.
S524DF4K15000002 login:
admin
Password: Welcome !
S524DF4K15000002 #
config system dns
S524DF4K15000002 (dns) #
show
config system dns set name "8.8.8.8" end
S524DF4K15000002 (dns) #
where the grey
part are my inputs.
And by this java code,
public static void main(String[] args){
try{
String command = "config system dns";
String command2 = "show";
String host = "10.105.234.56";
String user = "admin";
String password = "";
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);;
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream input = channel.getInputStream();
channel.connect();
System.out.println("Channel connected to machine " + host + " server with command: " + command );
try{
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(inputReader);
String line = null;
while((line = bufferedReader.readLine()) != null){
System.out.println(line);
}
bufferedReader.close();
inputReader.close();
}catch(IOException ex){
ex.printStackTrace();
}
channel.disconnect();
session.disconnect();
}catch(Exception ex){
ex.printStackTrace();
}
I was able to:
1. telnet 10.105.234.56" with user as: "admin"
2. run "config system dns" command.
But where I may add this follow-up show
command in this java code ?
The OS of this device is little simple, that I can not pipe both commands into one piece to run.