背景
有时候担心电脑被黑,或者账号密码丢失,导致别人偷摸登录个人设备。所以搞一个小监控程序。程序脚本用 nodejs 写
准备
安装 nvm ,安装最新版本的 nodejs
开发脚本
安装依赖
npm install axios
脚本开发
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const os = require('os');
// 日志配置
const logPath = path.join(__dirname, 'post_log.txt');
const log = (message) => {
const timestamp = new Date().toISOString();
fs.appendFileSync(logPath, `[${timestamp}] ${message}\n`);
};
const userInfo = os.userInfo();
const args = process.argv.slice(2);
let title = "";
if(args.length>0){
title = args[0];
}
const date = new Date();
const formatted = `${date.getFullYear()}-${String(date.getMonth()+1).padStart(2,'0')}-${String(date.getDate()).padStart(2,'0')} ${String(date.getHours()).padStart(2,'0')}:${String(date.getMinutes()).padStart(2,'0')}:${String(date.getSeconds()).padStart(2,'0')}`;
// 请求配置
const postData = {
userNames: "收件人",
title: "机器["+title+"]提醒",
msg: userInfo.username + " | " + getLocalIPs() + " | " + formatted
};
function getLocalIPs() {
const interfaces = os.networkInterfaces();
const ips = [];
for (const [dev, ifaceList] of Object.entries(interfaces)) {
for (const iface of ifaceList) {
// 过滤 IPv4 且非内网地址
if (iface.family === 'IPv4' && !iface.internal) {
ips.push({
interface: dev,
address: iface.address,
family: iface.family,
mac: iface.mac
});
}
}
}
if(ips.length>0){
return ips[0].address;
}
return "";
}
// 发送请求
const sendRequest = async () => {
try {
const response = await axios.post(
'https://testhost/sendMsg',
postData,
{
headers: { 'Content-Type': 'application/json' },
httpsAgent: new (require('https').Agent)({
rejectUnauthorized: false // 临时忽略证书验证(测试用)
})
}
);
log(`请求成功,状态码:${response.status}`);
} catch (error) {
log(`请求失败:${error.message}`);
}
};
// 执行请求
sendRequest();
配置 windows 任务
win + R 运行 taskschd.msc 打开任务配置,创建基本任务


其他
这里的程序是用 nodejs 写的,纯粹就是想试试 nodejs。如果大家只是需要这么一个功能,那么完全可以用 windows bat 实现,bat 的代码也贴给大家参考
@echo off
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set datetime=%%I
set year=%datetime:~0,4%
set month=%datetime:~4,2%
set day=%datetime:~6,2%
set hour=%datetime:~8,2%
set minute=%datetime:~10,2%
set second=%datetime:~12,2%
set formatted_time=%year%-%month%-%day% %hour%:%minute%:%second%
echo cur_time:%formatted_time%
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| findstr /i "IPv4"') do (
set IP=%%a
goto :break
)
:break
echo localIP:%IP%
curl -X POST "https://testhost/sendMsg" ^
-H "Content-Type: application/json" ^
-d "{\"userNames\": \"收件人\",\"title\": \"PC notice\",\"msg\": \"%USERNAME% login [%IP%] at %formatted_time%\"}"
1598

被折叠的 条评论
为什么被折叠?



