2013-02-13 43 views
1

对不起,如果我的英语太糟糕了。我想请教一下执行从Java “passwd文件” 命令(我使用NetBeans IDE & JSCH库)用JAVA更改UNIX密码

这是我的代码

String username = txtusername.getText(); 
    String password = txtpassword.getText(); 
    String ip = txtIP.getText(); 
    int port = 22; 
    Session session = null; 
    Session session2=null; 
    ChannelExec channel = null; 
    Channel channel2 = null; 
    StringBuffer result = new StringBuffer(); 
    try 
    { 
     JSch shell = new JSch(); 
     session = shell.getSession(username, ip, port); 
     java.util.Properties config = new java.util.Properties(); 
     config.put("StrictHostKeyChecking", "no"); 
     session.setConfig(config); 
     session.setPassword(password); 
     session.connect(3000); 
     channel = (ChannelExec) session.openChannel("exec"); 
     ((ChannelExec)channel).setCommand("passwd"); 

     channel.setInputStream(null); 
     channel.setOutputStream(null); 
     ((ChannelExec)channel).setErrStream(System.err); 

     InputStream in=channel.getInputStream(); 
     OutputStream out=channel.getOutputStream(); 

     PrintStream char_to_send_to_channel = new PrintStream(out,true); 
     BufferedReader out_from_channel=new BufferedReader(new InputStreamReader(in)); 
     char_to_send_to_channel.println(); 

     String line; 
     channel.connect(); 

     JOptionPane.showMessageDialog(null,"Channel opened!" +"\n"); 

     // Process p = Runtime.getRuntime().exec("passwd"); 

     byte[] tmp=new byte[1024]; 
     while(true){ 
      while(in.available()>0){ 
      int i=in.read(tmp, 0, 1024); 
      if(i<0) { 
        break; 
       } 
      } 
     } 
    } catch (IOException ex) { 
     Logger.getLogger(connect.class.getName()).log(Level.SEVERE, null, ex); 
    } catch(JSchException | HeadlessException e){ 
     JOptionPane.showMessageDialog(null, e); 
    } 
} 

的问题是,我的输出就像

(current) UNIX password: 

如果我输入/输入当前的UNIX密码,则什么也没有发生。 我想要更改Ubuntu Server帐户密码,将其安装在我的VMWare上。 而我在我原来的操作系统上运行这个程序。 简单地说,我想我的输出交互(输入和输出就像输出从Linux命令'passwd')

我的代码有什么问题? :(需要建议

回答