java远程执行linux命令
2013-06-05 23:50
465 查看
如何实现JAVA远程操控linux服务器,代码如下:
Java代码
package hb.linux;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class TestCtrCommond {
public static void main(String[] args) {
String hostname = "129.17.17.20";
String username = "weblogic";
String password = "weblogic";
//指明连接主机的IP地址
Connection conn = new Connection(hostname);
Session ssh = null;
try {
//连接到主机
conn.connect();
//使用用户名和密码校验
boolean isconn = conn.authenticateWithPassword(username, password);
if(!isconn){
System.out.println("用户名称或者是密码不正确");
}else{
System.out.println("已经连接OK");
ssh = conn.openSession();
//使用多个命令用分号隔开
// ssh.execCommand("pwd;cd /tmp;mkdir hb;ls;ps -ef|grep weblogic");
ssh.execCommand("cd /app/weblogic/Oracle/Middleware/user_projects/domains/base_domain;./startWebLogic.sh &");
//只允许使用一行命令,即ssh对象只能使用一次execCommand这个方法,多次使用则会出现异常
// ssh.execCommand("mkdir hb");
//将屏幕上的文字全部打印出来
InputStream is = new StreamGobbler(ssh.getStdout());
BufferedReader brs = new BufferedReader(new InputStreamReader(is));
while(true){
String line = brs.readLine();
if(line==null){
break;
}
System.out.println(line);
}
}
//连接的Session和Connection对象都需要关闭
ssh.close();
conn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
远程连接的SSH使用的jar包:ganymed-ssh2-build210.jar
Java代码
package hb.linux;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class TestCtrCommond {
public static void main(String[] args) {
String hostname = "129.17.17.20";
String username = "weblogic";
String password = "weblogic";
//指明连接主机的IP地址
Connection conn = new Connection(hostname);
Session ssh = null;
try {
//连接到主机
conn.connect();
//使用用户名和密码校验
boolean isconn = conn.authenticateWithPassword(username, password);
if(!isconn){
System.out.println("用户名称或者是密码不正确");
}else{
System.out.println("已经连接OK");
ssh = conn.openSession();
//使用多个命令用分号隔开
// ssh.execCommand("pwd;cd /tmp;mkdir hb;ls;ps -ef|grep weblogic");
ssh.execCommand("cd /app/weblogic/Oracle/Middleware/user_projects/domains/base_domain;./startWebLogic.sh &");
//只允许使用一行命令,即ssh对象只能使用一次execCommand这个方法,多次使用则会出现异常
// ssh.execCommand("mkdir hb");
//将屏幕上的文字全部打印出来
InputStream is = new StreamGobbler(ssh.getStdout());
BufferedReader brs = new BufferedReader(new InputStreamReader(is));
while(true){
String line = brs.readLine();
if(line==null){
break;
}
System.out.println(line);
}
}
//连接的Session和Connection对象都需要关闭
ssh.close();
conn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
远程连接的SSH使用的jar包:ganymed-ssh2-build210.jar
相关文章推荐
- 利用java实现可远程执行linux命令的小工具
- java本地调用cmd,shell命令,远程调用Linux执行命令方法总结
- sshxcute --java远程执行linux/unix命令的工具类
- 利用java实现可远程执行linux命令的小工具
- windows 下 java程序jsch连接远程linux服务器执行shell命令
- java远程执行linux命令
- Java远程连接Linux系统并执行Linux命令
- java 使用jsch 远程链接linux执行命令
- Java实现远程连接Linux并执行命令
- java 远程执行 linux 命令
- java 使用jsch 远程链接linux执行命令
- JAVA实现远程SSH连接linux并执行命令
- java 使用jsch 远程链接linux执行命令
- java本地调用cmd,shell命令,远程调用Linux执行命令方法总结
- java通过代码登录远程linux服务器并执行linux命令
- Ganymed SSH-2 java执行远程linux机器命令工具
- java通过ssh2远程连接计算机并执行linux命令的方法(转)
- Java 远程执行 Linux 的命令
- java远程执行linux命令
- java应用程序远程登录linux并执行其命令(ssh jar包)