1. 程式人生 > >寫一個函式獲取某一張指定的網絡卡的IP地址

寫一個函式獲取某一張指定的網絡卡的IP地址

/*********************************************************************************
 *      Copyright:  (C) 2018 NULL
 *                  All rights reserved.
 *
 *       Filename:  get.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(01/24/2018)
 *         Author:  zhaowenbin <
[email protected]
> * ChangeLog: 1, Release initial version on "01/24/2018 06:17:24 PM" * ********************************************************************************/ #include <stdio.h> #include <unistd.h> #include <string.h> int get_nic_ipaddr(char *nic,char *ip,char *netmask); int main(int argv,char **argc) { char net[30]; char ip[32]; char netmask[32]; memset(net,0,sizeof(net)); printf("please enter netcommand:\n"); gets(net); get_nic_ipaddr(net,ip,netmask); return 0; } int get_nic_ipaddr(char *nic,char *ip,char *netmask) { FILE *fp; char buf[512]; char *stream1,*stream2; int num=0; printf("commander:%s\n",nic); fp=popen(nic,"r"); printf("commander:%s\n",nic); //stream1=strstr(fp,"addr:"); //stream2=strstr(fp,"Bcast"); //printf("%p\n",stream1); while(fgets(buf,sizeof(buf),fp)) { if(strstr(buf,"inet addr:")) { stream1=strstr(buf,"inet addr:"); //printf("%p\n",stream1); } if(strstr(buf,"Bcast:")) { stream2=strstr(buf,"Bcast"); //printf("%p\n",stream2); snprintf(ip,stream2-stream1,stream1); printf("%s\n",ip); stream1=0; stream2=0; } if(stream1==0) { if(strstr(buf,"Mask:")) { stream1=strstr(buf,"Mask:"); //printf("%p\n",stream1); printf("%s",stream1); } } if(stream2==0) { if(strstr(buf,"inet6 addr:")) { stream2=strstr(buf,"inet6 addr:"); //printf("%p\n",stream2); //snprintf(netmask,stream2-stream1,stream1); //printf("%s",netmask); stream1=0; stream2=0; } } } close(fp); }
測試結果如下:
[[email protected] 2018125]$ ./a.out 
please enter netcommand:
ifconfig eth1
commander:ifconfig eth1
commander:ifconfig eth1
eth1: error fetching interface information: Device not found
[[email protected] 2018125]$ ./a.out 
please enter netcommand:
ifconfig eth0
commander:ifconfig eth0
commander:ifconfig eth0
inet addr:192.168.138.128 
Mask:255.255.255.0


相關推薦

一個函式獲取指定IP地址

/********************************************************************************* * Copyright: (C) 2018 NULL *

利用popen一個函式獲取指定IP地址

ifconfig eth1是在我電腦上的普通網絡卡裝置,今天我們來對這幾行數字進行操作,從而讀取它的IP地址和子網掩碼Netmask[[email protected] file]$ ifco

C#之獲取IP地址

有時候不想讀取配置檔案來進行網路監聽,預設把本級所有IP地址監聽一遍,這個時候就需要獲取本級所有IP地址。 如下: string name = Dns.GetHostName(); IPAddress[] ipadrlist = Dns.Get

linux獲取活動ip地址

1、根據ioctl機制列印當前所有網絡卡 程式碼: #include <sys/ioctl.h> #include <net/if.h> #include <netinet/in.h> #include <arpa/inet.h> #include <

python獲取IP地址

#!/usr/bin/env python # -*- coding: utf-8 -*- import socket import fcntl import struct def get_ip_address(ifname): s = sock

shell 獲取IP地址掩碼 預設閘道器 廣播地址 MAC地址

# IP地址 ifconfig eth0 | grep "inet addr:" | awk -F " " '{print $2}' | awk -F ":" '{print $2}' # 廣播地址 ifconfig eth0 | grep "inet addr:" |

delphi 獲取IP地址列表和Mac地址

1、宣告windows系統的sendarp函式 function sendarp(ipaddr: ulong; temp: dword; ulmacaddr: pointer; ulmacaddrleng: pointer): Dword; StdCall;External

java獲取機器名及所有IP

獲取機器名: public String getLocalHostName() { String hostName; try { In

LINUX下獲取IP地址和MAC地址,子掩碼程式參考

/* mode time:20120727 LINUX下獲取IP地址和MAC地址.程式相關結構體在程式後面。 列印網絡卡的ip地址 子網掩碼 廣播地址 mac地址 環境: [[email protected] temp]# uname -a Linux b

Telnet HTTP獲取網頁HTML的寫法

回車 我們 一個 雙引號) htm .html com 步驟 寫法 1、cmd -> telnet www.webkaka.com 802、按組合鍵:ctrl + ] ,然後回車3、輸入"GET / HTTP/1.1"(無雙引號),註意第一個“/”前後有空格,然後回車

一個函式,輸入個數,隨機生成N條郵箱

寫一個函式,這個函式的功能是,傳入一個數字,產生N條郵箱,產生的手機號不能重複。郵箱前面的長度是6 - 12之間,產生的郵箱必須包含大寫字母、小寫字母、數字和特殊字元 import random

一個函式可以判斷個數是不是素數

程式程式碼: #include <stdio.h> #include <windows.h> #pragma warning (disable:4996) int prime(

主機上有多個用java指定獲取一個ip的問題

前幾天寫過一個基於tomcat發郵件修改密碼的功能,大致是這樣: 點選修改密碼按鈕就傳送一封郵件去指定賬戶的郵箱,郵箱裡有個超連結和驗證碼,超連結開啟進入修改密碼頁面。之前能用,但是今天我的筆記本打開了獵豹免費wifi,造成有兩個網絡卡都被啟用,如下圖:

獲取指定對應的IP地址

1 #include <stdio.h> 2 #include <string.h> 3 #include <sys/socket.h> 4 #include <sys/types.h> 5 #include <sys/ioctl.h>

你和物聯網之間就差著物聯網

近這些年來,隨著科學技術的不斷髮展,物聯網技術應用也變得越來越廣泛,社會發展也走向了智慧化和資訊化的道路,許多行業或領域都實現了智慧化,包括農業生產、車聯網、智慧穿戴等等,物聯網技術應用也因此成為了十分適用智慧化和資訊化發展的技術支援。 然而近日百度就依託物聯網絡卡開發出了屬於自己的一

linux 獲取eth0(指定IP地址(或MAC地址)

引言 使用場景: 我使用的Linux伺服器上有多個網絡卡,有時候就需要在指令碼中使用到指定網絡卡的IP地址或MAC地址,那麼這個時候就有兩種做法,一是預先讀取網絡卡的IP地址或MAC地址,然後寫入到指令碼中;另外一種方法就是通過shell命令實時獲取指定網絡卡上的當前

PDH獲取指定速度

#include <windows.h> #include <stdio.h> #include <conio.h> #include <pdh.h> #include <pdhmsg.h> #pragm

存在情況下獲取指定的MAC地址

應用軟體中經常有需要顯示mac地址的功能,方便使用者判斷當前聯網狀態,如果是在多網絡卡存在(無線,有線等)下,網路會經常切換,獲取到的MAC地址可能與預期不同,現給出指定網絡卡獲取mac地址的方法。 void get_mac(Mac_Address &am

linux c/c++按規則獲取ip

linux c/c++按規則獲取網絡卡ip 輸出專案到雲或者輸出給外部客戶,會遇到伺服器多網絡卡多ip的情形,如果有多個應用都需要這個主機ip,而且多應用需要獲取相同的ip,此時可以約定一種規則來獲取相同的ip,比如: 獲得所有網絡卡名,然後對網絡卡名按從小到大排序,查詢最小

在安卓6.0(及以上)裝置上無法獲取無線MAC地址的解決方案

在安卓6.0以下的裝置上,通過WifiManager.getConnectionInfo().getMacAddress()即可獲取WLAN實體地址, 而在6.0及以上,以此方式獲取到的MAC地址為固定值02:00:00:00:00:00,而非真實值 解決方案 WlanMacAddressGetter.