设为首页 收藏本站
云服务器等爆品抢先购,低至4.2元/月
查看: 1061|回复: 0

在java程序中跨平台运行shell命令或者脚本,并获得其输出(转)

[复制链接]

尚未签到

发表于 2017-5-16 11:46:13 | 显示全部楼层 |阅读模式
下文所述的两个试验在Linux AS3和Jdk5上测试通过。
一、在java程序中跨平台运行shell命令或者脚本,并获得其输出
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TestCMD {
/**
* @param args
*/
public static void main(String[] args) {
try {
Process child = null;
String systemType = System.getProperty("os.name");
if (systemType.equals("Linux")) {
System.out.println("Linux OS!");
child = Runtime.getRuntime().exec("jmap -h", null, null);
} else if (systemType.indexOf("Windows") > -1) {
// If the os is windows, you need to add cmd before command.
System.out.println("Windows OS!");
child = Runtime.getRuntime().exec("cmd java -version", null,
null);
}
// Get the input stream and read from it
BufferedReader in = new BufferedReader(new InputStreamReader(child
.getInputStream()));
String c = null;
while ((c = in.readLine()) != null) {
System.out.println(c);
}
in.close();
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}

二、推广:根据某个进程特殊的tag获得其进程ID
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
* @(#)TestGetPid.java 2008-5-21
*
* Copyright (c) 2008
* All rights reserved.
* $
*/
/**
* <strong>Purpose:</strong><br>
* TODO.
*
* @version 1.0.1 2008-5-21<br>
* @author AxisThinker<br>
* @email ddjoke@163.com
*/
public class TestGetPid {
/**
* @param args
*/
public static void main(String[] args) {
try {
Process child = null;
String systemType = System.getProperty("os.name");
if (systemType.equals("Linux")) {
// If the os is linux, you need to add bash before command.
System.out.println("Support Linux os!");
child = Runtime.getRuntime().exec("ps -Af", null, null);
} else if (systemType.indexOf("Windows") > -1) {
System.out.println("Not support windows!");
}
// Get the input stream and read from it
BufferedReader in = new BufferedReader(new InputStreamReader(child
.getInputStream()));
String c = null;
// Only get the first line of bash's output.
while ((c = in.readLine()) != null) {
//这里this.name=GOS.CORE就是我要找的某个进程的特殊tag,各位可以换成你想找的进程
if (c.contains("this.name=GOS.CORE")) {
c = c.substring(c.indexOf(" "), c.length());
c = c.trim();
c = c.substring(0, c.indexOf(" "));
c = c.trim();
System.out.println(c);
break;
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

三: 高级应用: 通过ssh的配置,远程调用其他主机的命令,并在本地得到其输出,中间不用输入密码
第一步:需要配置ssh。配置方法简要如下:
    首先,例如我想用本地的lxy账户登陆远程节点A的lxy1账户,那么首先进入本地/home/lxy目录,在这个目录下有一个隐藏目录.ssh,进入这个目录,利用ssh-keygen命令来生成本用户的公私钥对,可以利用-t参数来指定生成方法:一般用rsa方法。此时,会要求你输入key的密码,直接回车就可以。如果你输的话,就一定要输入4位以上的密码,而且,以后你用ssh的时候就要输入这个密码。所以,建议不输入。
    然后会在.ssh目录下生成两个文件,例如id_rsa和id_rsa.pub,利用vi拷贝id_rsa.pub中的内容到远程节点A的lxy1账户目录下的.ssh目录里面的authorized_keys文件里。这样,以后你在本地以lxy用户身份利用ssh命令用节点A的lxy1账号登陆A节点,就不用再输入密码了。而且,执行:ssh lxy1@A ls 就能直接返回远程节点A的/home/lxy1/目录结构,不用输入密码。
第二步:编写程序
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
*Author: AxisThinker
*Email: ddjoke@163.com
*/
public class TestCMD {
/**
* @param args
*/
public static void main(String[] args) {
try {
Process child = null;
String systemType = System.getProperty("os.name");
if (systemType.equals("Linux")) {
System.out.println("Linux OS!");
child = Runtime
.getRuntime()
.exec(
"ssh gos@10.61.0.215 bash -c \"$JAVA_HOME/bin/jmap -heap 10191\"");
//这里有两点需要注意:第一你要运行的命令需要用bash -c引导。这样是告诉shell,后面的是一条指令,并且要在远端解释环境变量;二是要用转义\"来讲这个指令包起来,否则的话远端会以为jmap是一个指令,-heap是一个指令等等。
} else if (systemType.indexOf("Windows") > -1) {
// If the os is windows, you need to add cmd before command.
System.out.println("Windows OS!");
child = Runtime.getRuntime().exec(
new String[] { "cmd", "java", "-version" }, null, null);
}
// Get the input stream and read from it
BufferedReader in = new BufferedReader(new InputStreamReader(child
.getInputStream()));
String c = null;
while ((c = in.readLine()) != null) {
System.out.println(c);
if (c.equals("XXX")) {//这里比较重要,由于c = in.readLine()容易发生阻塞,所以,这里用比较土的办法来解决,根据最后一行的返回值来决定其是否退出。
break;
}
}
in.close();
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-378139-1-1.html 上篇帖子: 【Tiptop ERP T】Tiptop GP與資料庫操作有關的Shell loadx/unloadx createdb解析 下篇帖子: 遭遇krnln.fnr,com.run,shell.fne,dp1.fne,eAPI.fne,internet.fne,RegEx.fnr,spec.fne等
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表