(转)java本地调用cmd,shell命令,远程调用Linux执行命令方法总结
发布日期:2021-07-30 03:26:01
浏览次数:6
分类:技术文章
本文共 3907 字,大约阅读时间需要 13 分钟。
【转载原因:代码亲测可以远程实现连接服务器,收获颇丰,谢谢博主精彩总结~】
【转载地址:】
有时候经常会碰到需要远程调用Linux或者本地调用Linux或者本地调用cmd的一些命令,最近小结了一下这几种用法
本地调用cmd命令
@Test public void testCmd()throws Exception{ String cmd="cmd /c date"; //命令的前面必须要有cmd /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命令
@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执行命令
jar包下载
http://central.maven.org/maven2/com/jcraft/jsch/0.1.54/jsch-0.1.54.jar
或者maven引用(更加方便)
com.jcraft jsch 0.1.54
考代码如下
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); }}
转载地址:https://blog.csdn.net/u010886217/article/details/99656034 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年03月04日 21时15分18秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
在mysql中删除表正确的是什么_在MySQL中删除表的操作教程
2019-04-21
mysql有3个共同好友_共同好友mysql
2019-04-21
代理查询 mysql_查询数据库代理设置
2019-04-21
mysql dif_mysqldiff实现MySQL数据表比较
2019-04-21
mysql 允许其他主机访问权限_允许其他主机访问本机MySQL
2019-04-21
druid不能close mysql连接_alibaba druid mysql连接问题
2019-04-21
mysql 设置按天分表_MySQL 优化实战记录
2019-04-21
java连接mysql 不推荐_java连接mysql
2019-04-21
idea测试连接mysql报错08001_IDEA连接MySQL错误
2019-04-21
layui导入模板数据_layui表格-template模板的三种用法
2019-04-21
mysql分组显示行号_mysql 显示行号,以及分组排序
2019-04-21
MySQL常见的主从复制架构_如何搭建经典的MySQL 主从复制架构
2019-04-21
python 公众号引流_公众号引流方法有哪些?
2019-04-21
java 减少内存_java中减少内存占用小技巧
2019-04-21
centos 7 mysql图形界面_centos7-vnstat图形界面搭建
2019-04-21
java 防渗透_「java、工程师工作经验怎么写」-看准网
2019-04-21
java中跳出当前循环怎么做_在java中,如何跳出当前的多重循环?
2019-04-21