新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

js调用linux命令 js执行本地shell命令 linux

怎么用java代码运行linux命令

以下方法支持Linux和windows两个系统的命令行调用。还用到了apache的lang工具包commons-lang3-3.1.jar来判断操作系统类型、也用到了和log4j-1.2.16.jar来打印日志。至于rm -rf 是否能成功删除文件,可以手动去调用命令行试试。

创新互联公司是一家专注于网站设计、做网站与策划设计,桂林网站建设哪家好?创新互联公司做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:桂林等地区。桂林做网站价格咨询:028-86922220

private String callCmd(String cmd) throws InterruptedException, UnHandledOSException, ExecuteException {

if(SystemUtils.IS_OS_LINUX){

try {

// 使用Runtime来执行command,生成Process对象

Process process = Runtime.getRuntime().exec(

new String[] { "/bin/sh", "-c", cmd });

int exitCode = process.waitFor();

// 取得命令结果的输出流

InputStream is = process.getInputStream();

// 用一个读输出流类去读

InputStreamReader isr = new InputStreamReader(is);

// 用缓冲器读行

BufferedReader br = new BufferedReader(isr);

String line = null;

StringBuilder sb = new StringBuilder();

while ((line = br.readLine()) != null) {

System.out.println(line);

sb.append(line);

}

is.close();

isr.close();

br.close();

return sb.toString();

} catch (java.lang.NullPointerException e) {

System.err.println("NullPointerException " + e.getMessage());

logger.error(cmd);

} catch (java.io.IOException e) {

System.err.println("IOException " + e.getMessage());

}

throw new ExecuteException(cmd + "执行出错!");

}

if(SystemUtils.IS_OS_WINDOWS){

Process process;

try {

//process = new ProcessBuilder(cmd).start();

String[] param_array = cmd.split("[\\s]+");

ProcessBuilder pb = new ProcessBuilder(param_array);

process = pb.start();

/*process=Runtime.getRuntime().exec(cmd);*/

int exitCode = process.waitFor();

InputStream is = process.getInputStream();

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String line;

StringBuilder sb = new StringBuilder();

while ((line = br.readLine()) != null) {

System.out.println(line);

sb.append(line);

}

is.close();

isr.close();

br.close();

return sb.toString();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

throw new ExecuteException(cmd + "执行出错!");

}

throw new UnHandledOSException("不支持本操作系统");

}

linux下c/c++怎么调用js api

system(执行shell 命令)

相关函数 fork,execve,waitpid,popen

表头文件 #includestdlib.h

定义函数 int system(const char * string);

函数说明 system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。

返回值 如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。若参数string为空指针(NULL),则返回非零值。如果system()调用成功则最后会返回执行shell命令后的返回值,但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因此最好能再检查errno 来确认执行成功。

附加说明 在编写具有SUID/SGID权限的程序时请勿使用system(),system()会继承环境变量,通过环境变量可能会造成系统安全的问题。

范例 #includestdlib.h

main()

{

system(“ls -al /etc/passwd /etc/shadow”);

}

执行 -rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd

-r--------- 1 root root 572 Sep 2 15 :34 /etc/shadow

java程序执行linux命令

首先确保Linux开启sshd服务,并支持远程SSH连接。java程序使用jsch框架登录Linux,执行命令。

protected void creation() throws Exception {

JSch jsch = new JSch();

session = jsch.getSession(userName, host, port);

session.setPassword(password);

Properties config = new Properties();

config.put("StrictHostKeyChecking", "no");

session.setConfig(config);

session.setTimeout(CONNECT_TIMEOUT);

session.setConfig("PreferredAuthentications", "password,keyboard-interactive");

session.setServerAliveInterval(1000 * 60 * 2);

session.connect();

}

public String sendCommand(String command) throws Exception {

if(!isConnected())

throw new JSchException("Session is not connected, command exec faild.");

final ChannelExec exec = (ChannelExec)session.openChannel("exec");

ByteArrayOutputStream out = new ByteArrayOutputStream();

exec.setCommand(command);

exec.setOutputStream(out);

exec.setExtOutputStream(out);

exec.connect();

final Thread thread = new Thread() {

public void run() {

while(!exec.isEOF()) {

try { Thread.sleep(500L); } catch(Exception e) {}

}

}

};

thread.setDaemon(true);

thread.start();

thread.join(EXEC_TIMEOUT);

thread.interrupt();

if(thread.isAlive()) {

throw new JSchException("Exec Time Out Error");

} else {

try {

exec.disconnect();

out.close();

} catch (Exception e) {

}

byte[] lens = out.toByteArray();

String result = new String(lens, charset);

if(result.startsWith("bash") result.indexOf("command not found") != -1)

return "";

return result;

}

}


文章名称:js调用linux命令 js执行本地shell命令 linux
本文路径:http://www.wjwzjz.com/article/hpogjp.html
在线咨询
服务热线
服务热线:028-86922220
TOP