I am doing SSH with my linux server via a java program , I am able to execute the commands like shell scripts. But the problem is whenever I am executing a particular shell script it is giving an option to select between yes or no , now from my java console I am selecting yes but this is not actually interacting with the linux server. How to do this job via a java programming? if you have any code spake please share . I am shairing my code for doing the SSH.
String arr1[]=new String[arr.length];
ReadConfig rc=new ReadConfig();
rc.readConfig();
hostname=rc.hostname;
username=rc.username;
password=rc.password;
for(int i=0;i<arr.length;i++)
{
arr1[i]= arr[i];
System.out.println("the array value "+arr1[i]+" "+i);
}
try{
String com=arr1[5].toString().trim();
System.out.println(" "+com);
/* Create a connection instance */
conn = new Connection(hostname);
/* Now connect */
conn.connect();
/* Authenticate.
* If you get an IOException saying something like
* "Authentication method password not supported by the server at this stage."
* then please check the FAQ.
*/
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
/* Create a session */
sess = conn.openSession();
try{
sess.execCommand(com);
//obj.append("Pass");
}catch(Exception e){
//obj.append("Fail");
}
//Session sess1 = conn.openSession();
System.out.println("Here is some information about the remote host:");
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
/* Show exit status, if available (otherwise "null") */
System.out.println("ExitCode: " + sess.getExitStatus());
}catch (Exception e) {
// TODO: handle exception
}finally{
/* Close this session */
//obj.close();
sess.close();
/* Close the connection */
conn.close();
}
You are not writing anything to the input stream. Just typing in the java console doesn't do anything at all.
Create a PrintWriter for the sessions stdin-OutputStream:
Then you can out.println("y")
if you need to. If you need interactivity using the java-console, you need to read from System.in and send that to your outputstream.
PrintWriter out = createPW(new OutputStreamWriter(sess.getStdin()), true);
Here's a half-decent way of creating a PrintWriter with a suitable line separator character.
public static PrintWriter createPrintWriter(OutputStreamWriter out, boolean autoflush) {
Properties props = System.getProperties();
synchronized (props) {
Object old = null;
try {
old = props.setProperty("line.separator", "\n");
return new PrintWriter(out, autoflush);
} finally {
if (old != null) {
props.put("line.separator", old);
}
}
}
}
you can use "yes" command. Pipe output from Yes into the next command
yes | apt-get install some_software
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.