CiscoTool_v1/netmiko_Api/DeviceController.js

4226 lines
153 KiB
JavaScript

const { exec, spawn } = require("child_process");
const fs = require("fs");
const moment = require("moment/moment");
const Device = require("../models/DeviceModel");
const callPython = require("../utils/callPython");
const { io } = require("socket.io-client");
const callPythonUnsync = require("../utils/callPythonUnsync");
const test = require("../utils/autoTest/test");
let socket = io(process.env.SOCKET_SERVER);
// socket?.emit("newUser", "test");
// socket.on("sendListDeviceOn", (data) => {
// console.log("fds ", data)
// });
/**
* Controller kiem tra thong tin thiet bi
* @param {String} action Ten thao tac
* @param {Object} infoStation Thong tin station
* @param {Object} listLine Thong tin cua dong tren station
* @param {String} created_by Nguoi thuc hien thao tac
*/
// exports.testDevice = async function (req, res) {
// const { infoStation, listLine, created_by } = req.body;
// console.log("station", infoStation);
// res.json({ noti: "done" });
// try {
// test(infoStation, listLine, "test", created_by);
// } catch (error) {
// console.log(error);
// }
// };
/**
* Controller kiem tra che do hoat dong va kiem tra thong tin thiet bi tu dong
* @param {Object} infoStation Thong tin station
* @param {Object} listLine Thong tin cua dong tren station
*/
// exports.testDevice = async function (req, res) {
// let count = 0
// const main = async (req, res) => {
// count = count + 1
// const { infoStation, listLine, created_by } = req.body;
//
// let indexOutlet = listLine.line_number
// let selectAPC = infoStation.sta_APC1
// let APC_Port = infoStation.APC1_port
// try {
// socket?.emit("newUser", {email:"api", time: 9999999});
// const time = "./public/filesTest/" + Date.now().toString();
// if (parseInt(listLine.line_number) > 8) {
// indexOutlet = parseInt(listLine.line_number) - 10
// selectAPC = infoStation.sta_APC2
// APC_Port = infoStation.APC2_port
// }
// let reboot_sh = "./utils/break_router/APC_control.sh " + selectAPC + " " + indexOutlet + " " + APC_Port
// let fileName = "./utils/check_mode.sh " + infoStation.sta_ip + " " + listLine.line_port
// spawn(process.env.PYTHON_FORMAT, ["./utils/netmiko_test.py",
// "clear line " + listLine.line_clear,
// infoStation.sta_ip,
// infoStation.sta_port_telnet,
// "clear"], { timeout: 14000 });
// setTimeout(() => {
// socket?.emit("changeAction", {
// id_line: listLine.id_line,
// action: "Auto-test(check)"
// })
// exec(
// fileName,
// (error, stdout, stderr) => {
// console.log(stdout);
// // if (stdout.search("foreign host") !== -1 || stderr.search("foreign host") !== -1) {
// // console.log("-----APC is being used------");
// console.log(stderr);
// if (stdout.search("switch:") !== -1 || stdout.search("rommon") !== -1) {
// console.log("-----------ROMmon mode-------------");
// Device.addLog(
// listLine.id_line,
// time,
// "ROMMON",
// Date.now(),
// created_by,
// "",
// ""
// );
// fs.writeFile(time, "Device is in ROMmon mode\n" + stdout, function (err) {
// if (err) {
// return console.error(err);
// }
// console.log("Write log line " + listLine.line_number);
// });
// socket?.emit("send-process", {
// id: listLine.id_line, result: [{
// id_line: listLine.id_line,
// line: listLine.line_number,
// line_ip: listLine.line_ip,
// port: listLine.line_port,
// output: stdout,
// status: "ROMMON",
// sta_name: infoStation.sta_name,
// }]
// })
// } else {
// if (stdout.search("Password") !== -1 || stdout.search("Username:") !== -1) {
// console.log("--------PASSWORD----------");
// //clear line
// spawn(process.env.PYTHON_FORMAT, ["./utils/netmiko_test.py",
// "clear line " + listLine.line_clear,
// infoStation.sta_ip,
// infoStation.sta_port_telnet,
// "clear"], { timeout: 14000 });
// //stop 5s
// setTimeout(() => {
// console.log("*****REBOOT");
// //reboot line use APC
// socket?.emit("changeAction", {
// id_line: listLine.id_line,
// action: "Auto-test(Re-Boot)"
// })
// exec(
// reboot_sh,
// (error, stdout, stderr) => {
// console.log(stdout);
// //can't connect to APC
// if (stdout.search("foreign host") !== -1 || stderr.search("foreign host") !== -1) {
// // setTimeout(() => {
// // console.log("*****REBOOT");
// // exec(
// // reboot_sh,
// // (error, stdout, stderr) => {
// // console.log(stdout);
// // console.log(stderr);
// // })
// // console.log("*****CONFIGURE");
// // exec(
// // "./utils/break_router/station_control.sh " + infoStation.sta_ip + " " + listLine.line_clear,
// // (error, stdout, stderr) => {
// // console.log(stdout);
// // console.log(stderr);
// // })
// // setTimeout(() => {
// // console.log("*****SPAM");
// // exec(
// // "./utils/break_router/spam_spacebar.sh " + infoStation.sta_ip + " " + listLine.line_port,
// // (error, stdout, stderr) => {
// // console.log(stdout);
// // console.log(stderr);
// // })
// // }, 22000);
// // }, 60000);
// // main(req, res)
// } else {
// console.log("*****CONFIGURE");
// //run station control ----> clear and setting speed
// exec(
// "./utils/break_router/station_control.sh " + infoStation.sta_ip + " " + listLine.line_clear,
// (error, stdout, stderr) => {
// console.log(stdout);
// console.log(stderr);
// })
// setTimeout(() => {
// console.log("*****SPAM");
// //spam spacebar
// exec(
// "./utils/break_router/spam_spacebar.sh " + infoStation.sta_ip + " " + listLine.line_port,
// (error, stdout, stderr) => {
// console.log(stdout);
// console.log(stderr);
// if (stdout.search("rommon") !== -1) {
// setTimeout(() => {
// socket?.emit("changeAction", {
// id_line: listLine.id_line,
// action: "Auto-test(Break)"
// })
// exec(
// "./utils/break_router/break.sh " + infoStation.sta_ip + " " + listLine.line_port,
// (error, stdout, stderr) => {
// console.log(stdout);
// console.log(stderr);
// if (stdout !== "") {
// setTimeout(() => {
// socket?.emit("changeAction", {
// id_line: listLine.id_line,
// action: "Auto-test(Test)"
// })
// callPythonUnsync(infoStation, listLine, "test", created_by)
// }, 240000);
// }
// })
// }, 10000);
// } else {
// Device.addLog(
// listLine.id_line,
// time,
// "PASSWORD",
// Date.now(),
// created_by,
// "",
// ""
// );
// fs.writeFile(time, "Device is SWITCH! You need to reset device and perform break password.\n\n" + stdout, function (err) {
// if (err) {
// return console.error(err);
// }
// console.log("Write log line " + listLine.line_number);
// });
// socket?.emit("send-process", {
// id: listLine.id_line, result: [{
// id_line: listLine.id_line,
// line: listLine.line_number,
// line_ip: listLine.line_ip,
// port: listLine.line_port,
// output: stdout,
// status: "PASSWORD",
// sta_name: infoStation.sta_name,
// }]
// })
// }
// })
// // if(stdout!==""){
// // setTimeout(() => {
// // exec(
// // "./utils/break_router/break.sh " + infoStation.sta_ip + " " + listLine.line_port,
// // (error, stdout, stderr) => {
// // console.log(stdout);
// // console.log(stderr);
// // })
// // }, 5000);
// // }
// }, 22000);
// }
// console.log(stderr);
// })
// }, 5000);
// // exec(
// // reboot_sh,
// // (error, stdout, stderr) => {
// // console.log(stdout);
// // console.log(stderr);
// // })
// } else {
// if (stdout.search("show abc") !== -1) {
// console.log("--------GOOD----------");
// socket?.emit("changeAction", {
// id_line: listLine.id_line,
// action: "Auto-test(Test)"
// })
// callPythonUnsync(infoStation, listLine, "test", created_by)
// } else {
// if (stdout.length > 300) {
// console.log("--------BOOT----------");
// setTimeout(() => {
// main(req, res)
// }, 60000);
// // fs.writeFile(time, "Device is booting\n" + stdout, function (err) {
// // if (err) {
// // return console.error(err);
// // }
// // console.log("Write log line " + listLine.line_number);
// // });
// // Device.addLog(
// // listLine.id_line,
// // time,
// // "BOOT",
// // Date.now(),
// // created_by,
// // "",
// // ""
// // );
// // socket?.emit("send-process", {
// // id: listLine.id_line, result: [{
// // id_line: listLine.id_line,
// // line: listLine.line_number,
// // line_ip: listLine.line_ip,
// // port: listLine.line_port,
// // output: stdout,
// // status: "BOOT",
// // }]
// // })
// } else {
// console.log("--------ERROR----------");
// // if (count < 1) {
// // socket?.emit("changeAction", {
// // id_line: listLine.id_line,
// // action: "Auto-test(Re-Test)"
// // })
// // // spawn(process.env.PYTHON_FORMAT, ["./utils/netmiko_test.py",
// // // "clear line " + listLine.line_clear,
// // // infoStation.sta_ip,
// // // infoStation.sta_port_telnet,
// // // "clear"], { timeout: 14000 });
// // setTimeout(() => {
// // main(req, res)
// // count = count + 1
// // }, 25000);
// // }
// // else {
// fs.writeFile(time, "Error: Can't connect to device.\n" + stdout, function (err) {
// if (err) {
// return console.error(err);
// }
// console.log("Write log line " + listLine.line_number);
// });
// Device.addLog(
// listLine.id_line,
// time,
// "ERROR",
// Date.now(),
// created_by,
// "",
// ""
// );
// socket?.emit("send-process", {
// id: listLine.id_line, result: [{
// id_line: listLine.id_line,
// line: listLine.line_number,
// line_ip: listLine.line_ip,
// port: listLine.line_port,
// output: "Error: Can't connect to device.",
// status: "ERROR",
// sta_name: infoStation.sta_name,
// }]
// })
// // }
// }
// }
// }
// }
// // console.log("Kill process ", listen.pid);
// // process.kill(listen.pid)
// // exec("ps -ef | grep listen-reboot | grep -v grep | awk '{print $2}' | xargs kill");
// // }
// // setTimeout(() => {
// // if (stdout.search("switch:") === -1) {
// // // console.log(stdout.search("switch:"));
// // callPythonUnsync(infoStation, listLine, "test", "Admin")
// // }
// // }, 250000);
// });
// }, 5000);
// } catch (error) {
// console.log(error);
// }
// }
// res.json({ status: "auto-test" })
// main(req, res)
// };
exports.autoTest = async function (req, res) {
const { infoStation, listLine, created_by, mode } = req.body;
const date = Date.now();
const time =
"./public/filesTest/" +
moment(date).format("YYYYMMDD").toString() +
"-AUTO-Session." +
infoStation.sta_name +
".Port" +
listLine.line_number +
"-" +
moment(date).format("HH-mm-ss").toString() +
".log";
try {
socket?.emit("newUser", {email:"api", time: 9999999});
res.json({ status: "done" });
switch (mode) {
case "good":
console.log("sdfsdfsdfsdfsdfsdfsdfsdfsdfsdfds");
test(infoStation, listLine, "test", created_by);
break;
case "switch:":
const listCisco = fs.readFileSync("./utils/list_cisco.txt", "utf8");
let ios_image = "";
let listDevice = listCisco.split("\n");
// socket?.emit("changeAction", {
// id_line: listLine.id_line,
// action: "switch:(check)",
// });
// const checkPID = spawn(
// "python3",
// [
// "./utils/action/checkPID.py",
// "",
// infoStation.sta_ip,
// infoStation.sta_port_telnet,
// listLine.line_clear,
// listLine.line_number,
// ],
// { timeout: 30000 }
// );
// checkPID.stdout.on("data", (data) => {
// data = data.toString();
// console.log(data);
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "switch:(clear)",
});
const breakPass = spawn(
"python3",
[
"./utils/action/BreakPassword/Break_29xx_35xx_37xx.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
],
{ timeout: 150000 }
);
console.log("CLEAR*******");
breakPass.stdout.on("data", (data) => {
data = data.toString();
console.log("DATA********", data);
listDevice.map((u) => {
let temp = u.split("|||")[1];
if (data.search(temp.replace("\r", "")) !== -1 && temp.length > 5) {
ios_image = ios_image + u.split("|||")[2] + "\n";
}
});
//Boot khong loi
if (data.search("Error") === -1 && data.search("Failed") === -1) {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "switch:(boot)",
});
if (data.search("WS-C36") !== -1 || data.search("WS-C38") !== -1) {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "switch:(boot)",
});
setTimeout(() => {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "switch:(break)",
});
const configBreak = spawn(
"python3",
[
"./utils/action/BreakPassword/Break_36xx_38xx.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
],
{ timeout: 150000 }
);
console.log("BEGIN CONFIG BREAK");
configBreak.stdout.on("data", (data) => {
data = data.toString();
console.log("END CONFIG BREAK");
console.log("CONFIGURE********\n", data);
});
configBreak.stderr.on("data", (data) => {
data = data.toString();
console.log("ERROR********\n", data);
});
setTimeout(() => {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "switch:(test)",
});
console.log("*****Test ");
spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"show inv",
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: 18000 }
);
setTimeout(() => {
Device.addLog(
listLine.id_line,
time,
"Wait",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Device has been broken waiting for testing",
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: "Device has been broken waiting for testing",
status: "Wait",
},
],
});
}, 25000);
}, 310000);
}, 320000);
}
//auto test after 5m
if (
data.search("WS-C35") !== -1 ||
data.search("WS-C37") !== -1 ||
data.search("WS-C29") !== -1
) {
setTimeout(() => {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "switch:(test)",
});
spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"show inv",
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: 18000 }
);
setTimeout(() => {
Device.addLog(
listLine.id_line,
time,
"Wait",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Device has been broken waiting for testing",
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: "Device has been broken waiting for testing",
status: "Wait",
},
],
});
}, 25000);
}, 300000);
}
} else {
if (data.search("WS-C36") !== -1 || data.search("WS-C38") !== -1) {
if (data.search("Error loading") !== -1) {
if (
data
.slice(data.search("Error loading"), data.length - 2)
.search("done") !== -1
) {
//settimeout run script config in mode enable
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "switch:(boot)",
});
setTimeout(() => {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "switch:(break)",
});
// spawn(
// process.env.PYTHON_FORMAT,
// [
// "./utils/netmiko_test.py",
// "show inv",
// infoStation.sta_ip,
// listLine.line_port,
// "check",
// ],
// { timeout: 18000 }
// );
const configBreak = spawn(
"python3",
[
"./utils/action/BreakPassword/Break_36xx_38xx.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
],
{ timeout: 150000 }
);
console.log("BEGIN CONFIG BREAK");
configBreak.stdout.on("data", (data) => {
data = data.toString();
console.log("END CONFIG BREAK");
console.log("CONFIGURE********\n", data);
});
configBreak.stderr.on("data", (data) => {
data = data.toString();
console.log("ERROR********\n", data);
});
setTimeout(() => {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "switch:(test)",
});
console.log("*****Test ");
spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"show inv",
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: 18000 }
);
setTimeout(() => {
Device.addLog(
listLine.id_line,
time,
"Wait",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Device has been broken waiting for testing",
function (err) {
if (err) {
return console.error(err);
}
console.log(
"Write log line " + listLine.line_number
);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Device has been broken waiting for testing",
status: "Wait",
},
],
});
}, 20000);
}, 320000);
}, 320000);
} else {
// Device.addLog(
// listLine.id_line,
// time,
// "ERROR",
// Date.now(),
// created_by,
// "",
// ""
// );
// fs.writeFile(
// time,
// "IOS NOT FOUND+\n=======================================" +
// data,
// function (err) {
// if (err) {
// return console.error(err);
// }
// console.log("Write log line " + listLine.line_number);
// }
// );
// socket?.emit("send-process", {nameFile: time,
// id: listLine.id_line,
// result: [
// {
// id_line: listLine.id_line,
// line: listLine.line_number,
// sta_name: infoStation.sta_name,
// line_ip: listLine.line_ip,
// port: listLine.line_port,
// output:
// "IOS NOT FOUND+\n=======================================" +
// data,
// status: "ERROR",
// },
// ],
// });
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "loading IOS",
});
console.log(ios_image);
const loadIOS = spawn(
"python3",
[
"./utils/action/Install_IOS/Install_IOS_Switch.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
infoStation.gateway_line,
infoStation.netmask_line,
listLine.line_ip,
infoStation.sta_tftp,
ios_image?.split("\n")[0],
],
{ timeout: 1500000 }
);
loadIOS.stdout.on("data", (data) => {
data = data.toString();
console.log("DATA********", data);
if (
data.search("NO CONNECTION") !== -1 ||
data.search("Invalid") !== -1 || data.search("Load IOS failed") !== -1
) {
Device.addLog(
listLine.id_line,
time,
"ERROR",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Load IOS failed+\n=======================================" +
data,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Load IOS failed+\n=======================================" +
data,
status: "ERROR",
},
],
});
} else {
Device.addLog(
listLine.id_line,
time,
"loadingIOS",
Date.now(),
created_by,
"",
""
);
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: data,
status: "loadingIOS",
},
],
});
}
});
loadIOS.stderr.on("data", (data) => {
data = data.toString();
console.log("DATA********", data);
Device.addLog(
listLine.id_line,
time,
"ERROR",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Load IOS failed+\n=======================================" +
data,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Load IOS failed+\n=======================================" +
data,
status: "ERROR",
},
],
});
});
}
}
} else {
if (data.search("Error loading") !== -1) {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "loading IOS",
});
const loadIOS = spawn(
"python3",
[
"./utils/action/Install_IOS/Install_IOS_Switch.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
infoStation.gateway_line,
infoStation.netmask_line,
listLine.line_ip,
infoStation.sta_tftp,
ios_image?.split("\n")[0],
],
{ timeout: 1500000 }
);
loadIOS.stdout.on("data", (data) => {
data = data.toString();
console.log("DATA********", data);
if (
data.search("NO CONNECTION") !== -1 ||
data.search("Invalid") !== -1 || data.search("Load IOS failed") !== -1
) {
Device.addLog(
listLine.id_line,
time,
"ERROR",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Load IOS failed+\n=======================================" +
data,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Load IOS failed+\n=======================================" +
data,
status: "ERROR",
},
],
});
} else {
Device.addLog(
listLine.id_line,
time,
"loadingIOS",
Date.now(),
created_by,
"",
""
);
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: data,
status: "loadingIOS",
},
],
});
}
});
loadIOS.stderr.on("data", (data) => {
data = data.toString();
console.log("DATA********", data);
Device.addLog(
listLine.id_line,
time,
"ERROR",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Load IOS failed+\n=======================================" +
data,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Load IOS failed+\n=======================================" +
data,
status: "ERROR",
},
],
});
});
}
// Device.addLog(
// listLine.id_line,
// time,
// "ERROR",
// Date.now(),
// created_by,
// "",
// ""
// );
// fs.writeFile(
// time,
// "IOS NOT FOUND+\n=======================================" +
// data,
// function (err) {
// if (err) {
// return console.error(err);
// }
// console.log("Write log line " + listLine.line_number);
// }
// );
// socket?.emit("send-process", {nameFile: time,
// id: listLine.id_line,
// result: [
// {
// id_line: listLine.id_line,
// line: listLine.line_number,
// sta_name: infoStation.sta_name,
// line_ip: listLine.line_ip,
// port: listLine.line_port,
// output:
// "IOS NOT FOUND+\n=======================================" +
// data,
// status: "ERROR",
// },
// ],
// });
}
}
// }
});
breakPass.stderr.on("data", (data) => {
data = data.toString();
console.log("ERROR CLEAR", data);
if (data !== "") {
Device.addLog(
listLine.id_line,
time,
"ERROR",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"IOS NOT FOUND+\n=======================================" + data,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"IOS NOT FOUND+\n=======================================" +
data,
status: "ERROR",
},
],
});
}
});
breakPass.on("close", (code) => {
console.log(`Process exited with code ${code}`);
if (code === null) {
setTimeout(() => {
spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"show inv",
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: 18000 }
);
setTimeout(() => {
const inventory = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"show inv",
infoStation.sta_ip,
listLine.line_port,
"check",
],
{ timeout: 20000 }
);
inventory.stdout.on("data", (data) => {
data = data.toString();
if (
data.search("WS-C35") !== -1 ||
data.search("WS-C37") !== -1 ||
data.search("WS-C29") !== -1
) {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "switch:(test)",
});
console.log("*****Test ");
Device.addLog(
listLine.id_line,
time,
"Wait",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Device has been broken waiting for testing",
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: "Device has been broken waiting for testing",
status: "Wait",
},
],
});
} else {
if (
data.search("WS-C36") !== -1 ||
data.search("WS-C38") !== -1
) {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "switch:(boot)",
});
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "switch:(break)",
});
const configBreak = spawn(
"python3",
[
"./utils/action/BreakPassword/Break_36xx_38xx.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
],
{ timeout: 150000 }
);
console.log("BEGIN CONFIG BREAK");
configBreak.stdout.on("data", (data) => {
data = data.toString();
console.log("END CONFIG BREAK");
console.log("CONFIGURE********\n", data);
setTimeout(() => {
setTimeout(() => {
Device.addLog(
listLine.id_line,
time,
"Wait",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Device has been broken waiting for testing",
function (err) {
if (err) {
return console.error(err);
}
console.log(
"Write log line " + listLine.line_number
);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Device has been broken waiting for testing",
status: "Wait",
},
],
});
}, 20000);
}, 300000);
});
configBreak.stderr.on("data", (data) => {
data = data.toString();
console.log("ERROR********\n", data);
});
}
}
});
inventory.on("close", (code) => {
if (code === null) {
Device.addLog(
listLine.id_line,
time,
"ERROR",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Break fail\n=======================================" +
code,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Break fail\n=======================================" +
code,
status: "ERROR",
},
],
});
}
});
}, 25000);
}, 300000);
}
});
// });
// checkPID.stderr.on("data", (data) => {
// data = data.toString();
// console.log(data);
// });
break;
case "rommon":
const listCisco1 = fs.readFileSync("./utils/list_cisco.txt", "utf8");
let PID1 = "";
let listDevice1 = listCisco1.split("\n");
let ios = "";
const checkPID1 = spawn(
"python3",
[
"./utils/action/checkPID.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
],
{ timeout: 200000 }
);
checkPID1.stdout.on("data", (data) => {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "rommon >(boot)",
});
data = data.toString();
console.log(data);
if(data.search("failed") || data.search("xxError")){
Device.addLog(
listLine.id_line,
time,
"ERROR",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Load IOS failed+\n=======================================" +
data,
function (err) {
if (err) {
return console.error(err);
}
console.log(
"Write log line " + listLine.line_number
);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Load IOS failed+\n=======================================" +
data,
status: "ERROR",
},
],
});
}else{
setTimeout(() => {
const checkPID2 = spawn(
"python3",
[
"./utils/action/checkPID.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
],
{ timeout: 200000 }
);
checkPID2.stdout.on("data", (data) => {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "rommon >(boot)",
});
data = data.toString();
console.log(data);
if (
data.search("rommon") !== -1 ||
data.search("failed") !== -1
) {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "LoadingIOS",
});
listDevice1.map((u) => {
let temp = u.split("|||")[1];
if (
data.search(temp.replace("\r", "")) !== -1 &&
temp.length > 5
) {
ios = ios + u.split("|||")[2] + "\n";
}
});
console.log("***IOS ",ios )
const loadIOS = spawn(
"python3",
[
"./utils/action/Install_IOS/Install_IOS_Router.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
infoStation.gateway_line,
infoStation.netmask_line,
listLine.line_ip,
infoStation.sta_tftp,
ios?.split("\n")[0],
],
{ timeout: 1500000 }
);
loadIOS.stdout.on("data", (data) => {
data = data.toString();
console.log(data);
if (data.search("Load IOS failed") !== -1 || data.search("TFTP file too large") !== -1) {
Device.addLog(
listLine.id_line,
time,
"ERROR",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Load IOS failed+\n=======================================" +
data,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Load IOS failed+\n=======================================" +
data,
status: "ERROR",
},
],
});
} else {
Device.addLog(
listLine.id_line,
time,
"loadingIOS",
Date.now(),
created_by,
"",
""
);
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: data,
status: "loadingIOS",
},
],
});
}
});
loadIOS.stderr.on("data", (data) => {
data = data.toString();
console.log(data);
Device.addLog(
listLine.id_line,
time,
"ERROR",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Load IOS failed+\n=======================================" +
data,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Load IOS failed+\n=======================================" +
data,
status: "ERROR",
},
],
});
});
} else {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "rommon >(break)",
});
const clearConfig = spawn(
"python3",
[
"./utils/action/BreakPassword/disableConfigASA.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
],
{ timeout: 20000 }
);
clearConfig.stdout.on("data", (data) => {
data = data.toString();
console.log(data);
});
setTimeout(() => {
const breakRouter = spawn(
"python3",
[
"./utils/action/BreakPassword/Break_Router.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
],
{ timeout: 500000 }
);
console.log("BEGIN BREAK ROUTER");
breakRouter.stdout.on("data", (data) => {
console.log("END BREAK ROUTER");
data = data.toString();
console.log(data);
if (data.search("rommon") !== -1) {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "LoadingIOS",
});
listDevice1.map((u) => {
let temp = u.split("|||")[1];
if (
data.search(temp.replace("\r", "")) !== -1 &&
temp.length > 5
) {
ios = ios + u.split("|||")[2] + "\n";
}
});
const loadIOS = spawn(
"python3",
[
"./utils/action/Install_IOS/Install_IOS_Router.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
infoStation.gateway_line,
infoStation.netmask_line,
listLine.line_ip,
infoStation.sta_tftp,
ios?.split("\n")[0],
],
{ timeout: 1500000 }
);
loadIOS.stdout.on("data", (data) => {
data = data.toString();
console.log(data);
if (data.search("Load IOS failed") !== -1) {
Device.addLog(
listLine.id_line,
time,
"ERROR",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Load IOS failed+\n=======================================" +
data,
function (err) {
if (err) {
return console.error(err);
}
console.log(
"Write log line " + listLine.line_number
);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Load IOS failed+\n=======================================" +
data,
status: "ERROR",
},
],
});
} else {
Device.addLog(
listLine.id_line,
time,
"loadingIOS",
Date.now(),
created_by,
"",
""
);
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log(
"Write log line " + listLine.line_number
);
});
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: data,
status: "loadingIOS",
},
],
});
}
});
loadIOS.stderr.on("data", (data) => {
data = data.toString();
console.log(data);
Device.addLog(
listLine.id_line,
time,
"ERROR",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Load IOS failed+\n=======================================" +
data,
function (err) {
if (err) {
return console.error(err);
}
console.log(
"Write log line " + listLine.line_number
);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Load IOS failed+\n=======================================" +
data,
status: "ERROR",
},
],
});
});
} else {
if (data.search("ASA") !== -1) {
setTimeout(() => {
const clearConfig = spawn(
"python3",
[
"./utils/action/BreakPassword/disableConfigASA.py",
"",
infoStation.sta_ip,
infoStation.sta_port_telnet,
listLine.line_clear,
listLine.line_number,
],
{ timeout: 30000 }
);
clearConfig.stdout.on("data", (data) => {
data = data.toString();
console.log(data);
Device.addLog(
listLine.id_line,
time,
"Wait",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Device has been broken waiting for testing",
function (err) {
if (err) {
return console.error(err);
}
console.log(
"Write log line " + listLine.line_number
);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Device has been broken waiting for testing",
status: "Wait",
},
],
});
});
}, 240000);
} else {
Device.addLog(
listLine.id_line,
time,
"Wait",
Date.now(),
created_by,
"",
""
);
fs.writeFile(
time,
"Device has been broken waiting for testing",
function (err) {
if (err) {
return console.error(err);
}
console.log(
"Write log line " + listLine.line_number
);
}
);
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
sta_name: infoStation.sta_name,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Device has been broken waiting for testing",
status: "Wait",
},
],
});
}
}
});
breakRouter.stderr.on("data", (data) => {
data = data.toString();
console.log(data);
});
breakRouter.on("close", (code) => {
console.log(`Process exited with code ${code}`);
});
}, 20000);
}
});
}, 240000);
}
});
checkPID1.stderr.on("data", (data) => {
data = data.toString();
console.log(data);
});
break;
case "password":
let selectAPC = infoStation.sta_APC1;
let APC_Port = infoStation.APC1_port;
let indexOutlet = parseInt(listLine.line_number);
if (parseInt(listLine.line_number) > 8) {
indexOutlet = parseInt(listLine.line_number) - 10;
selectAPC = infoStation.sta_APC2;
APC_Port = infoStation.APC2_port;
}
let reboot_sh =
"./utils/break_router/APC_control.sh " +
selectAPC +
" " +
indexOutlet +
" " +
APC_Port;
const reboot = () => {
// spawn(
// process.env.PYTHON_FORMAT,
// [
// "./utils/netmiko_test.py",
// "clear line " + listLine.line_clear,
// infoStation.sta_ip,
// infoStation.sta_port_telnet,
// "clear",
// ],
// { timeout: 14000 }
// );
//stop 5s
setTimeout(() => {
// console.log("*****REBOOT");
//reboot line use APC
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "Password(check)",
});
exec(reboot_sh, (error, stdout, stderr) => {
// console.log("*****CONFIGURE");
//run station control ----> clear and setting speed
console.log(stdout);
console.log(stderr);
console.log("*****LISTENING");
if (
stdout.search("foreign host") !== -1 ||
stderr.search("foreign host") !== -1
) {
setTimeout(() => {
reboot();
}, 60000);
} else {
// spawn(
// process.env.PYTHON_FORMAT,
// [
// "./utils/netmiko_test.py",
// "clear line " + listLine.line_clear,
// infoStation.sta_ip,
// infoStation.sta_port_telnet,
// "clear",
// ],
// { timeout: 14000 }
// );
setTimeout(() => {
// console.log("*****SPAM");
exec(
"./utils/break_router/station_control.sh " +
infoStation.sta_ip +
" " +
listLine.line_clear +
" " +
infoStation.sta_port_telnet,
(error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
}
);
//SUA CHO NAY
//spam spacebar
// setTimeout(() => {
// console.log("*****SPAM");
// exec(
// "./utils/break_router/spam_spacebar.sh " + infoStation.sta_ip + " " + listLine.line_port,
// (error, stdout, stderr) => {
// console.log(stdout);
// console.log(stderr);
// })
// }, 12000);
exec(
"./utils/listen-reboot.sh " +
infoStation.sta_ip +
" " +
infoStation.sta_port_telnet +
" " +
listLine.line_number +
" " +
listLine.line_clear,
(error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (stdout.search("rommon") !== -1) {
socket?.emit("changeAction", {
id_line: listLine.id_line,
action: "rommon >(clear)",
});
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: stdout,
// SN: temptemp.replace("\r", ""),
status: "PASSWORD_ROUTER",
sta_name: infoStation.sta_name,
},
],
});
} else {
// arrayDevice.map((u) => {
// let temp = u.split("|||")[1];
// if (
// stdout.search(temp.replace("\r", "")) !== -1 &&
// temp.length > 5
// ) {
// listPID =
// listPID +
// u.split("|||")[0] +
// " -- " +
// u.split("|||")[1] +
// "\n";
// }
// });
// let report =
// listPID !== ""
// ? "Device detected: \n" + listPID + "\n" + stdout
// : "Device not detected! \n" + "\n" + stdout;
Device.addLog(
listLine.id_line,
time,
"PASSWORD",
Date.now(),
created_by,
"",
""
);
fs.writeFile(time, stdout, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
socket?.emit("send-process", {
nameFile: time,
id: listLine.id_line,
result: [
{
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: stdout,
// SN: temptemp.replace("\r", ""),
status: "PASSWORD",
sta_name: infoStation.sta_name,
},
],
});
}
// }
}
);
}, 5000);
}
});
}, 5000);
};
reboot();
break;
//boot
}
} catch (error) {
console.log(error);
}
};
exports.powerControl = async function (req, res) {
const { infoStation, listLine, action, type, length } = req.body;
res.json({ status: "done" });
try {
let selectAPC = infoStation.sta_APC1;
let APC_Port = infoStation.APC1_port;
let scriptFile1 = "./utils/powerControl/connectPower.sh ";
let scriptFile2 = "./utils/powerControl/connectPower.sh ";
const connectAPC = (scriptFile) => {
exec(scriptFile, (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (
stdout.search("foreign host") !== -1 ||
stderr.search("foreign host") !== -1
) {
setTimeout(() => {
console.log("***RE-CONNECT");
connectAPC(scriptFile);
}, 30000);
} else {
console.log("***CONTROL POWER SUCCESS");
}
});
};
//tung line
if (type === "one") {
let indexOutlet = listLine.line_number;
if (parseInt(listLine.line_number) > 8) {
indexOutlet = parseInt(listLine.line_number) - 10;
selectAPC = infoStation.sta_APC2;
APC_Port = infoStation.APC2_port;
}
scriptFile1 =
scriptFile1 +
selectAPC +
" " +
indexOutlet +
" " +
APC_Port +
" " +
action;
connectAPC(scriptFile1);
}
//tat ca line trong 1 station
if (type === "all") {
scriptFile1 =
scriptFile1 +
infoStation.sta_APC1 +
" 9 " +
infoStation.APC1_port +
" " +
action;
scriptFile2 =
scriptFile2 +
infoStation.sta_APC2 +
" 9 " +
infoStation.APC2_port +
" " +
action;
if (length > 8) {
connectAPC(scriptFile1);
connectAPC(scriptFile2);
} else {
connectAPC(scriptFile1);
}
}
} catch (error) {
console.log(error);
}
};
/**
* Controller xoa mat khau cua dong thiet bi 2960/3560/3750
* @param {String} action Ten thao tac
* @param {Object} infoStation Thong tin station
* @param {Object} listLine Thong tin cua dong tren station
* @param {Integer} idGroup ID nhom chua model can be khoa
* @param {String} created_by Nguoi thuc hien thao tac
*/
exports.breakPassword_2960_3560_3750 = async function (req, res) {
const { action, infoStation, listLine, idGroup, created_by } = req.body;
socket?.emit("newUser", {email:"api", time: 9999999});
const result = [];
const time = "./public/filesTest/" + Date.now().toString();
let commands = [];
let contentFile =
"#!/usr/bin/expect\n\nspawn telnet " +
infoStation.sta_ip +
" " +
listLine.line_port +
"\r";
console.log("Connect API breakPassword_2960_3560_3750");
res.json({ status: "breaking" });
try {
const clearLine = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"clear line " + listLine.line_clear,
infoStation.sta_ip,
infoStation.sta_port_telnet,
"clear",
],
{ timeout: 14000 }
);
//clear success
clearLine.stdout.on("data", (data) => {
data = data.toString();
if (data.search("not available for clearing") !== -1) {
console.log("Loi: ", data);
result.push({
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: data,
status: "Break",
});
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
Device.addLog(
listLine.id_line,
time,
"Break",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", { id: listLine.id_line, result });
} else {
console.log("OK: ", data);
const checkPass = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/checkPassword.py",
"\n",
infoStation.sta_ip,
listLine.line_port,
],
{ timeout: 25000 }
);
checkPass.stdout.on("data", async (data) => {
data = data.toString();
console.log(data);
//no password
if (data.search("Pass") !== -1) {
//Device mode is not correct
result.push({
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: "Device mode is not correct",
status: "Break",
});
fs.writeFile(time, "Device mode is not correct", function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
Device.addLog(
listLine.id_line,
time,
"Break",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", { id: listLine.id_line, result });
} else {
//password
if (data.search("NetmikoAuthenticationException") !== -1) {
const res2 = await Device.getAllCmdOfGroup(idGroup);
res2.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
for (let i = 0; i < commands.length; i++) {
contentFile = contentFile + commands[i] + "\r";
}
let fileName =
"./utils/break/" +
listLine.id_line.toString() +
"break-password.sh";
fs.writeFile(fileName, contentFile, function (err) {
if (err) {
return console.error(err);
}
console.log("Write break file");
});
setTimeout(() => {
var yourscript = exec(fileName, (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(`exec error: ${error}`);
result.push({
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Break error: " +
stderr +
"\n" +
`exec error: ${error}`,
status: "Break",
});
fs.writeFile(
time,
"Break error: " + stderr + "\n" + `exec error: ${error}`,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
Device.addLog(
listLine.id_line,
time,
"Break",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", {
id: listLine.id_line,
result,
});
}
if (stdout !== "") {
result.push({
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: "Device mode is not correct",
status: "Break",
});
setTimeout(() => {
fs.writeFile(
time,
"Break success: " + stdout,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
Device.addLog(
listLine.id_line,
time,
"Break",
Date.now(),
created_by,
"",
""
);
callPythonUnsync(
infoStation,
listLine,
"test",
created_by
);
}, 360000);
}
});
}, 2000);
} else {
result.push({
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: data,
status: "Break",
});
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
}
}
});
}
});
//clear fail
clearLine.stderr.on("data", (data) => {
data = data.toString();
if (data !== "") {
console.log("Loi: ", data);
result.push({
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: data,
status: "Break",
});
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
Device.addLog(
listLine.id_line,
time,
"Break",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", { id: listLine.id_line, result });
}
});
// setTimeout(() => {
// if (result.length === 0) {
// result.push({
// id_line: listLine.id_line,
// line: listLine.line_number,
// line_ip: listLine.line_ip,
// port: listLine.line_port,
// output: "Time out",
// status: "Break",
// });
// fs.writeFile(time, "Time out", function (err) {
// if (err) {
// return console.error(err);
// }
// console.log("Write log line " + listLine.line_number);
// });
// Device.addLog(
// listLine.id_line,
// time,
// "Break",
// Date.now(),
// create_by,
// "",
// ""
// );
// socket?.emit("send-process", { id: listLine.id_line, result })
// }
// }, 30000);
} catch (error) {
console.log(error);
}
};
/**
* Controller xoa mat khau cua dong thiet bi 36xx/38xx
* @param {String} action Ten thao tac
* @param {Object} infoStation Thong tin station
* @param {Object} listLine Thong tin cua dong tren station
* @param {Integer} idGroup ID nhom chua model can be khoa
* @param {String} created_by Nguoi thuc hien thao tac
*/
exports.breakPassword_36_38 = async function (req, res) {
const { action, infoStation, listLine, idGroup, created_by } = req.body;
socket?.emit("newUser", {email:"api", time: 9999999});
const result = [];
const time = "./public/filesTest/" + Date.now().toString();
let commands = [];
let contentFile =
"#!/usr/bin/expect\n\nspawn telnet " +
infoStation.sta_ip +
" " +
listLine.line_port +
"\r";
console.log("Connect API breakPassword_36_38");
res.json({ status: "breaking" });
try {
const clearLine = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"clear line " + listLine.line_clear,
infoStation.sta_ip,
infoStation.sta_port_telnet,
"clear",
],
{ timeout: 14000 }
);
//clear success
clearLine.stdout.on("data", (data) => {
data = data.toString();
if (data.search("not available for clearing") !== -1) {
console.log("Loi: ", data);
result.push({
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: data,
status: "Break",
});
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
Device.addLog(
listLine.id_line,
time,
"Break",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", { id: listLine.id_line, result });
} else {
console.log("OK: ", data);
const checkPass = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/checkPassword.py",
"\n",
infoStation.sta_ip,
listLine.line_port,
],
{ timeout: 25000 }
);
checkPass.stdout.on("data", async (data) => {
data = data.toString();
console.log(data);
//no password
if (data.search("Pass") !== -1) {
//Device mode is not correct
result.push({
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: "Device mode is not correct",
status: "Break",
});
fs.writeFile(time, "Device mode is not correct", function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
Device.addLog(
listLine.id_line,
time,
"Break",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", { id: listLine.id_line, result });
} else {
//password
if (data.search("NetmikoAuthenticationException") !== -1) {
const res2 = await Device.getAllCmdOfGroup(idGroup);
res2.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
for (let i = 0; i < commands.length; i++) {
contentFile = contentFile + commands[i] + "\r";
}
let fileName =
"./utils/break/" +
listLine.id_line.toString() +
"break-password.sh";
fs.writeFile(fileName, contentFile, function (err) {
if (err) {
return console.error(err);
}
console.log("Write break file");
});
setTimeout(() => {
var yourscript = exec(fileName, (error, stdout, stderr) => {
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(`exec error: ${error}`);
result.push({
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output:
"Break error: " +
stderr +
"\n" +
`exec error: ${error}`,
status: "Break",
});
fs.writeFile(
time,
"Break error: " + stderr + "\n" + `exec error: ${error}`,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
}
);
Device.addLog(
listLine.id_line,
time,
"Break",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", {
id: listLine.id_line,
result,
});
}
if (stdout !== "") {
console.log("start break file");
spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"clear line " + listLine.line_clear,
infoStation.sta_ip,
infoStation.sta_port_telnet,
"clear",
],
{ timeout: 14000 }
);
spawn(
process.env.PYTHON_FORMAT,
[
"./utils/checkPassword.py",
"\n",
infoStation.sta_ip,
listLine.line_port,
],
{ timeout: 25000 }
);
const clearLineBreak = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"clear line " + listLine.line_clear,
infoStation.sta_ip,
infoStation.sta_port_telnet,
"clear",
],
{ timeout: 14000 }
);
clearLineBreak.stdout.on("data", (data) => {
data = data.toString();
console.log(data);
setTimeout(() => {
const breaking = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/breakPassword.py",
"\n",
infoStation.sta_ip,
listLine.line_port,
],
{ timeout: 500000 }
);
console.log("run break file ");
breaking.stdout.on("data", (data) => {
data = data.toString();
console.log(data.toString());
console.log("have data");
if (
data.toLocaleLowerCase().search("exception") !== -1
) {
setTimeout(() => {
const clearLineBreak = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"clear line " + listLine.line_clear,
infoStation.sta_ip,
infoStation.sta_port_telnet,
"clear",
],
{ timeout: 14000 }
);
clearLineBreak.stdout.on("data", (data) => {
data = data.toString();
console.log(data);
if (
data
.toLocaleLowerCase()
.search("exception") !== -1
) {
setTimeout(() => {
const breaking = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/breakPassword.py",
"\n",
infoStation.sta_ip,
listLine.line_port,
],
{ timeout: 500000 }
);
console.log("re-run break file ");
breaking.stdout.on("data", (data) => {
console.log(data.toString());
console.log("have data");
if (data !== "") {
console.log(data.toString());
setTimeout(() => {
fs.writeFile(
time,
"Break success: " + stdout,
function (err) {
if (err) {
return console.error(err);
}
console.log(
"Write log line " +
listLine.line_number
);
}
);
Device.addLog(
listLine.id_line,
time,
"Break",
Date.now(),
created_by,
"",
""
);
callPythonUnsync(
infoStation,
listLine,
"test",
created_by
);
}, 360000);
}
});
breaking.stderr.on("data", (data) => {
console.log(data.toString());
});
}, 30000);
} else {
console.log("no exception");
if (data !== "") {
console.log(data.toString());
console.log("***********");
setTimeout(() => {
fs.writeFile(
time,
"Break success: " + stdout,
function (err) {
if (err) {
return console.error(err);
}
console.log(
"Write log line " +
listLine.line_number
);
}
);
Device.addLog(
listLine.id_line,
time,
"Break",
Date.now(),
created_by,
"",
""
);
callPythonUnsync(
infoStation,
listLine,
"test",
created_by
);
}, 360000);
}
}
breaking.stderr.on("data", (data) => {
console.log(data.toString());
});
});
}, 30000);
} else {
console.log("no exception");
if (data !== "") {
console.log(data.toString());
console.log("***********");
setTimeout(() => {
fs.writeFile(
time,
"Break success: " + stdout,
function (err) {
if (err) {
return console.error(err);
}
console.log(
"Write log line " + listLine.line_number
);
}
);
Device.addLog(
listLine.id_line,
time,
"Break",
Date.now(),
created_by,
"",
""
);
callPythonUnsync(
infoStation,
listLine,
"test",
created_by
);
}, 360000);
}
}
});
breaking.stderr.on("data", (data) => {
console.log(data.toString());
});
}, 5000);
});
}
});
}, 2000);
} else {
result.push({
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: data,
status: "Break",
});
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
}
}
});
}
});
//clear fail
clearLine.stderr.on("data", (data) => {
data = data.toString();
if (data !== "") {
console.log("Loi: ", data);
result.push({
id_line: listLine.id_line,
line: listLine.line_number,
line_ip: listLine.line_ip,
port: listLine.line_port,
output: data,
status: "Break",
});
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + listLine.line_number);
});
Device.addLog(
listLine.id_line,
time,
"Break",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", { id: listLine.id_line, result });
}
});
} catch (error) {
console.log(error);
}
};
/**
* Controller update IOS image cho thiet bi
* @param {Object} infoStation Thong tin station
* @param {Object} infoLine Thong tin cua dong tren station
* @param {String} ios_image Ten IOS image can update
* @param {String} action Ten thao tac
* @param {Integer} idGroup ID nhom chua model can update
* @param {String} nameGroup Ten group chua model can update
* @param {String} created_by Nguoi thuc hien thao tac
*/
exports.updateIOS = async function (req, res) {
const {
infoStation,
infoLine,
ios_image,
action,
idGroup,
nameGroup,
created_by,
} = req.body;
socket?.emit("newUser", {email:"api", time: 9999999});
console.log("station", infoStation);
console.log("Update IOS");
res.json({ noti: "done" });
let result = [];
let commands = [];
let typeDevice = "";
if (nameGroup.toLocaleLowerCase().search("router") !== -1) {
typeDevice = "router";
} else {
if (nameGroup.toLocaleLowerCase().search("switch") !== -1) {
if (nameGroup.toLocaleLowerCase().search("3650/3850") !== -1) {
typeDevice = "switch_36_38";
} else {
if (nameGroup.toLocaleLowerCase().search("2960") !== -1) {
typeDevice = "switch_2960";
}
}
}
}
const time = "./public/filesTest/" + Date.now().toString();
try {
const clearLine = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"clear line " + infoLine.line_clear,
infoStation.sta_ip,
infoStation.sta_port_telnet,
"clear",
],
{ timeout: 14000 }
);
socket?.emit("changeAction", {
id_line: infoLine.id_line,
action: "UpdateIOS(Clear)",
});
clearLine.stdout.on("data", (data) => {
data = data.toString();
if (data.search("not available for clearing") !== -1) {
console.log("Loi: ", data);
result.push({
id_line: infoLine.id_line,
line: infoLine.line_number,
line_ip: infoLine.line_ip,
port: infoLine.line_port,
output: data,
status: "IOS",
});
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + infoLine.line_number);
});
Device.addLog(
infoLine.id_line,
time,
"IOS",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", { id: infoLine.id_line, result });
} else {
console.log("OK: ", data);
socket?.emit("changeAction", {
id_line: infoLine.id_line,
action: "UpdateIOS(Check-password)",
});
const checkPass = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/checkPassword.py",
"\n",
infoStation.sta_ip,
infoLine.line_port,
],
{ timeout: 25000 }
);
checkPass.stdout.on("data", async (data) => {
data = data.toString();
console.log(data);
//no password
if (data.search("Pass") !== -1) {
//Good
const res2 = await Device.getAllCmdOfGroup(idGroup);
res2.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
socket?.emit("changeAction", {
id_line: infoLine.id_line,
action: "UpdateIOS(configure)",
});
const updateIOS = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/updateIOS.py",
commands,
infoStation.sta_ip, //2
infoLine.line_port, //3
infoLine.line_ip, //4
infoStation.netmask_line, //5
infoStation.gateway_line, //6
ios_image, //7
typeDevice, //8
],
{ timeout: 50000000 }
);
console.log("===================");
updateIOS.stdout.on("data", async (data) => {
socket?.emit("changeAction", {
id_line: infoLine.id_line,
action: "UpdateIOS(update)",
});
data = data.toString();
console.log(data);
console.log("dataaaa");
if (data.search("reload") !== -1) {
result.push({
id_line: infoLine.id_line,
line: infoLine.line_number,
line_ip: infoLine.line_ip,
port: infoLine.line_port,
output: "Update IOS success: \nOutput: \n" + data,
status: "IOS",
});
fs.writeFile(
time,
"Update IOS success: \nOutput: \n" + data,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + infoLine.line_number);
}
);
Device.addLog(
infoLine.id_line,
time,
"IOS",
Date.now(),
created_by,
"",
""
);
if (data.search("Error") === -1) {
setTimeout(() => {
fs.writeFile(
time,
"Update IOS success: " + data,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + infoLine.line_number);
}
);
Device.addLog(
infoLine.id_line,
time,
"IOS",
Date.now(),
created_by,
"",
""
);
spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"clear line " + infoLine.line_clear,
infoStation.sta_ip,
infoStation.sta_port_telnet,
"clear",
],
{ timeout: 14000 }
);
spawn(
process.env.PYTHON_FORMAT,
[
"./utils/checkPassword.py",
"\n",
infoStation.sta_ip,
infoLine.line_port,
],
{ timeout: 25000 }
);
socket?.emit("changeAction", {
id_line: infoLine.id_line,
action: "UpdateIOS(test)",
});
callPythonUnsync(infoStation, infoLine, "test", created_by);
}, 360000);
}
} else {
result.push({
id_line: infoLine.id_line,
line: infoLine.line_number,
line_ip: infoLine.line_ip,
port: infoLine.line_port,
output: data,
status: "IOS",
});
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + infoLine.line_number);
});
Device.addLog(
infoLine.id_line,
time,
"IOS",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", { id: infoLine.id_line, result });
}
});
console.log("*****");
updateIOS.stderr.on("data", async (data) => {
data = data.toString();
console.log(data);
if (data !== "") {
result.push({
id_line: infoLine.id_line,
line: infoLine.line_number,
line_ip: infoLine.line_ip,
port: infoLine.line_port,
output: data,
status: "IOS",
});
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + infoLine.line_number);
});
Device.addLog(
infoLine.id_line,
time,
"IOS",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", { id: infoLine.id_line, result });
}
});
} else {
//password
spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"clear line " + infoLine.line_clear,
infoStation.sta_ip,
infoStation.sta_port_telnet,
"clear",
],
{ timeout: 14000 }
);
spawn(
process.env.PYTHON_FORMAT,
[
"./utils/checkPassword.py",
"\n",
infoStation.sta_ip,
infoLine.line_port,
],
{ timeout: 25000 }
);
setTimeout(() => {
const clearLine = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"clear line " + infoLine.line_clear,
infoStation.sta_ip,
infoStation.sta_port_telnet,
"clear",
],
{ timeout: 14000 }
);
clearLine.stdout.on("data", (data) => {
data = data.toString();
if (data.search("not available for clearing") !== -1) {
console.log("Loi: ", data);
result.push({
id_line: infoLine.id_line,
line: infoLine.line_number,
line_ip: infoLine.line_ip,
port: infoLine.line_port,
output: data,
status: "IOS",
});
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + infoLine.line_number);
});
Device.addLog(
infoLine.id_line,
time,
"IOS",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", {
id: infoLine.id_line,
result,
});
} else {
console.log("OK: ", data);
const checkPass = spawn(
process.env.PYTHON_FORMAT,
[
"./utils/checkPassword.py",
"\n",
infoStation.sta_ip,
infoLine.line_port,
],
{ timeout: 25000 }
);
}
});
checkPass.stdout.on("data", async (data) => {
data = data.toString();
console.log(data);
//no password
if (data.search("Pass") !== -1) {
//Good
const res2 = await Device.getAllCmdOfGroup(idGroup);
res2.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
const updateIOS = await spawn(
process.env.PYTHON_FORMAT,
[
"./utils/updateIOS.py",
commands,
infoStation.sta_ip, //2
infoLine.line_port, //3
infoLine.line_ip, //4
infoStation.netmask_line, //5
infoStation.gateway_line, //6
ios_image, //7
typeDevice, //8
],
{ timeout: 50000000 }
);
console.log("===================");
updateIOS.stdout.on("data", async (data) => {
data = data.toString();
console.log(data);
console.log("dataaaa");
if (data.search("reload") !== -1) {
result.push({
id_line: infoLine.id_line,
line: infoLine.line_number,
line_ip: infoLine.line_ip,
port: infoLine.line_port,
output: "Update IOS success: \nOutput: \n" + data,
status: "IOS",
});
fs.writeFile(
time,
"Update IOS success: \nOutput: \n" + data,
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + infoLine.line_number);
}
);
Device.addLog(
infoLine.id_line,
time,
"IOS",
Date.now(),
created_by,
"",
""
);
if (data.search("Error") === -1) {
setTimeout(() => {
fs.writeFile(
time,
"Update IOS success: " + data,
function (err) {
if (err) {
return console.error(err);
}
console.log(
"Write log line " + infoLine.line_number
);
}
);
Device.addLog(
infoLine.id_line,
time,
"IOS",
Date.now(),
created_by,
"",
""
);
spawn(
process.env.PYTHON_FORMAT,
[
"./utils/netmiko_test.py",
"clear line " + infoLine.line_clear,
infoStation.sta_ip,
infoStation.sta_port_telnet,
"clear",
],
{ timeout: 14000 }
);
spawn(
process.env.PYTHON_FORMAT,
[
"./utils/checkPassword.py",
"\n",
infoStation.sta_ip,
infoLine.line_port,
],
{ timeout: 25000 }
);
callPythonUnsync(
infoStation,
infoLine,
"test",
created_by
);
}, 360000);
}
}
});
console.log("*****");
updateIOS.stderr.on("data", async (data) => {
data = data.toString();
console.log(data);
if (data !== "") {
result.push({
id_line: infoLine.id_line,
line: infoLine.line_number,
line_ip: infoLine.line_ip,
port: infoLine.line_port,
output: data,
status: "IOS",
});
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + infoLine.line_number);
});
Device.addLog(
infoLine.id_line,
time,
"IOS",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", {
id: infoLine.id_line,
result,
});
}
});
} else {
if (data.search("NetmikoAuthenticationException") !== -1) {
result.push({
id_line: infoLine.id_line,
line: infoLine.line_number,
line_ip: infoLine.line_ip,
port: infoLine.line_port,
output:
"Line console password exist or device is not exist",
status: "IOS",
});
fs.writeFile(
time,
"Line console password exist or device is not exist",
function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + infoLine.line_number);
}
);
Device.addLog(
infoLine.id_line,
time,
"IOS",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", {
id: infoLine.id_line,
result,
});
}
}
});
}, 5000);
}
});
}
});
//clear fail
clearLine.stderr.on("data", (data) => {
data = data.toString();
if (data !== "") {
console.log("Loi: ", data);
result.push({
id_line: infoLine.id_line,
line: infoLine.line_number,
line_ip: infoLine.line_ip,
port: infoLine.line_port,
output: data,
status: "IOS",
});
fs.writeFile(time, data, function (err) {
if (err) {
return console.error(err);
}
console.log("Write log line " + infoLine.line_number);
});
Device.addLog(
infoLine.id_line,
time,
"IOS",
Date.now(),
created_by,
"",
""
);
socket?.emit("send-process", { id: infoLine.id_line, result });
}
});
} catch (error) {
console.log(error);
}
};
/**
* Controller lay tat ca cac loi duoc ngoai tru tu bang exclude_error
*/
exports.getAllExcludeError = async function (req, res) {
try {
const { pData, pMess, pCode } = await Device.getAllExcludeError();
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller them loi duoc ngoai tru vao bang exclude_error
* @param {String} message Noi dung cua loi
* @param {String} model Ten model duoc ap dung (id_model | All)
*/
exports.addExcludeError = async function (req, res) {
const { message, model } = req.body;
console.log(req.body);
try {
const { pMess, pCode } = await Device.addExcludeError(message, model);
res.json({ pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller xoa loi ngoai tru khoi bang exclude_error
* @param {Integer} id ID cua loi
*/
exports.deleteExcludeError = async function (req, res) {
const { id } = req.body;
try {
const { pMess, pCode } = await Device.deleteExcludeError(id);
res.json({ pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay thong tin cua tat ca station tu bang `station`
*/
exports.getAllStation = async function (req, res) {
try {
let listStation = [];
let listLine = [];
const { pData, pMess, pCode } = await Device.getAllStation();
res.json({ pData, pMess, pCode });
//getAllValue send to socket server
const listValue = await Device.getAllValue();
/* ID cac station
1 --> US1 01-08
2 --> AUS-dev
3 --> AUS-2
4 --> US2 11-18
5 --> AUS-1
pData[ID-1]
*/
// for (let i = 0; i < pData.length; i++) {
const res0 = await Device.getAllLineOfStation(pData[0].id_station);
// const res1 = await Device.getAllLineOfStation(pData[1].id_station);
const res2 = await Device.getAllLineOfStation(pData[2].id_station);
const res3 = await Device.getAllLineOfStation(pData[3].id_station);
const res4 = await Device.getAllLineOfStation(pData[4].id_station);
// listLine.push({line:{...res1.pData}})
listLine.push(res0.pData);
listStation.push(pData[0]);
// listLine.push(res1.pData);
// listStation.push(pData[1]);
listLine.push(res2.pData);
listStation.push(pData[2]);
listLine.push(res3.pData);
listStation.push(pData[3]);
listLine.push(res4.pData);
listStation.push(pData[4]);
// // }
// console.log(listStation)
// console.log(listLine)
socket?.emit("listenInfoStation", listStation);
socket?.emit("listenListLine", listLine);
// console.log(listLine.line);
} catch (error) {
console.log(error);
}
};
/**
* Controller them 1 line cho 1 station
* @param {String} number So thu tu cua line trong station
* @param {Integer} port Cong cua line
* @param {String} clear So hieu line dung de clear line tren station
* @param {String} ip IP cua line
* @param {Integer} station ID cua station can them line
*/
exports.addLineForStation = async function (req, res) {
const { number, port, clear, ip, station } = req.body;
console.log(req.body);
try {
const { pMess, pCode } = await Device.addLineForStation(
number,
port,
clear,
ip,
station
);
res.json({ pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay thong tin tat ca cac line cua 1 station
* @param {Integer} id ID cua station
*/
exports.getAllLineOfStation = async function (req, res) {
const { id } = req.body;
try {
const { pData, pMess, pCode } = await Device.getAllLineOfStation(id);
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller chinh sua thong tin cua line
* @param {Integer} id_line ID line can chinh sua
* @param {String} line_number So thu tu cua line trong station (new data|old data)
* @param {Integer} line_port Cong cua line (new data|old data)
* @param {String} line_clear So hieu line dung de clear line tren station (new data|old data)
* @param {String} line_ip IP cua line (new data|old data)
*/
exports.editLine = async function (req, res) {
const { id_line, line_number, line_port, line_clear, line_ip } = req.body;
try {
const { pMess, pCode } = await Device.editLine(
id_line,
line_number,
line_port,
line_clear,
line_ip
);
res.json({ pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller tao moi 1 nhom thiet bi
* @param {String} group_name Ten nhom
* @param {String} action Ten chuc nang cua nhom
* @param {String} created_by Tai khoan tao nhom
* @param {Array} model Mang cac model duoc them vao nhom
* @param {Array} command Mang cac cau lenh cua nhom
*/
exports.addGroup = async function (req, res) {
const { group_name, action, created_by, model, command } = req.body;
const time = Date.now();
console.log(req.body);
try {
const { pData, pMess, pCode } = await Device.addGroup(
group_name,
created_by,
time,
action
);
if (pCode === 1) {
for (let i = 0; i < command.length; i++) {
await Device.addCmdForGroup(pData.id_group, command[i]);
}
for (let i = 0; i < model.length; i++) {
await Device.addModelForGroup(pData.id_group, model[i]);
}
}
res.json({ pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller tao moi 1 model
* @param {String} id_model ID model (Ten model)
* @param {String} model_type Kieu cua model (Group|Single)
* @param {String} id_group ID nhom cua model (Khi model_type = Group)
* @param {Array} command Mang cac cau lenh cua model (Khi model_type = Single)
*/
exports.addModel = async function (req, res) {
const { id_model, model_type, id_group, command } = req.body;
console.log(req.body);
const time = Date.now();
let allModel = [];
try {
const { pData } = await Device.getAllModel();
for (let i = 0; i < pData.length; i++) {
allModel.push(pData[i].id_model.toLocaleLowerCase());
}
if (allModel.includes(id_model.toLocaleLowerCase())) {
res.json({ pMess: "Model exist", pCode: 0 });
} else {
if (model_type === "Group") {
const { pMess, pCode } = await Device.addModel(
id_model,
id_group,
model_type,
time
);
res.json({ pMess, pCode });
} else {
const { pMess, pCode } = await Device.addModel(
id_model,
null,
model_type,
time
);
if (command.length > 0) {
for (let i = 0; i < command.length; i++) {
await Device.addCmdForModel(id_model, command[i]);
}
}
res.json({ pMess, pCode });
}
}
} catch (error) {
console.log(error);
}
};
/**
* Controller lay thong tin tat ca cac nhom
*/
exports.getAllGroup = async function (req, res) {
try {
const { pData, pMess, pCode } = await Device.getAllGroup();
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay thong tin tat ca cac model
*/
exports.getAllModel = async function (req, res) {
try {
const { pData, pMess, pCode } = await Device.getAllModel();
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay thong tin tat ca cac model cua 1 nhom
* @param {Integer} id_group ID cua nhom can lay thong tin
*/
exports.getAllModelOfGroup = async function (req, res) {
const { id_group } = req.body;
try {
const { pData, pMess, pCode } = await Device.getAllModelOfGroup(id_group);
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay thong tin tat ca cac nhom cua 1 model
* @param {Integer} id_model ID cua model can lay thong tin
*/
exports.getAllGroupOfModel = async function (req, res) {
const { id_model } = req.body;
try {
const { pData, pMess, pCode } = await Device.getAllGroupOfModel(id_model);
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller kiem tra model da thuoc nhom nao co action tuong ung hay khong
* @param {Integer} id_model ID cua model can kiem tra
* @param {String} action Action can kiem tra
*/
exports.checkActionModel = async function (req, res) {
const { id_model, action } = req.body;
let check = 0;
let groupName = "";
try {
const { pData, pMess, pCode } = await Device.getAllGroupOfModel(id_model);
for (let i = 0; i < pData.length; i++) {
if (
pData[i].group_action.toLocaleLowerCase() === action.toLocaleLowerCase()
) {
//can't add model in group
groupName = pData[i].group_name;
check = 1;
}
}
res.json({ pCode: check, pMess: "check finish", group: groupName });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay thong tin tat ca cau lenh cua 1 nhom
* @param {Integer} id_group ID nhom can lay thong tin
*/
exports.getAllCmdOfGroup = async function (req, res) {
const { id_group } = req.body;
try {
const { pData, pMess, pCode } = await Device.getAllCmdOfGroup(id_group);
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay thong tin tat ca cau lenh cua 1 model
* @param {Integer} id_model ID model can lay thong tin
*/
exports.getAllCmdOfModel = async function (req, res) {
const { id_model } = req.body;
try {
const { pData, pMess, pCode } = await Device.getAllCmdOfModel(id_model);
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller chinh sua thong tin cua 1 nhom
* @param {Integer} id_group ID nhom can lay chinh sua
* @param {String} group_name Ten nhom (new data|old data)
* @param {Array} model_change Mang cac thao tac them/xoa cac model voi nhom
* @param {Array} cmd_change Mang cac thao tac them/xoa cac cau lenh voi nhom
*/
exports.editGroup = async function (req, res) {
const { id_group, group_name, model_change, cmd_change } = req.body;
console.log(req.body);
try {
const { pData, pMess, pCode } = await Device.editGroup(
id_group,
group_name
);
for (let i = 0; i < model_change.length; i++) {
if (model_change[i].status === "delete") {
await Device.deleteModelOfGroup(model_change[i].id, id_group);
} else {
await Device.addModelForGroup(id_group, model_change[i].id);
}
}
for (let j = 0; j < cmd_change.length; j++) {
if (cmd_change[j].status === "delete") {
await Device.deleteCmdOfGroup(cmd_change[j].mess, id_group);
} else {
await Device.addCmdForGroup(id_group, cmd_change[j].mess);
}
}
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller chinh sua thong tin cua 1 model
* @param {Integer} id_model ID nhom can lay chinh sua
* @param {String} type Kieu cua model (new data|old data)
* @param {String} type_init Kieu model truoc khi chinh sua
* @param {Array} group_change Mang cac thao tac them/xoa cac nhom voi model
* @param {Array} cmd_change Mang cac thao tac them/xoa cac cau nhom voi model
* @param {Array} groupOfModel Mang cac nhom cua model truoc khi chinh sua
*/
exports.editModel = async function (req, res) {
const { id_model, type, type_init, group_change, cmd_change, groupOfModel } =
req.body;
console.log(req.body);
try {
const { pData, pMess, pCode } = await Device.editModel(id_model, type);
//no change type
if (type === type_init) {
if (type === "Group") {
//update group of model
for (let i = 0; i < group_change.length; i++) {
if (group_change[i].status === "delete") {
await Device.deleteModelOfGroup(id_model, group_change[i].id);
} else {
await Device.addModelForGroup(group_change[i].id, id_model);
}
}
} else {
// update command of model
for (let j = 0; j < cmd_change.length; j++) {
if (cmd_change[j].status === "delete") {
await Device.deleteCmdOfModel(cmd_change[j].mess, id_model);
} else {
await Device.addCmdForModel(id_model, cmd_change[j].mess);
}
}
}
} else {
//change type model
if (type === "Single") {
// clear all group of model and add command for model
await Device.clearGroupOfModel(id_model);
for (let j = 0; j < cmd_change.length; j++) {
if (cmd_change[j].status === "delete") {
await Device.deleteCmdOfModel(cmd_change[j].mess, id_model);
} else {
await Device.addCmdForModel(id_model, cmd_change[j].mess);
}
}
} else {
// clear all command of model and add group for model
await Device.clearCmdOfModel(id_model);
for (let i = 0; i < group_change.length; i++) {
if (group_change[i].status === "delete") {
await Device.deleteModelOfGroup(id_model, group_change[i].id);
} else {
await Device.addModelForGroup(group_change[i].id, id_model);
}
}
}
}
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller xoa 1 nhom
* @param {Integer} id ID nhom can xoa
*/
exports.deleteGroup = async function (req, res) {
const { id } = req.body;
try {
const { pMess, pCode } = await Device.deleteGroup(id);
res.json({ pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller xoa 1 model
* @param {String} id ID model can xoa
*/
exports.deleteModel = async function (req, res) {
const { id } = req.body;
try {
const { pMess, pCode } = await Device.deleteModel(id);
res.json({ pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay thong tin tat ca cau lenh cua 1 model
* @param {String} id ID model can lay thong tin
* @param {String} action Ten thao tac cua nhom ma model o trong do
*/
exports.getCommandByModel = async function (req, res) {
const { id, action } = req.body;
let commands = [];
try {
const checkModel = await Device.getAllCmdOfModel(id);
if (checkModel.pData.length > 0) {
checkModel.pData.map((u) => {
commands.push(u.cmd_message);
});
} else {
const res = await Device.getAllGroupOfModel(id);
const group_info = res.pData.filter(
(item) =>
item.group_action.toLocaleLowerCase() === action.toLocaleLowerCase()
);
const res1 = await Device.getAllCmdOfGroup(group_info[0].id_group);
res1.pData.map((u) => {
commands.push(u.cmd_message);
});
console.log(commands);
}
res.json(commands);
} catch (error) {
console.log(error);
}
};
/**
* Controller chinh sua thong tin cua 1 station
* @param {Integer} id ID station can chinh sua
* @param {String} ip IP station
* @param {String} tftp IP may chu TFTP ap dung cho station
* @param {String} network Dia chi mang station
* @param {String} netmask Mat na mang station
* @param {String} gateway Default-gateway cua station
* @param {String} gateway_line Default-gateway cua cac line trong station
* @param {String} netmask_line Mat na mang cua cac line trong station
* @param {String} APC1 IP nguon 1 cua station
* @param {String} APC2 IP nguon 2 cua station
*/
exports.editStation = async function (req, res) {
const {
id,
ip,
tftp,
network,
netmask,
gateway,
gateway_line,
netmask_line,
APC1,
APC2,
portTelnet,
APC1_port,
APC2_port,
} = req.body;
console.log(req.body);
try {
const { pData, pMess, pCode } = await Device.editStation(
id,
ip,
tftp,
network,
netmask,
gateway,
gateway_line,
netmask_line,
APC1,
APC2,
portTelnet,
APC1_port,
APC2_port
);
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller thay doi che do hoat dong cua line
* @param {Integer} id ID line
* @param {Integer} status Ma trang thai cua line (1: enable|-1: disable)
*/
exports.enableLine = async function (req, res) {
const { id, status } = req.body;
console.log(req.body);
try {
const { pData, pMess, pCode } = await Device.enableLine(id, status);
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay thong tin tat ca cac log cuoi cung cua cac line trong 1 station
* @param {Integer} id_station ID station
*/
exports.getListLastLog = async function (req, res) {
const { id_station } = req.body;
let listLastLog = [];
try {
const allLine = await Device.getAllLineOfStation(id_station);
for (let i = 0; i < allLine.pData.length; i++) {
const allLog = await Device.getAllLogOfLine(allLine.pData[i].id_line);
if (allLog.pData[0] !== undefined) {
const data = fs.readFileSync(allLog.pData[0].log_content, "utf8");
listLastLog.push({
id_log: allLog.pData[0].id_log,
fileName: allLog.pData[0].log_content,
log_status: allLog.pData[0].log_status,
log_content: data,
log_line: allLog.pData[0].log_line,
PID: allLog.pData[0].PID,
SN: allLog.pData[0].SN,
command: allLog.pData[0].command,
created_at: allLog.pData[0].created_at,
created_by: allLog.pData[0].created_by,
});
}
}
res.json({ listLastLog: listLastLog });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay thong tin tat ca cac log cua 1 station
* @param {Integer} id_station ID station
*/
exports.getAllLogOfStation = async function (req, res) {
const { id_station } = req.body;
let listLog = [];
try {
const allLine = await Device.getAllLineOfStation(id_station);
for (let i = 0; i < allLine.pData.length; i++) {
const allLog = await Device.getAllLogOfLine(allLine.pData[i].id_line);
for (let j = 0; j < allLog.pData.length; j++) {
const data = fs.readFileSync(allLog.pData[j].log_content, "utf8");
listLog.push({
id_log: allLog.pData[j].id_log,
fileName: allLog.pData[j].log_content,
log_status: allLog.pData[j].log_status,
log_line: allLog.pData[j].log_line,
log_content: data,
PID: allLog.pData[j].PID,
SN: allLog.pData[j].SN,
command: allLog.pData[j].command,
line: allLine.pData[i].line_number,
created_at: allLog.pData[j].created_at,
created_by: allLog.pData[j].created_by,
});
}
}
res.json({ listLog: listLog });
} catch (error) {
console.log(error);
}
};
/**
* Controller thay doi command cua log
* @param {Integer} id ID log
* @param {String} command ID station
*/
exports.changeCommand = async function (req, res) {
const { id, command } = req.body;
try {
const { pMess, pCode } = await Device.changeCommand(id, command);
res.json({ pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay tat ca cac cap key-value vao bang key_value
*/
exports.getAllValue = async function (req, res) {
try {
const { pData, pMess, pCode } = await Device.getAllValue();
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller them key-value vao bang key_value
* @param {String} message Noi dung cua loi
* @param {String} model Ten model duoc ap dung (id_model | All)
*/
exports.addValue = async function (req, res) {
const { key, value } = req.body;
console.log(req.body);
try {
const { pMess, pCode } = await Device.addValue(key, value);
res.json({ pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller xoa key-value vao bang key_value
* @param {Integer} id ID cua loi
*/
exports.deleteValue = async function (req, res) {
const { id } = req.body;
try {
const { pMess, pCode } = await Device.deleteValue(id);
res.json({ pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller them key-value vao bang key_value
* @param {String} message Noi dung cua loi
* @param {String} model Ten model duoc ap dung (id_model | All)
*/
exports.addItem = async function (req, res) {
const { item_SN, item_PID, item_desrc, SN_parent_item } = req.body;
try {
const { pMess, pCode } = await Device.addItem(
item_SN,
item_PID,
item_desrc,
SN_parent_item,
Date.now()
);
res.json({ pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay tat ca cac cap key-value vao bang key_value
*/
exports.getAllItem = async function (req, res) {
try {
const { pData, pMess, pCode } = await Device.getAllItem();
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};
/**
* Controller lay tat ca cac cap key-value vao bang key_value
*/
exports.getAllItemBySNParent = async function (req, res) {
const { id } = req.body;
try {
const { pData, pMess, pCode } = await Device.getAllItemBySNParent(id);
res.json({ pData, pMess, pCode });
} catch (error) {
console.log(error);
}
};