EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
scrape-meshcentral-devices.js
Go to the documentation of this file.
1const https = require('https');
2const { URL } = require('url');
3
4require('dotenv').config();
5
6const MESHCENTRAL_URL = process.env.MESHCENTRAL_URL;
7const USERNAME = process.env.MESHCENTRAL_USERNAME;
8const PASSWORD = process.env.MESHCENTRAL_PASSWORD;
9
10/**
11 *
12 */
13async function login() {
14 const loginUrl = new URL('/login', MESHCENTRAL_URL);
15
16 const postData = `action=login&username=${encodeURIComponent(USERNAME)}&password=${encodeURIComponent(PASSWORD)}&token=`;
17
18 const options = {
19 method: 'POST',
20 headers: {
21 'Content-Type': 'application/x-www-form-urlencoded',
22 'Content-Length': Buffer.byteLength(postData)
23 }
24 };
25
26 return new Promise((resolve, reject) => {
27 const req = https.request(loginUrl, options, (res) => {
28 let cookie = null;
29
30 if (res.headers['set-cookie']) {
31 const cookies = res.headers['set-cookie'];
32 for (const c of cookies) {
33 if (c.startsWith('xid=')) {
34 cookie = c.split(';')[0];
35 break;
36 }
37 }
38 }
39
40 let data = '';
41 res.on('data', chunk => data += chunk);
42 res.on('end', () => resolve(cookie));
43 });
44
45 req.on('error', reject);
46 req.write(postData);
47 req.end();
48 });
49}
50
51/**
52 *
53 * @param path
54 * @param cookie
55 */
56async function getPage(path, cookie) {
57 const url = new URL(path, MESHCENTRAL_URL);
58
59 const options = {
60 headers: {
61 'Cookie': cookie,
62 'User-Agent': 'Mozilla/5.0'
63 }
64 };
65
66 return new Promise((resolve, reject) => {
67 https.get(url, options, (res) => {
68 let data = '';
69 res.on('data', chunk => data += chunk);
70 res.on('end', () => resolve(data));
71 }).on('error', reject);
72 });
73}
74
75/**
76 *
77 */
78async function main() {
79 try {
80 console.log('šŸ”§ Logging in to MeshCentral...\n');
81
82 const cookie = await login();
83 if (!cookie) {
84 throw new Error('Login failed - no cookie received');
85 }
86
87 console.log('āœ… Login successful\n');
88
89 // Try to get the main page
90 console.log('šŸ“„ Fetching main page...\n');
91 const html = await getPage('/', cookie);
92
93 // Look for device references in the HTML
94 const deviceMatches = html.match(/node\/[a-zA-Z0-9\/_-]+/g) || [];
95 const uniqueDevices = [...new Set(deviceMatches)];
96
97 console.log(`āœ… Found ${uniqueDevices.length} device reference(s):\n`);
98 uniqueDevices.forEach(device => {
99 console.log(` - ${device}`);
100 });
101
102 // Also look for mesh references
103 const meshMatches = html.match(/mesh\/[a-zA-Z0-9\/_-]+/g) || [];
104 const uniqueMeshes = [...new Set(meshMatches)];
105
106 console.log(`\nāœ… Found ${uniqueMeshes.length} mesh reference(s):\n`);
107 uniqueMeshes.forEach(mesh => {
108 console.log(` - ${mesh}`);
109 });
110
111 // Look for any IPs
112 const ipMatches = html.match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g) || [];
113 const uniqueIps = [...new Set(ipMatches)];
114
115 if (uniqueIps.length > 0) {
116 console.log(`\nšŸ“ Found ${uniqueIps.length} IP address(es):\n`);
117 uniqueIps.forEach(ip => {
118 console.log(` - ${ip}`);
119 });
120 }
121
122 } catch (error) {
123 console.error('āŒ Error:', error.message);
124 process.exit(1);
125 }
126}
127
128main();