java 远程执行linux命令
2016-01-12 10:19
711 查看
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包(j2ssh-core-0.2.9.jar),还有相关的demo
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包(j2ssh-core-0.2.9.jar),还有相关的demo
相关文章推荐
- linux常用命令(三)
- Linux 下的screen命令的使用
- linux php多版本
- Linux软连接和硬链接
- CentOS 6.2安装portmap
- linux hosts&dns
- CentOS和Ubuntu下安装配置Greenplum数据库集群(包括安装包和源码编译安装)
- linux系统fdisk分区过程-46
- 关于rndc-confgen 在centos 上不能用的解决方法
- CentOS 下载地址
- CentOS 7系统升级备份恢复实验记录
- Linux文件系统(基础)
- CentOS 64位上编译 Hadoop 2.6.0
- Linux下搭建MySQL数据库系统
- linux定时任务
- linux 命令 —— tar
- linux通过信号进程间通信实例理解
- Linux找出程序启动失败的原因
- linux(Ubuntu)中安装mavne私服Nexus
- centos workerman php Call to undefined function mysqli_connect()