私はLinuxプログラムを使って自分のLinuxサーバーでSSHをやっていますが、シェルスクリプトのようなコマンドを実行することができます。しかし、問題は、特定のシェルスクリプトを実行しているときにyesまたはnoを選択するオプションを与えているときです。私のjavaコンソールからはyesを選択していますが、これは実際にLinuxサーバと対話していません。どのようにJavaプログラミングを介してこの仕事をするには?あなたがコードspakeを持っている場合は、共有してください。私はSSHを行うための私のコードをshairingです。JAVAプログラムから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();
}
Ganymed-SSH2を使用しているようです。それが何であれ、あなたはその質問の中でそう言うべきです。しかし、シェルスクリプトが何かをして、コンソールから入力を得るのであれば、実際には何もできません。 – EJP
はい私はganymade sshを使用していますが、Linuxサーバーと対話させる方法はありますか? – user993158