.......
try{
Session session = new JSch().getSession(user, ip, port);
session.setPassword(pwd);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("userauth.gssapi-with-mic", "no");
session.connect();
ChannelExec exec = (ChannelExec) session.openChannel("exec");
exec.setCommand("ifconfig");//这里是你要执行的命令,部分命令不支持 , 具体自己执行下
ByteArrayOutputStream bao = new ByteArrayOutputStream();
exec.setOutputStream(bao);
ByteArrayOutputStream baerr = new ByteArrayOutputStream();
exec.setErrStream(baerr);
exec.connect();
while (!exec.isEOF())
;
String errmsg = new String(baerr.toByteArray(), "utf-8");
if (StringUtils.notNull(errmsg)) {
throw new RuntimeException(errmsg);
} else {
System.out.println(new String(bao.toByteArray(), "utf-8"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
//关闭session等操作
}
怎样在java代码中调用执行shell脚本【java代码运行sh命令 java执行java代码】// 用法:Runtime.getRuntime().exec("命令");
String shpath="/test/test.sh";//程序路径
Process process =null;
String command1 = “chmod 777 ” + shpath;
try {
Runtime.getRuntime().exec(command1 ).waitFor();
} catch (IOException e1) {
e1.printStackTrace();
}catch (InterruptedException e) {
e.printStackTrace();
}
String var="201102";/参数
String command2 = “/bin/sh ” + shpath + ” ” + var;
Runtime.getRuntime().exec(command2).waitFor();
关于java代码运行sh命令和java执行java代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- 云蹦迪无人直播间直播,云蹦迪网站
- 什么片子视频可以看,什么视频可以看所有电影
- oracle表存在报00942,oracle报错00903
- 目录怎么自动生成word2010,目录怎么自动生成参考文献
- go语言的运行慢 go 语言 性能
- chatgpt国外玩法,potato chat国际版
- excel怎么备注表格,excel表格备注怎么弄
- excel打印如何居中打印,excell打印怎么居中
- mysql怎么使用教程 mysql使用指南