import * as z from "zod" import { tool } from "langchain" import os from 'os' import { promisify } from 'util' import { exec } from 'child_process' const execPromise = promisify(exec) /** * 获取系统网卡和网络连接信息工具 * @returns {string} - 返回网络信息的 JSON 字符串,包括网卡信息和网络连接信息 */ export const getNetworkInfoTool = tool( async (input) => { try { const networkInfo = { platform: os.platform(), networkInterfaces: [], dnsServers: [], gateway: [] } // 获取网络接口信息(跨平台) const interfaces = os.networkInterfaces() for (const [name, addresses] of Object.entries(interfaces)) { networkInfo.networkInterfaces.push({ name, addresses: addresses.map(addr => ({ family: addr.family, address: addr.address, netmask: addr.netmask, mac: addr.mac, internal: addr.internal, cidr: addr.cidr })) }) } // 获取详细网络信息(Windows) if (os.platform() === 'win32') { try { const { stdout } = await execPromise('ipconfig /all') const lines = stdout.trim().split('\n') let currentAdapter = null lines.forEach(line => { const adapterMatch = line.match(/适配器\s+(.+?):/) if (adapterMatch) { if (currentAdapter) { networkInfo.networkInterfaces.push(currentAdapter) } currentAdapter = { name: adapterMatch[1], description: '', macAddress: '', dhcpEnabled: false, ipAddress: [], subnetMask: [], defaultGateway: [], dnsServers: [] } } else if (currentAdapter) { const descMatch = line.match(/描述\s+(.+)/) if (descMatch) currentAdapter.description = descMatch[1].trim() const macMatch = line.match(/物理地址\s+:\s+(.+)/) if (macMatch) currentAdapter.macAddress = macMatch[1].trim() const dhcpMatch = line.match(/DHCP 已启用\s+:\s+(.+)/) if (dhcpMatch) currentAdapter.dhcpEnabled = dhcpMatch[1].trim() === '是' const ipMatch = line.match(/IPv4 地址\s+:\s+(.+)/) if (ipMatch) currentAdapter.ipAddress.push(ipMatch[1].trim()) const subnetMatch = line.match(/子网掩码\s+:\s+(.+)/) if (subnetMatch) currentAdapter.subnetMask.push(subnetMatch[1].trim()) const gatewayMatch = line.match(/默认网关\s+:\s+(.+)/) if (gatewayMatch && gatewayMatch[1].trim()) currentAdapter.defaultGateway.push(gatewayMatch[1].trim()) const dnsMatch = line.match(/DNS 服务器\s+:\s+(.+)/) if (dnsMatch && dnsMatch[1].trim()) currentAdapter.dnsServers.push(dnsMatch[1].trim()) } }) if (currentAdapter) { networkInfo.networkInterfaces.push(currentAdapter) } } catch (error) { console.error('Error getting network config:', error) } // 获取DNS服务器(Windows) try { const { stdout } = await execPromise('nslookup localhost 2>&1') const dnsMatch = stdout.match(/Server:\s+(.+)/) if (dnsMatch) { networkInfo.dnsServers.push(dnsMatch[1].trim()) } } catch (error) { console.error('Error getting DNS servers:', error) } } // 获取详细网络信息(Linux) else if (os.platform() === 'linux') { try { const { stdout } = await execPromise('ip addr show') const lines = stdout.trim().split('\n') let currentInterface = null lines.forEach(line => { const interfaceMatch = line.match(/^\d+:\s+(\w+):/) if (interfaceMatch) { if (currentInterface) { networkInfo.networkInterfaces.push(currentInterface) } currentInterface = { name: interfaceMatch[1], state: '', macAddress: '', ipAddress: [], subnetMask: [] } } else if (currentInterface) { const stateMatch = line.match(/state\s+(\w+)/) if (stateMatch) currentInterface.state = stateMatch[1] const macMatch = line.match(/link\/ether\s+([a-fA-F0-9:]+)/) if (macMatch) currentInterface.macAddress = macMatch[1] const ipMatch = line.match(/inet\s+(\d+\.\d+\.\d+\.\d+)\/(\d+)/) if (ipMatch) { currentInterface.ipAddress.push(ipMatch[1]) const maskLength = parseInt(ipMatch[2]) currentInterface.subnetMask.push(maskLength) } } }) if (currentInterface) { networkInfo.networkInterfaces.push(currentInterface) } } catch (error) { console.error('Error getting network config:', error) } // 获取DNS服务器(Linux) try { const { stdout } = await execPromise('cat /etc/resolv.conf') const dnsMatches = stdout.match(/nameserver\s+(.+)/g) if (dnsMatches) { networkInfo.dnsServers = dnsMatches.map(match => match.replace('nameserver', '').trim()) } } catch (error) { console.error('Error getting DNS servers:', error) } // 获取网关(Linux) try { const { stdout } = await execPromise('ip route show default') const gatewayMatch = stdout.match(/default via (\d+\.\d+\.\d+\.\d+)/) if (gatewayMatch) { networkInfo.gateway.push(gatewayMatch[1]) } } catch (error) { console.error('Error getting gateway:', error) } } // 获取详细网络信息(macOS) else if (os.platform() === 'darwin') { try { const { stdout } = await execPromise('ifconfig') const lines = stdout.trim().split('\n') let currentInterface = null lines.forEach(line => { const interfaceMatch = line.match(/^(\w+):/) if (interfaceMatch) { if (currentInterface) { networkInfo.networkInterfaces.push(currentInterface) } currentInterface = { name: interfaceMatch[1], status: '', macAddress: '', ipAddress: [], subnetMask: [] } } else if (currentInterface) { const statusMatch = line.match(/status:\s+(\w+)/) if (statusMatch) currentInterface.status = statusMatch[1] const macMatch = line.match(/ether\s+([a-fA-F0-9:]+)/) if (macMatch) currentInterface.macAddress = macMatch[1] const ipMatch = line.match(/inet\s+(\d+\.\d+\.\d+\.\d+)\s+netmask\s+(0x[0-9a-fA-F]+)/) if (ipMatch) { currentInterface.ipAddress.push(ipMatch[1]) currentInterface.subnetMask.push(ipMatch[2]) } } }) if (currentInterface) { networkInfo.networkInterfaces.push(currentInterface) } } catch (error) { console.error('Error getting network config:', error) } // 获取DNS服务器(macOS) try { const { stdout } = await execPromise('scutil --dns | grep nameserver | awk \'{print $3}\'') networkInfo.dnsServers = stdout.trim().split('\n').filter(dns => dns) } catch (error) { console.error('Error getting DNS servers:', error) } // 获取网关(macOS) try { const { stdout } = await execPromise('netstat -nr | grep default') const gatewayMatch = stdout.match(/default\s+(\d+\.\d+\.\d+\.\d+)/) if (gatewayMatch) { networkInfo.gateway.push(gatewayMatch[1]) } } catch (error) { console.error('Error getting gateway:', error) } } return JSON.stringify(networkInfo, null, 2) } catch (error) { console.error('Error getting network info:', error) return JSON.stringify({ error: error.message }, null, 2) } }, { name: "get_network_info", description: `获取系统网卡和网络信息,包括网卡配置、IP地址、MAC地址、DNS服务器、网关等`, schema: z.object({}) } )