Java远程执行Linux命令
2015-06-13 10:33
483 查看
//指明连接主机的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(command);
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();
}
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(command);
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();
}
相关文章推荐
- Linux注意到Makefile
- VMware虚拟机上安装linux和克隆
- CentOS 5.5 Samba服务器安装总结
- Centos 5.5下安装samba
- linux C++ 进程间共享内存以及临界区的实现
- 【学习笔记】CentOS 修改显示语言
- linux 笔记
- Linux kernel Panic后自动重启机器的设置
- dhcp服务器详细配置
- 如何在RHEL/CentOS 7.0中使用tmpfs
- Linux环境下MySQL设置gbk编码
- Centos7 安装 mono 4
- 使用kickstart服务全自动安装RHEL7.0系统
- linux下安装jdk1.7
- linux安装jdk
- 本机能ping通虚拟机,但无法通过SSH登录到虚拟机上的linux系统
- VMware虚拟机上安装linux和克隆
- Linux 有问必答:如何知道进程运行在哪个 CPU 内核上?
- 在centOS上离安装Python2.7以及numpy,scipy,matplot,sklearn等
- Linux系统上的DNS中正反向解析与主从服务器配置详解