ssh2 -java 实例
来源:互联网 发布:淘宝店铺第三层级好吗 编辑:程序博客网 时间:2024/05/03 22:31
package testcase;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
public class TestSSH2 {
private boolean authed = false;
private Connection conn = null;
private Session session = null;
public DataInputStream dis = null;
public DataOutputStream dos = null;
public TestSSH2(String ip, int port) {
this.conn = new Connection(ip, port);
try {
this.conn.connect();
} catch (IOException e) {
e.printStackTrace();
}
}
public void auth(String user, String pwd) {
try {
// true:支持password认证 false:不支持password认证
if (!this.conn.isAuthMethodAvailable(user, "password")) {
System.out.println("---------- 不支持密码认证 ----------");
}
this.setAuthed(this.conn.authenticateWithPassword(user, pwd));
/* true:支持keyboard-interactive认证 false:不支持keyboard-interactive认证
if (conn.isAuthMethodAvailable(user, "keyboard-interactive")) {
this.setAuthed(this.conn.authenticateWithKeyboardInteractive(uid, new InteractiveLogic(pwd)));
}
*/
// 认证成功
if (!this.isAuthed()) {
System.out.println("---------- 认证失败 ----------");
return;
}
System.out.println("---------- 认证成功 ----------");
this.session = this.conn.openSession();
this.session.requestPTY("vt100", 80, 800, 0, 0, null);
this.session.startShell();
this.dis = new DataInputStream(this.session.getStdout());
this.dos = new DataOutputStream(this.session.getStdin());
// 可以尝试输入命令
//this.session.execCommand("whoami");
String cmd = "whoami";
this.dos.write(cmd.getBytes());
this.dos.write("\r".getBytes());
Thread.sleep(1000l);
byte[] buffer = new byte[1024];
int len = dis.read(buffer);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < len; i++) {
char c = (char) buffer[i];
sb.append(c);
}
System.out.println(sb.toString());
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TestSSH2 test = new TestSSH2("192.168.2.99", 22);
test.auth("root", "Abc@12345");
}
public boolean isAuthed() {
return authed;
}
public void setAuthed(boolean authed) {
this.authed = authed;
}
}
- ssh2 -java 实例
- java SSH1/SSH2远程连接实例
- ssh2 java
- SSH2简单实例
- SSH2项目最完整实例
- SSH2整合实例详细教程
- JAVA-SSH2:JSch试用
- Java-SSH2-网上银行交易系统
- ssh2
- SSH2
- SSH2
- ssh2
- ssh2
- java方向的SSH2问题
- Java ssh2简单整合流程
- JAVA SSH2学生管理系统
- Java通过SSH2协议运行远程程序
- Java程序员新基本功之一:SSH2
- Unity中的Path对应各平台中的Path
- 创业一周年,我亲历的这五道坎
- Android View.onMeasure方法的理解
- MySQL数据库可以用任意ip连接访问的方法
- 关于IO流
- ssh2 -java 实例
- SVN_cleanup死循环解决方案
- 数据结构
- Collections.sort
- MySQL常用命令大全
- ArcGIS Engine开发的汉化小技巧-来自arcchen和记忆De残骸
- 【Git学习笔记】创建版本库
- 网关地址
- RichEdit 2.0使用问题