4 * Link an agent to a MeshCentral device by updating meshcentral_nodeid
5 * Usage: node link-agent-to-meshcentral.js <agent_uuid> <meshcentral_nodeid>
8const { Pool } = require('pg');
9require('dotenv').config();
11const pool = new Pool({
12 host: process.env.POSTGRES_HOST,
13 port: process.env.POSTGRES_PORT || 25060,
14 database: process.env.POSTGRES_DATABASE || 'defaultdb',
15 user: process.env.POSTGRES_USER,
16 password: process.env.POSTGRES_PASSWORD,
17 ssl: process.env.POSTGRES_SSL === 'true' ? { rejectUnauthorized: false } : false
23 * @param meshcentralNodeId
25async function linkAgent(agentUuid, meshcentralNodeId) {
27 // Check if agent exists
28 const agentCheck = await pool.query(
29 'SELECT agent_id, agent_uuid, hostname, meshcentral_nodeid FROM agents WHERE agent_uuid = $1',
33 if (agentCheck.rows.length === 0) {
34 console.error(`ā Agent not found: ${agentUuid}`);
38 const agent = agentCheck.rows[0];
39 console.log(`\nš Current Agent State:`);
40 console.log(` Agent ID: ${agent.agent_id}`);
41 console.log(` UUID: ${agent.agent_uuid}`);
42 console.log(` Hostname: ${agent.hostname}`);
43 console.log(` Current Node ID: ${agent.meshcentral_nodeid || '(none)'}`);
46 const result = await pool.query(
48 SET meshcentral_nodeid = $1,
49 meshcentral_connected = true,
50 meshcentral_last_seen = NOW()
52 RETURNING agent_id, hostname, meshcentral_nodeid`,
53 [meshcentralNodeId, agentUuid]
56 console.log(`\nā
Agent Updated:`);
57 console.log(` New Node ID: ${result.rows[0].meshcentral_nodeid}`);
58 console.log(`\nš Link successful! Agent ${agentUuid} is now connected to MeshCentral device.`);
59 console.log(`\nš View agent at: https://everydaytech.au/agents/${agentUuid}`);
62 console.error('ā Error linking agent:', error.message);
63 console.error('Full error:', error);
70// Parse command line arguments
71const args = process.argv.slice(2);
73if (args.length !== 2) {
74 console.error('Usage: node link-agent-to-meshcentral.js <agent_uuid> <meshcentral_nodeid>');
75 console.error('\nExample:');
76 console.error(' node link-agent-to-meshcentral.js 2c3e2d7a-c521-4844-b44b-a30b4c2ec4d8 "yfNgVgiQyrbYaDLpK1El3YM81NyK3nwX9ggSYdjc1LdFPB9rL$zq2EYaAaOzi0xW"');
80const [agentUuid, meshcentralNodeId] = args;
82linkAgent(agentUuid, meshcentralNodeId);