我正在通過java程序與我的linux服務器進行SSH通信,我能夠執行諸如shell腳本之類的命令。但問題是,每當我執行一個特定的shell腳本時,它會給出一個選項來選擇yes還是no,現在從我的java控制檯中選擇yes,但這實際上並沒有與linux服務器交互。如何通過java編程來完成這項工作?如果你有任何代碼請分享。我正在修改我的SSH代碼。無法通過SSH從JAVA程序進行交互
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();
}
看起來你使用的是Ganymed-SSH2。無論你在這個問題上應該這樣說。但是,如果shell腳本執行某些操作,以便從控制檯獲得輸入而不是stdin,則無法對其執行任何操作。 – EJP
是的我使用ganymade ssh,但如何使它與linux服務器交互? – user993158