java ssh工具类_java通过SSH远程修改linux用户密码详解

代码如下:

package com.util;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import ch.ethz.ssh2.Connection;

import ch.ethz.ssh2.Session;

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 = "192.168.25.111"; //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,22);

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 = newpassword+"\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 || rs.indexOf("Sorry")>-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("root", "password");

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;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值