node.js获取本机IP地址

想在node中获取本机ip,需要先判断操作系统,可以通过process.platform来判断,MAC下得到的值是darwin,window下得到的值是win32;

然后再利用os模块的networkInterfaces()可以获取所有网卡信息;从其中就可以找到需要的ip;下面是个小例子

function hostIp() {
    var IPv4;
    if(process.platform === ‘darwin‘) {
        for(var i = 0; i < os.networkInterfaces().en0.length; i++) {
            if(os.networkInterfaces().en0[i].family == ‘IPv4‘) {
                IPv4 = os.networkInterfaces().en0[i].address;
            }
        }
    } else if(process.platform === ‘win32‘) {
        for(var i = 0; i < os.networkInterfaces()[‘本地连接‘].length; i++) {
            if(os.networkInterfaces()[‘本地连接‘][i].family == ‘IPv4‘) {
                IPv4 = os.networkInterfaces()[‘本地连接‘][i].address;

            }
        }
    }
    return IPv4;
}

当然也可以通过os.type来判断操作系统。MAC下返回的仍然是darwin;而windows 下返回的是window_NT

时间: 2024-10-23 22:42:06

node.js获取本机IP地址的相关文章

js获取本机ip地址

/** * 获取本地IP地址 */ function getLocalIPAddress() { var obj = null; var rslt = "127.0.0.1"; try { obj = new ActiveXObject("rcbdyctl.Setting"); if (!isNull(obj.GetIPAddress)) { rslt = obj.GetIPAddress; } obj = null; } catch(e) { //异常发生 } r

node.js获取本机Ip, hostName, mac

//获取ip地址 getIPAdress() { let interfaces = require('os').networkInterfaces(); for (var devName in interfaces) { var iface = interfaces[devName]; for (var i = 0; i < iface.length; i++) { let alias = iface[i]; if (alias.family === 'IPv4' && alias.

js获取本机mac地址,IP地址,计算机名

<!DOCTYPE HTML> <html> <head> <title>js获取本机mac地址,IP地址,计算机名</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta content="MSHTML 6.00.2800.1106" name="

获取本机IP地址

这里有两种方法: 1 //获取本机IP 2 - (NSString *)localIPAddress 3 { 4 NSString *localIP = nil; 5 struct ifaddrs *addrs; 6 if (getifaddrs(&addrs)==0) { 7 const struct ifaddrs *cursor = addrs; 8 while (cursor != NULL) { 9 if (cursor->ifa_addr->sa_family == AF_

关于是用dotnet获取本机IP地址+计算机名的方法

印象中在maxscript帮助文档里找到过方法,但是当时没记下来.只能通过dotnet实现了. 如果电脑有无线网卡和本地连接,可能会出现乱码,也问了写dotnet的朋友,提供了一些思路,不过最终还是使用了这个笨办法. fn getIP_PCname = ( cc = (dotnetclass "System.Net.Dns") oo = cc.GetHostAddresses(cc.GetHostName()) for ip = 1 to oo.count do ( getip = f

java获取本机IP地址

/** * WIFI没打开:ip为127.0.0.1 * 获取本机IP地址字符串 * @return */ public String getWifiIp() { if (!getWifiEnabled()) { return "127.0.0.1"; } WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); String ip = intToIp(i

Linux编程获取本机IP地址

使用函数getifaddrs来枚举网卡IP,其中使用到的结构体如下所示: struct ifaddrs { struct ifaddrs *ifa_next; /* Next item in list */ char *ifa_name; /* Name of interface */ unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */ struct sockaddr *ifa_addr; /* Address of interface *

#获取本机IP地址时排除IPv6类型,只返回IPv4地址的方法

public static string GetLocalIP(){try{string HostName = Dns.GetHostName(); //得到主机名IPHostEntry IpEntry = Dns.GetHostEntry(HostName); for (int i=0; i < IpEntry.AddressList.Length; i++){//从IP地址列表中筛选出IPv4类型的IP地址//AddressFamily.InterNetwork表示此IP为IPv4,//Ad

Python 获取本机IP地址

import socket #获取本机IP地址 self.local_ip = socket.gethostbyname(socket.gethostname()) print (self.local_ip) python 判断本机是否联网 1 timeout = 200 2 host = 'www.baidu.com' 3 port = 80 4 s=socket.socket() 5 s.settimeout(timeout) 6 status = s.connect_ex((host,po