Commit 41472db6 by 钱炳权

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

parent 51383361
......@@ -46,7 +46,7 @@ public class AlfnetController {
}
/**
* 算法2 无参数型
* 算法2 机器学习的
*/
@RequestMapping(value = "/algorithmTwoStart", method = RequestMethod.POST)
public AjaxResult algorithmTwoStart(@RequestBody final AlgorithmOneCmdParams algorithmOneCmdParams) {
......
......@@ -29,11 +29,10 @@ public class TestServiceImpl implements TestService {
@Override
public void algorithmOneStart(AlgorithmOneCmdParams algorithmOneCmdParams) {
public void algorithmTwoStart(AlgorithmOneCmdParams algorithmOneCmdParams) {
TestControlTools.setIsRunning(true);
String aflnetCmd = getAlgorithmOneFinalCmd(algorithmOneCmdParams);
String pythonCmd = getAlgorithmOnePythonCmd(algorithmOneCmdParams);
String recodeCmd = getAlgorithmOneResultCmd(algorithmOneCmdParams);
String aflnetCmd = getAlgorithmTwoFinalCmd(algorithmOneCmdParams);
String pythonCmd = getAlgorithmTwoPythonCmd(algorithmOneCmdParams);
if (aflnetCmd == null) {
log.error("AlgorithmOne Cmd is null!");
throw new AflnetException("Cmd Generate error!");
......@@ -42,13 +41,9 @@ public class TestServiceImpl implements TestService {
log.error("pythonCmd Cmd is null!");
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);
try {
cmdTools.runProgramCmd(aflnetCmd,pythonCmd,recodeCmd);
cmdTools.runProgramCmd(aflnetCmd,pythonCmd);
} catch (JSchException e) {
throw new RuntimeException(e);
} catch (IOException e) {
......@@ -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()) {
case "dns":
return "dnsResultCmd(algorithmOneCmdParams)";
return "cd /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch && python main_python.py --SUT dnsmasq";
case "sip":
return "sipResultCmd(algorithmOneCmdParams)";
return "cd /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch && python main_python.py --SUT kamailio";
case "ftp":
return "ftpResultCmd(algorithmOneCmdParams)";
return "cd /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch && python main_python.py --SUT kamailio";
case "rtsp":
return "rtspResultCmd(algorithmOneCmdParams)";
return "cd /home/lddc/SHENYANLONG/SMGfuzz/RL_Pytorch && python main_python.py --SUT live555";
default:
return null;
//todo 增加其他协议 的格式
}
}
private String getAlgorithmOneFinalCmd(AlgorithmOneCmdParams algorithmOneCmdParams) {
private String getAlgorithmTwoFinalCmd(AlgorithmOneCmdParams algorithmOneCmdParams) {
switch (algorithmOneCmdParams.getProtopcol().toLowerCase()) {
case "dns":
return dnsCmd(algorithmOneCmdParams);
......@@ -195,7 +174,7 @@ public class TestServiceImpl implements TestService {
//todo 不同服务不同端口
@Override
public void algorithmTwoStart(AlgorithmOneCmdParams algorithmOneCmdParams) {
public void algorithmOneStart(AlgorithmOneCmdParams algorithmOneCmdParams) {
TestControlTools.setIsRunning(true);
String cmd = cmdTools.parse(algorithmOneCmdParams);
Date date = new Date();
......@@ -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";
log.info("The cmd is [{}]", finalCmd);
try {
cmdTools.runProgramCmd("source /etc/profile && " + finalCmd,"","");
cmdTools.runProgramCmd("source /etc/profile && " + finalCmd,"");
} catch (JSchException e) {
throw new RuntimeException(e);
} catch (IOException e) {
......
......@@ -18,6 +18,7 @@ public class CmdTools {
Boolean show = true;
Boolean send = false;
/**
* 运行不需要后台运行cmd
*/
......@@ -41,18 +42,17 @@ public class CmdTools {
* 运行需要后台运行cmd
* 通过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;
String host = aflnetSshProperties.getHost();
int port =aflnetSshProperties.getPort();
int port = aflnetSshProperties.getPort();
String user = aflnetSshProperties.getUser();
String password = aflnetSshProperties.getPassword();
// 手动创建对象实现连接
LinuxCommandUtil linuxCommandUtil = new LinuxCommandUtil();
linuxCommandUtil.connect(host, port, user, password);
linuxCommandUtil.execCmd(aflnetCmd);
// linuxCommandUtil.execCmdWithoutResults(pythonCmd);
// linuxCommandUtil.execCmdWithoutResults(recodeCmd);
linuxCommandUtil.execCmdWithoutResults(pythonCmd);
}
......@@ -130,6 +130,7 @@ public class CmdTools {
}
return cmd.toString();
}
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")) {
show = false;
......
......@@ -82,7 +82,6 @@ public class LinuxCommandUtil {
* 执行指令
*/
public void execCmd(String command) throws JSchException, IOException {
List<String> result = new ArrayList<>();
InputStream in = channelExec.getInputStream();
InputStream error = channelExec.getErrStream();
new Thread(new Runnable() {
......@@ -105,9 +104,24 @@ public class LinuxCommandUtil {
* 不需要结果执行指令
*/
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.connect();
}
public void outPutStream(InputStream in) {
Reader reader = new InputStreamReader(in);
BufferedReader bf = new BufferedReader(reader);
......@@ -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) {
Reader reader = new InputStreamReader(error);
BufferedReader bf = new BufferedReader(reader);
......
......@@ -21,12 +21,16 @@ aflnet:
web:
ip: 192.168.9.177
#aflnetssh:
# host: 192.168.31.53
# port: 22
# user: lddc
# password: sysulddc
aflnetssh:
host: 192.168.31.53
host: 172.17.4.151
port: 22
user: lddc
password: sysulddc
user: root
password: 123456
#aflnetssh:
# host: 192.168.31.133
# 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