批处理远程执行bat_bat循环执行命令

批处理远程执行bat_bat循环执行命令privatestaticfinalintTIME_OUT=2000;privatefinalstaticStringip=”192.168.70.147″;privatefinalstaticStringusername=”Administrator”;privatefinalstaticStringpasswd=”Yanfa_1304″;/***执行远程电脑下的脚本*/privat


Jetbrains全系列IDE稳定放心使用

1、需要用到psexec.exe,网上都可以下载到

private static final int TIME_OUT = 2000;
    private final static String ip = "192.168.70.**";
    private final static String username = "Administrator";
    private final static String passwd = "**";
    /**
     * 执行远程电脑下的脚本
     */
    private final static String command = "c:\\test.bat";

    public static void main(String[] args) throws IOException, InterruptedException { 
   
        Process p;
        String cmd = "c:\\\\psexec \\\\" + ip + " -u " + username + " -p " + passwd + " -s " + command;
        //执行命令
        p = Runtime.getRuntime().exec(cmd);
        p.waitFor(TIME_OUT, TimeUnit.MILLISECONDS);

        BufferedReader errReader =
                new BufferedReader(new InputStreamReader(p.getErrorStream(), "GBK"));
        String errString = errReader.readLine();
        if (!StringUtils.isEmpty(errString)) { 
   
            System.out.println("fail");
        } else { 
   
            System.out.println("success");
        }
    }

2、使用wmic

private static final int TIME_OUT = 2000;
    private final static String ip = "192.168.0.181";
    private final static String username = "administrt";
    private final static String passwd = "0823";
    /**
     * 执行远程电脑下的脚本
     */
    private final static String command = "shutdown -r";

    public static void main(String[] args) throws Exception { 
   
        char[] chars = new char[1024];
        Process p;
        String cmd = "wmic /node:" + ip + " /user:" + username + " /password:" + passwd + " process call create \"cmd.exe /c " + command
                + ">d:\\result.txt\"";
        //执行命令
        p = Runtime.getRuntime().exec(cmd);
//        p.waitFor(TIME_OUT, TimeUnit.MILLISECONDS);

        BufferedReader errReader =
                new BufferedReader(new InputStreamReader(p.getErrorStream(), "GBK"));
        errReader.read(chars);
        String errString = new String(chars);
        BufferedReader reader =
                new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
        reader.read(chars);
        String string = new String(chars);
        System.out.println(string);
    }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/190200.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • make wildcard_其在古文中的用法

    make wildcard_其在古文中的用法
    在Makefile规则中,通配符会被自动展开。但在变量的定义和函数引用时,通配符将失效。这种情况下如果需要通配符有效,就需要使用函数“wildcard”,它的用法是:$(wildcardPATTERN…)。在Makefile中,它被展开为已经存在的、使用空格分开的、匹配此模式的所有文件列表。如果不存在任何符合此模式的文件,函数会忽略模式字符并返回空。需要注意的是:这种情况下规则中通配符的展开和上一小节匹配通配符的区别。
    一般我们可以使用“$(wildcard*.c)”来获取工作

    2022年10月21日
  • Anaconda中将python 3.7版本退回python 3.6版本

    Anaconda中将python 3.7版本退回python 3.6版本最近在下载tensorflow的时候出现了如下图所示的情况:Couldnotfindaversionthatsatisfiestherequirementtensorflow经过一番查找资料,发现,原来在2019年,TensorFlow还不支持python3.7,所以,迫于无奈,我只能乖乖把python的版本退回到3.6版本,具体步骤也很简单。就是打开anacondap…

    2022年5月28日
  • Random随机数nextInt(n)的使用

    Random随机数nextInt(n)的使用nextInt(intn) 方法用于获取一个伪随机,在0(包括)和指定值(不包括),从此随机数生成器的序列中取出均匀分布的int值。声明以下是对java.util.Random.nextInt()方法的声明。publicintnextInt(intn)参数n–这是结合于该随机数返回。必须为正数。返回值在方法调用返回

    2022年7月22日
  • SQL datetime和smalldatetime区别

    SQL datetime和smalldatetime区别datetime存储大小8个字节,精确到分后的3为小数,日期范围从1753年1月1日到9999年12月31日;而smalldatetime存储大小为4个字节,精确到分,日期范围从1900年1月1日到2079年6月6日。参考http://msdn.microsoft.com/zh-cn/library/ms187819%28v=…

    2022年7月27日
  • shell学习教程(超详细完整)[通俗易懂]

    shell学习教程(超详细完整)[通俗易懂]为了方便以后工作和复习,记录一下学习shell脚本的笔记,看这篇文章需要对linux系统熟悉文章目录一、什么是shell?为什么要学习和使用shell?二、shell的分类shell脚本的执行三、shell变量1.变量的命名规则:2.变量的分类:2.1用户自定义变量:2.1.1变量定义2.1.2变量调用2.1.3变量查看2.1.4变量删除2.2环境变量:1)环境变量设置2)环境变量查询和删除3)系统默认环境变量2.3位置参数变量:2.4预定义变量:3.只读变量:4.接受键盘输入:四、shell运算符1.

    2022年8月18日
  • Java实现水仙花代码「建议收藏」

    Java实现水仙花代码「建议收藏」Java实现水仙花数简单代码//代码如下importjava.util.*;publicclassShuiXianHua{publicstaticvoidmain(String[]args){System.out.println(“判断水仙花数”);inti,j,k=0;//i是个位…

    2022年10月29日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号