使用InputStream和OutputStream来获得命令行运行结果和输入内容是首先需要具备的.
然后要解决SSH连接问题.
在OpenSource项目里找到了
Ganymed SSH-2 for Java .下载后有例子,但是没有找到有关交互命令的例子.所以自己探索了一下
在网上看有老外讨论用这个如何改密码,基本上的问题都是因为找不到返回的提示
Linux修改密码输入passwd后,首先是输入当前密码,然后是输入两个新密码
如果当前密码输入错误,或者新密码输入不符合系统密码规则要求,会要求重新输入.如果碰到这种情况,程序很容易进入一个不太好控制的循环.
可能一般人都认为输入命令后所有的返回都应该是用session.getStdout();而不是session.getStderr();从方法名称上看确实如此,一个是取得标准输出,一个是取得错误信息.
其实在输入passwd后.
Changing password for user [username].
Changing password for [username]
这两行是需要通过session.getStdout();来获得的.但是后面的
(current) UNIX password:
New UNIX password:
Retype new UNIX password:
这三行是需要使用sessssion.getStderr();来获取的
如果用户输入没有通过系统检测,会返回具体原因,但是都是BAD PASSWORD开始的.
如果最后修改成功了.你也会看到有successfully来表示.这些信息是通过session.getStdout();来获取的.
现在问题很好解决了吧.
写了一个类来完成修改密码的操作.
后来使用这个方法,现在公司内部新建帐号以及帐号设置自动邮件回复都可以通过自己的管理系统设置了.
import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; import com.io.Debug; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; public class ChangeEmailPassword { private String username = ""; private String oldpassword = ""; private Connection conn = null; private boolean hasError = false; private String ErrorMessage = ""; private boolean isSuccessfully = false; private String SystemMessage = ""; public static final String HOST = "127.0.0.1"; //server ip public boolean isSuccessfully(){ return isSuccessfully; } public boolean isHasError(){ return hasError; } public String getErrorMessage(){ return ErrorMessage; } public void setErrorMessage(String msg){ hasError = true; this.ErrorMessage = msg; } public ChangeEmailPassword(String username,String oldpassword) { this.username = username; this.oldpassword = oldpassword; try{ conn = new Connection(HOST); conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, oldpassword); if (isAuthenticated == false) { setErrorMessage("Authentication failed."); conn=null; } }catch(Exception e){ conn.close(); conn = null; System.out.println(e); } } public void setNewPassword(String newpassword) { if(hasError){ return; } if (conn==null){ return; } try{ Session sess = conn.openSession(); sess.execCommand("passwd"); InputStream so = sess.getStdout(); InputStream err = sess.getStderr(); OutputStream out = sess.getStdin(); byte[] buffer = new byte[500];//其实没有必要这么大.130就差不多了.怕万一有什么提示. int length = 0; length = err.read(buffer); // if (length > 0) { // System.out.println("#1:"+ new String(buffer, 0, length)); // //(current) UNIX password: // } String coldpassword = oldpassword+"/n"; out.write(coldpassword.getBytes()); length = err.read(buffer); // if (length > 0) { // System.out.println("#2:"+ new String(buffer, 0, length)); // //(current) UNIX password: // } String cnewpass = newpassword +"/n"; out.write(cnewpass.getBytes()); length = err.read(buffer); if (length > 0) { String rs = new String(buffer, 0, length); //System.out.println("#3:"+rs); if(rs.indexOf("BAD")>-1){ sess.close(); conn.close(); setErrorMessage(rs); return; } } out.write(cnewpass.getBytes()); length = so.read(buffer); if (length > 0) { String rs = new String(buffer, 0, length); if(rs.indexOf("successfully")>-1){ this.isSuccessfully = true; this.SystemMessage = rs; } } sess.close(); conn.close(); } catch (IOException e) { e.printStackTrace(System.err); } } public static void main(String[] args) { ChangeEmailPassword cep = new ChangeEmailPassword("username", "oldpassword"); if(cep.isHasError()){ System.out.println(cep.getErrorMessage()); cep = null; return; } cep.setNewPassword("newpassword"); if(cep.isHasError()){ System.out.println(cep.getErrorMessage()); cep = null; return; } if(cep.isSuccessfully){ System.out.println(cep.getSystemMessage()); } } public String getSystemMessage() { return SystemMessage; } }
把里面注解掉的System.out.println打开可以看到系统提示的信息.我只保留了最关键的信息提示