Commit 41472db6 by 钱炳权

ssh至服务器并用websocket返回运行数据

parent 51383361
...@@ -46,7 +46,7 @@ public class AlfnetController { ...@@ -46,7 +46,7 @@ public class AlfnetController {
} }
/** /**
* 算法2 无参数型 * 算法2 机器学习的
*/ */
@RequestMapping(value = "/algorithmTwoStart", method = RequestMethod.POST) @RequestMapping(value = "/algorithmTwoStart", method = RequestMethod.POST)
public AjaxResult algorithmTwoStart(@RequestBody final AlgorithmOneCmdParams algorithmOneCmdParams) { public AjaxResult algorithmTwoStart(@RequestBody final AlgorithmOneCmdParams algorithmOneCmdParams) {
......
...@@ -29,11 +29,10 @@ public class TestServiceImpl implements TestService { ...@@ -29,11 +29,10 @@ public class TestServiceImpl implements TestService {
@Override @Override
public void algorithmOneStart(AlgorithmOneCmdParams algorithmOneCmdParams) { public void algorithmTwoStart(AlgorithmOneCmdParams algorithmOneCmdParams) {
TestControlTools.setIsRunning(true); TestControlTools.setIsRunning(true);
String aflnetCmd = getAlgorithmOneFinalCmd(algorithmOneCmdParams); String aflnetCmd = getAlgorithmTwoFinalCmd(algorithmOneCmdParams);
String pythonCmd = getAlgorithmOnePythonCmd(algorithmOneCmdParams); String pythonCmd = getAlgorithmTwoPythonCmd(algorithmOneCmdParams);
String recodeCmd = getAlgorithmOneResultCmd(algorithmOneCmdParams);
if (aflnetCmd == null) { if (aflnetCmd == null) {
log.error("AlgorithmOne Cmd is null!"); log.error("AlgorithmOne Cmd is null!");
throw new AflnetException("Cmd Generate error!"); throw new AflnetException("Cmd Generate error!");
...@@ -42,13 +41,9 @@ public class TestServiceImpl implements TestService { ...@@ -42,13 +41,9 @@ public class TestServiceImpl implements TestService {
log.error("pythonCmd Cmd is null!"); log.error("pythonCmd Cmd is null!");
throw new AflnetException("Cmd Generate error!"); throw new AflnetException("Cmd Generate error!");
} }
if (recodeCmd == null) {
log.error("recodeCmd Cmd is null!");
throw new AflnetException("Cmd Generate error!");
}
log.info("The cmd is [{}]", aflnetCmd); log.info("The cmd is [{}]", aflnetCmd);
try { try {
cmdTools.runProgramCmd(aflnetCmd,pythonCmd,recodeCmd); cmdTools.runProgramCmd(aflnetCmd,pythonCmd);
} catch (JSchException e) { } catch (JSchException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (IOException e) { } catch (IOException e) {
...@@ -57,40 +52,24 @@ public class TestServiceImpl implements TestService { ...@@ -57,40 +52,24 @@ public class TestServiceImpl implements TestService {
} }
private String getAlgorithmOneResultCmd(AlgorithmOneCmdParams algorithmOneCmdParams) {
switch (algorithmOneCmdParams.getProtopcol().toLowerCase()) {
case "dns":
return "cd /home/lddc/SHENYANLONG/SMGfuzz && python Copy_fuzz_stats.py --SUT dnsmasq --ALG RLGfuzz";
case "sip":
return "cd /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch && python Copy_fuzz_stats.py --SUT kamailio --ALG RLGfuzz";
case "ftp":
return "cd /home/lddc/SHENYANLONG/SMGfuzz && python Copy_fuzz_stats.py --SUT kamailio --ALG RLGfuzz";
case "rtsp":
return "cd /home/lddc/SHENYANLONG/SMGfuzz && python Copy_fuzz_stats.py --SUT live555 --ALG RLGfuzz";
default:
return null;
//todo 增加其他协议 的格式
}
}
private String getAlgorithmOnePythonCmd(AlgorithmOneCmdParams algorithmOneCmdParams) { private String getAlgorithmTwoPythonCmd(AlgorithmOneCmdParams algorithmOneCmdParams) {
switch (algorithmOneCmdParams.getProtopcol().toLowerCase()) { switch (algorithmOneCmdParams.getProtopcol().toLowerCase()) {
case "dns": case "dns":
return "dnsResultCmd(algorithmOneCmdParams)"; return "cd /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch && python main_python.py --SUT dnsmasq";
case "sip": case "sip":
return "sipResultCmd(algorithmOneCmdParams)"; return "cd /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch && python main_python.py --SUT kamailio";
case "ftp": case "ftp":
return "ftpResultCmd(algorithmOneCmdParams)"; return "cd /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch && python main_python.py --SUT kamailio";
case "rtsp": case "rtsp":
return "rtspResultCmd(algorithmOneCmdParams)"; return "cd /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch && python main_python.py --SUT live555";
default: default:
return null; return null;
//todo 增加其他协议 的格式
} }
} }
private String getAlgorithmOneFinalCmd(AlgorithmOneCmdParams algorithmOneCmdParams) { private String getAlgorithmTwoFinalCmd(AlgorithmOneCmdParams algorithmOneCmdParams) {
switch (algorithmOneCmdParams.getProtopcol().toLowerCase()) { switch (algorithmOneCmdParams.getProtopcol().toLowerCase()) {
case "dns": case "dns":
return dnsCmd(algorithmOneCmdParams); return dnsCmd(algorithmOneCmdParams);
...@@ -195,7 +174,7 @@ public class TestServiceImpl implements TestService { ...@@ -195,7 +174,7 @@ public class TestServiceImpl implements TestService {
//todo 不同服务不同端口 //todo 不同服务不同端口
@Override @Override
public void algorithmTwoStart(AlgorithmOneCmdParams algorithmOneCmdParams) { public void algorithmOneStart(AlgorithmOneCmdParams algorithmOneCmdParams) {
TestControlTools.setIsRunning(true); TestControlTools.setIsRunning(true);
String cmd = cmdTools.parse(algorithmOneCmdParams); String cmd = cmdTools.parse(algorithmOneCmdParams);
Date date = new Date(); Date date = new Date();
...@@ -204,7 +183,7 @@ public class TestServiceImpl implements TestService { ...@@ -204,7 +183,7 @@ public class TestServiceImpl implements TestService {
String finalCmd = CmdConstent.RUN_AFLNET_BEFORE + outputFileName + CmdConstent.RUN_AFLNET_AFTER + cmd + aflnetProperties.getAflnetPath() + "live555/testProgs/testOnDemandRTSPServer 8554"; String finalCmd = CmdConstent.RUN_AFLNET_BEFORE + outputFileName + CmdConstent.RUN_AFLNET_AFTER + cmd + aflnetProperties.getAflnetPath() + "live555/testProgs/testOnDemandRTSPServer 8554";
log.info("The cmd is [{}]", finalCmd); log.info("The cmd is [{}]", finalCmd);
try { try {
cmdTools.runProgramCmd("source /etc/profile && " + finalCmd,"",""); cmdTools.runProgramCmd("source /etc/profile && " + finalCmd,"");
} catch (JSchException e) { } catch (JSchException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (IOException e) { } catch (IOException e) {
......
...@@ -18,6 +18,7 @@ public class CmdTools { ...@@ -18,6 +18,7 @@ public class CmdTools {
Boolean show = true; Boolean show = true;
Boolean send = false; Boolean send = false;
/** /**
* 运行不需要后台运行cmd * 运行不需要后台运行cmd
*/ */
...@@ -41,18 +42,17 @@ public class CmdTools { ...@@ -41,18 +42,17 @@ public class CmdTools {
* 运行需要后台运行cmd * 运行需要后台运行cmd
* 通过websocket返回数据 * 通过websocket返回数据
*/ */
public void runProgramCmd(String aflnetCmd,String pythonCmd,String recodeCmd) throws AflnetException, JSchException, IOException { public void runProgramCmd(String aflnetCmd, String pythonCmd) throws AflnetException, JSchException, IOException {
AflnetSshProperties aflnetSshProperties = GlobalParameters.aflnetSshProperties; AflnetSshProperties aflnetSshProperties = GlobalParameters.aflnetSshProperties;
String host = aflnetSshProperties.getHost(); String host = aflnetSshProperties.getHost();
int port =aflnetSshProperties.getPort(); int port = aflnetSshProperties.getPort();
String user = aflnetSshProperties.getUser(); String user = aflnetSshProperties.getUser();
String password = aflnetSshProperties.getPassword(); String password = aflnetSshProperties.getPassword();
// 手动创建对象实现连接 // 手动创建对象实现连接
LinuxCommandUtil linuxCommandUtil = new LinuxCommandUtil(); LinuxCommandUtil linuxCommandUtil = new LinuxCommandUtil();
linuxCommandUtil.connect(host, port, user, password); linuxCommandUtil.connect(host, port, user, password);
linuxCommandUtil.execCmd(aflnetCmd); linuxCommandUtil.execCmd(aflnetCmd);
// linuxCommandUtil.execCmdWithoutResults(pythonCmd); linuxCommandUtil.execCmdWithoutResults(pythonCmd);
// linuxCommandUtil.execCmdWithoutResults(recodeCmd);
} }
...@@ -130,6 +130,7 @@ public class CmdTools { ...@@ -130,6 +130,7 @@ public class CmdTools {
} }
return cmd.toString(); return cmd.toString();
} }
public TestReturnEntity makeReturnEntity(String line, TestReturnEntity returnEntity) { public TestReturnEntity makeReturnEntity(String line, TestReturnEntity returnEntity) {
if (line.contains("All set and ready to roll!") || line.contains("american fuzzy") || line.contains("process timing overall results")) { if (line.contains("All set and ready to roll!") || line.contains("american fuzzy") || line.contains("process timing overall results")) {
show = false; show = false;
......
...@@ -82,7 +82,6 @@ public class LinuxCommandUtil { ...@@ -82,7 +82,6 @@ public class LinuxCommandUtil {
* 执行指令 * 执行指令
*/ */
public void execCmd(String command) throws JSchException, IOException { public void execCmd(String command) throws JSchException, IOException {
List<String> result = new ArrayList<>();
InputStream in = channelExec.getInputStream(); InputStream in = channelExec.getInputStream();
InputStream error = channelExec.getErrStream(); InputStream error = channelExec.getErrStream();
new Thread(new Runnable() { new Thread(new Runnable() {
...@@ -105,9 +104,24 @@ public class LinuxCommandUtil { ...@@ -105,9 +104,24 @@ public class LinuxCommandUtil {
* 不需要结果执行指令 * 不需要结果执行指令
*/ */
public void execCmdWithoutResults(String command) throws JSchException, IOException { public void execCmdWithoutResults(String command) throws JSchException, IOException {
InputStream in = channelExec.getInputStream();
InputStream error = channelExec.getErrStream();
new Thread(new Runnable() {
@Override
public void run() {
trainOutPutStream(in);
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
errorStream(error);
}
}).start();
channelExec.setCommand(command); channelExec.setCommand(command);
channelExec.connect(); channelExec.connect();
} }
public void outPutStream(InputStream in) { public void outPutStream(InputStream in) {
Reader reader = new InputStreamReader(in); Reader reader = new InputStreamReader(in);
BufferedReader bf = new BufferedReader(reader); BufferedReader bf = new BufferedReader(reader);
...@@ -133,6 +147,24 @@ public class LinuxCommandUtil { ...@@ -133,6 +147,24 @@ public class LinuxCommandUtil {
} }
} }
public void trainOutPutStream(InputStream in) {
Reader reader = new InputStreamReader(in);
BufferedReader bf = new BufferedReader(reader);
String line = null;
try {
while ((line = bf.readLine()) != null && TestControlTools.getIsRunning()) {
System.out.println(line);
GlobalParameters.webSocket.appointSending("fuzzMaster", "webTrainResult", line);
}
//结束就关闭
if (!TestControlTools.getIsRunning()) {
close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void errorStream(InputStream error) { public void errorStream(InputStream error) {
Reader reader = new InputStreamReader(error); Reader reader = new InputStreamReader(error);
BufferedReader bf = new BufferedReader(reader); BufferedReader bf = new BufferedReader(reader);
......
...@@ -21,12 +21,16 @@ aflnet: ...@@ -21,12 +21,16 @@ aflnet:
web: web:
ip: 192.168.9.177 ip: 192.168.9.177
#aflnetssh:
# host: 192.168.31.53
# port: 22
# user: lddc
# password: sysulddc
aflnetssh: aflnetssh:
host: 192.168.31.53 host: 172.17.4.151
port: 22 port: 22
user: lddc user: root
password: sysulddc password: 123456
#aflnetssh: #aflnetssh:
# host: 192.168.31.133 # host: 192.168.31.133
# port: 22 # port: 22
......
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>websocket client</title>
<script type="text/javascript">
var ws1 = null;
var ws2 = null;
function myFunction() {
ws1 = new WebSocket("ws://192.168.31.133:8100/websocket/testResult/webTrainResult");
ws1.onmessage = function (evt) {
console.log(evt);
console.log(evt.data)
var context = '<div class="sendMsg">' +
'aflnet:<br/>'+
'aflnet<h3>'+evt.data+'</h3><br/>'+
' </div>';
document.getElementById("sendDiv").innerHTML = context;
};
ws1.onclose = function () {
// 关闭 websocket
alert("连接已关闭...");
};
}
</script>
<body onload="javascript:myFunction()">
<div id="sendDiv">
</div>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment