java通过SSH远程修改linux用户密码
2018-03-30 18:42
567 查看
转自:http://blog.sina.com.cn/s/blog_407956230100e7qt.html
使用的第三方包下载:ganymed-ssh2-build210.jar
代码如下: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;
}
}
使用的第三方包下载:ganymed-ssh2-build210.jar
代码如下: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;
}
}
相关文章推荐
- Linux下自动修改用户密码的方法(直接通过命令而不是在终端输入密码)
- CentOS6.5下通过Shell修改MySQL初始密码,开启远程登录,授权远程登录用户
- 通过SSH信任关系,批量修改LINUX密码[转]
- SSH密匙登录Linux,WinSCP教程,修改Linux用户密码
- 通过JAVA开发的修改AD用户的工具,修改AD用户密码时报错
- [转] 关于linux下通过shell命令(自动)修改用户密码
- Linux下通过 ssh-copy-id 无密码远程登录主机
- 【Hadoop系列】linux下 root用户免密码登录远程主机 ssh
- linux下开启SSH,并且允许root用户远程登录,允许无密码登录
- 通过linux ssh远程登录另一台Linux,无需密码,用证书验证
- 【Hadoop系列】linux下 root用户免密码登录远程主机 ssh
- 新建用户无法通过SecureSRT进行ssh登录到远程linux (zz)
- linux下开启SSH,并且允许root用户远程登录,允许无密码登录
- 如何通过linux ssh远程linux不用输入密码登入
- 如何通过linux ssh远程linux不用输入密码登入
- Java 使用SSH连接Linux 使用passwd命令修改密码
- linux通过grub修改root用户密码为123456,详细记录步骤
- CentOS6.5下通过Shell修改MySQL初始密码,开启远程登录,授权远程登录用户
- 通过java使用ssh访问远程Linux
- 通过SSH信任关系,批量修改LINUX密码