运行环境:1.linux系统 2. ssm框架 要求:jsp从页面输入一个IP地址以及选择一个数字,数字从1-8,把它们传到home下的shell脚本中,写出完整代码和代码所在的文件名,以及运行结果
时间: 2024-05-12 19:16:34 浏览: 72
代码:
1. jsp页面:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/executeShell" method="post">
IP地址:<input type="text" name="ip"><br>
选择数字:<select name="number">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select><br>
<input type="submit" value="执行">
</form>
</body>
</html>
```
2. Controller:
```java
package com.example.demo.controller;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HomeController {
@PostMapping("/executeShell")
public String executeShell(@RequestParam("ip") String ip, @RequestParam("number") String number, Model model) {
String[] cmd = { "/bin/sh", "-c", "/home/xxx/test.sh " + ip + " " + number };
String result = "";
try {
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
result += line + "<br>";
}
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("result", result);
return "result";
}
}
```
3. Shell脚本:
```sh
#!/bin/bash
ip=$1
number=$2
echo "IP地址为:$ip"
echo "选择数字为:$number"
```
4. 运行结果:
在执行完毕后,返回一个result.jsp页面,页面中显示如下内容:
IP地址为:xxx.xxx.xxx.xxx
选择数字为:x
其中,xxx.xxx.xxx.xxx为输入的IP地址,x为选择的数字。
阅读全文