java本地调用cmd,shell命令,远程调用Linux执行命令方法总结
2017-07-13 14:43
1006 查看
部署远程服务器的时候会用到,之前做过类似的项目,逛首页的时候又看到了,记录一下。
有时候经常会碰到需要远程调用Linux或者本地调用Linux或者本地调用cmd的一些命令,最近小结了一下这几种用法。
jar包下载
2.
或者maven引用
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class SSHHelper {
/**
* 远程 执行命令并返回结果调用过程 是同步的(执行完才会返回)
* @param host 主机名
* @param user 用户名
* @param psw 密码
* @param port 端口
* @param command 命令
* @return
*/
public static String exec(String host,String user,String psw,int port,String command){
StringBuffer sb= new StringBuffer();
Session session =null;
ChannelExec openChannel =null;
try {
JSch jsch=new JSch();
session = jsch.getSession(user, host, port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");//跳过公钥的询问
session.setConfig(config);
session.setPassword(psw);
session.connect(5000);//设置连接的超时时间
openChannel = (ChannelExec) session.openChannel("exec");
openChannel.setCommand(command); //执行命令
int exitStatus = openChannel.getExitStatus(); //退出状态为-1,直到通道关闭
System.out.println(exitStatus);
// 下面是得到输出的内容
openChannel.connect();
InputStream in = openChannel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String buf = null;
while ((buf = reader.readLine()) != null) {
sb.append(buf+"\n");
}
} catch (JSchException | IOException e) {
sb.append(e.getMessage()+"\n");
}finally{
if(openChannel!=null&&!openChannel.isClosed()){
openChannel.disconnect();
}
if(session!=null&&session.isConnected()){
session.disconnect();
}
}
return sb.toString();
}
public static void main(String args[]){
String exec = exec("192.168.1.xx", "user", "name", 22, "ls");
System.out.println(exec);
}
}本文转自
http://blog.csdn.net/t1dmzks/article/details/75050207
有时候经常会碰到需要远程调用Linux或者本地调用Linux或者本地调用cmd的一些命令,最近小结了一下这几种用法。
本地调用cmd命令
package com.cn.cmd; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class testCmd { public void testCmd(){ String cmd = "cmd /c date"; execCmd(cmd); } private void execCmd(String cmd) { // TODO Auto-generated method stub Runtime rt = Runtime.getRuntime(); try { Process proc = rt.exec(cmd,null,null); InputStream stderr = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(stderr,"GBK"); BufferedReader br = new BufferedReader(isr); String line = ""; while((line = br.readLine()) != null){ System.out.println(line); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // TODO Auto-generated method stub testCmd tc = new testCmd(); tc.testCmd(); } }
本地调用Linux命令
@Test public void testCmd()throws Exception{ String cmd="/bin/sh -c date"; //命令的前面必须要有/bin/sh -c execCmd(cmd); } public static void execCmd(String cmd){ try{ Runtime rt = Runtime.getRuntime(); //执行命令, 最后一个参数,可以使用new File("path")指定运行的命令的位置 Process proc = rt.exec(cmd,null,null); InputStream stderr = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(stderr,"GBK"); BufferedReader br = new BufferedReader(isr); String line=""; while ((line = br.readLine()) != null) { // 打印出命令执行的结果 System.out.println(line); } }catch (Exception e){ e.printStackTrace(); } }
远程调用Linux执行命令
1.jar包下载
http://central.maven.org/maven2/com/jcraft/jsch/0.1.54/jsch-0.1.54.jar
2.
或者maven引用
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.54</version> </dependency>3.源代码如下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class SSHHelper {
/**
* 远程 执行命令并返回结果调用过程 是同步的(执行完才会返回)
* @param host 主机名
* @param user 用户名
* @param psw 密码
* @param port 端口
* @param command 命令
* @return
*/
public static String exec(String host,String user,String psw,int port,String command){
StringBuffer sb= new StringBuffer();
Session session =null;
ChannelExec openChannel =null;
try {
JSch jsch=new JSch();
session = jsch.getSession(user, host, port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");//跳过公钥的询问
session.setConfig(config);
session.setPassword(psw);
session.connect(5000);//设置连接的超时时间
openChannel = (ChannelExec) session.openChannel("exec");
openChannel.setCommand(command); //执行命令
int exitStatus = openChannel.getExitStatus(); //退出状态为-1,直到通道关闭
System.out.println(exitStatus);
// 下面是得到输出的内容
openChannel.connect();
InputStream in = openChannel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String buf = null;
while ((buf = reader.readLine()) != null) {
sb.append(buf+"\n");
}
} catch (JSchException | IOException e) {
sb.append(e.getMessage()+"\n");
}finally{
if(openChannel!=null&&!openChannel.isClosed()){
openChannel.disconnect();
}
if(session!=null&&session.isConnected()){
session.disconnect();
}
}
return sb.toString();
}
public static void main(String args[]){
String exec = exec("192.168.1.xx", "user", "name", 22, "ls");
System.out.println(exec);
}
}本文转自
http://blog.csdn.net/t1dmzks/article/details/75050207
相关文章推荐
- java本地调用cmd,shell命令,远程调用Linux执行命令方法总结
- linux 通过shell脚本在本地执行远程机器命令
- paip.执行shell cmd 命令uapi java php python总结
- java Windows下cmd 命令和Linux下shell命令 执行非JDK中的jar包
- Linux中执行shell脚本命令的4种方法总结
- Java调用linux系统shell执行命令
- 每天一个linux命令(62):sh命令 /Linux中执行shell脚本的4种方法总结
- java上传附件到本地服务器,调用linux命令然后再传输到远程服务器
- java调用windows/Linux/Unix 命令行执行命令的方法--调用ImageMagick的合并图形的命令
- loadrunner调用plink,远程linux执行shell命令
- java 中调用window系统中的文件,或者执行命令(shell、.CMD、.EXE)并获取返回值(如果有的话)
- java通过ssh2远程连接计算机并执行linux命令的方法(转)
- java 中调用window系统中的文件,或者执行命令(shell、.CMD、.EXE)并获取返回值
- windows 下 java程序jsch连接远程linux服务器执行shell命令
- 【Linux】shell调用Java程序main方法通过crontab定时执行
- JAVA Runtime类执行本地命令的方法总结
- Windows系统中Java调用cmd命令及执行exe程序的方法
- java 执行linux命令或shell脚本方法
- 六、java远程调用linux命令或者shell脚本
- paip.执行shell cmd 命令uapi java php python总结