linux獲取ip地址c語言
㈠ linux下如何通過編程得到DNS ip地址和默認網關ip地址。(C 語言)
方案一:
你用c語言去讀取/etc/resolv.conf,格式是namserver *.*.*.* 提取以下 就有DNS了
至於網關 你可以去讀取/etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0等文件,但是各個Linux系統可能少有差別。
方案二:
在C程序中調用exec函數,執行route命令,從返回的結果中提取網關
至於DNS 也可以通過dig localhost, 從結果中提取DNS 但是就沒有方案一中快了
㈡ 獲取Linux IP地址的六種方法總結
本文總結六種查看Linux IP地址的方法,方便以後的運維開發工作。
在介紹前先學習一下三個命令行篩選的主要的指令,也是頻繁使用到的命令。
1、head。 head 命令可用於查看文件的開頭部分的內容,有一個常用的參數 -n 用於顯示行數,默認為 10。
運行head --help查看說明信息:
-q 隱藏文件名
-v 顯示文件名
-c<數目> 顯示的位元組數。
-n<行數> 顯示的行數。
2、grep。 grep 命令用於查找文件里符合條件的字元串。運行grep --help查看說明信息,參數太多主要有以下幾種:
grep -r遞歸選擇。
grep -v反選,顯示不包含匹配文本的所有行。
grep -n顯示符合樣式的那一行之前。
grep -A顯示符合範本樣式的那一列之外,並顯示該行之後的內容。
3、awk。 強大的文本分析工具,命令使用過於復雜(awk --help),只需要知道 awk '{print$2}'為列印第二行數據。
4、tail 。tail命令可用於查看文件的結束部分的內容,有一個常用的參數 -n 用於顯示行數,默認為 10。tail --help查看主要的參數:
tail -n顯示最後多少行
tail -c顯示最後十個字元
tail -f 循環讀取,跟蹤顯示最後十行
5、cut。 顯示每行從開頭算起的文字。
cut -b :以位元組為單位進行分割。
cut -c :以字元為單位進行分割
cut -d :自定義分隔符,默認為製表符
cut -f :與-d一起使用,指定顯示哪個區域
無線網卡地址:
echo wlan0=`ifconfig wlan0 | head -n2 | grep inet | awk '{print$2}'`
有線網卡地址:
echo eth0=`ifconfig eth0 | head -n2 | grep inet | awk '{print$2}'`
或者命令:
ifconfig | grep "inet " | cut -d: -f2 | awk '{print $1}' | grep -v "^127."
無線網卡地址:
ip address | grep wlan0 | awk '{print$2}'
有線網卡地址:
ip address | grep eth0 | awk '{print$2}'
或者
echo eth0=`ip address show eth0 | head -n4 | grep inet | awk '{print$2}'
echo wlan0=`ip address show wlan0 | head -n4 | grep inet | awk '{print$2}'
運行hostname -help命令查看說明信息:
Program options:
-a, --alias alias names
-A, --all-fqdns all long host names (FQDNs)
-b, --boot set default hostname if none available
-d, --domain DNS domain name
-f, --fqdn, --long long host name (FQDN)
-F, --file read host name or NIS domain name from given file
-i, --ip-address addresses for the host name
-I, --all-ip-addresses all addresses for the host
-s, --short short host name
-y, --yp, --nis NIS/YP domain name
hostname -i得到環回地址127.0.1.1, hostname -I得到具體的網卡信息192.168.31.82 。
php語言查看ip就是使用函數shell_exec來執行shell命令。
比如:
<?php
echo shell_exec("echo wlan0=`ifconfig wlan0 | head -n2 | grep inet | awk '{print$2}'`");
?>
然後執行php ip.php 。shell_exec()裡面可以放置任何shell命令。這個方法的意義在於php可以通過網頁對外提供服務。
#!/usr/bin/env python
import socket
import fcntl
import struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
Local_wlan0=get_ip_address("wlan0")
Local_lo=get_ip_address("lo")
#Local_eth0=get_ip_address("eth0")
print Local_wlan0
print Local_lo
#print Local_eth0
利用socket包,然後執行python ip.py 得到wlan0信息。
#!/usr/bin/env python
import os
def get_ip():
out = os.popen("echo wlan0=`ifconfig wlan0 | head -n2 | grep inet | awk '{print$2}'`").read()
print out
if __name__ == '__main__':
get_ip()
和php的shell_exec函數類似,os.popen()裡面可以放置任何shell命令。注意有個函數os.system的結果只是命令執行結果的返回值,執行成功為0;os.popen()可以讀出執行的內容,輸出的結果比較特殊,帶換行符\n 。
㈢ linux如何查看ip地址命令(linux如何查看ip地址)
1、linux如何查看ip地址命令。
2、linux如何查看ip地址和埠。
3、linux如何查看ip地址,網關和掩碼。
4、linux如何查看ip地址 python。
1."linux如何查看ip地址,為您提供linux如何查看ip地址圖文信息,打開linux系統,在linux的桌面的空白處右擊。
2.在彈出的下拉選項里,點擊打開終端。
3.在打開的Linux伺服器終端中用查詢ip命令查找。
4.linux查看ip地址的命令為ifconfig-a,在命令窗口輸入然後按回車。
5.按下回車後就會在終端命令窗口裡顯示出ip地址。
㈣ linux c怎麼獲得IPv6地址
編輯文件/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="dhcp"
#BOOTPROTO="static"
HWADDR="2C:27:D7:31:2C:4C"
#IPADDR="192.168.0.2"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
保證IPV6選項為YES ,所處環境中有IPV6網路的話就可以自動獲行培取檔穗唯了.自己也可以用linux搭建一個.
然後用ifconfig eth0 查看網族中卡信息參數
㈤ linux 系統獲取當前登陸ip地址的幾種方法
1
[root@localhost test]# echo $SSH_CLIENT |awk ' { print $1 }'
111.161.8.26
2
[root@localhost test]# who am i |awk ' {print $5 }'
(111.161.8.26)
3 以下是2種不太完整的方法
[root@localhost test]# w | awk '{ print $3 }'
8
FROM
111.161.8.26
[root@localhost test]# finger | awk '{ print $7 }'
Office
(111.161.8.26)
㈥ linux 查看ip地址的命令
linux如何查看ip地址,可以使用ifconfig命令來快速查看ip地址。
1、首先在電腦中打開linux客戶端,點擊連接linux按鈕。
㈦ 請問linux下有c語言函數或者代碼可以獲取與之相連設備的ip嗎
有的fd為你連接設備的網路套接字。
你直接輸入套接字就可以得到了連接設備ip和埠了。
struct sockaddr_in sa;
int len1 = sizeof(sa);
if(getpeername(fd, (struct sockaddr *)&sa, &len1))
{
fprintf(stderr,"get client ip and port failed,exit!\n");
}
printf("ip=%s|埠=%s\n",inet_ntoa(sa.sin_addr),ntohs(sa.sin_port));ip|port
㈧ 請問在linux如何用C語言取得網關的IP。libnet或pcap的函數有嗎
問題1:
在linux系統裡面獲得網關地址沒有專門的函數,可以通過查配置文件/proc/net/route來獲得,沒有更好的方法了,因為linux提供的源碼包裡面都是這么做的,因此你需要編寫一洞耐游段解析配置文件納銷/proc/畝慧net/route的代碼來完成.
問題2:
我覺得用libnet得網關MAC應該不是個小例子,我也沒有研究過,不好下結論,樓下繼續......
㈨ 獲取ip地址對應的mac地址,c/c++編程實現,linux下
/**
*@send_arp.c
*@questwithLinux*PF_PACKETinterface
*@Author:xuelei
**/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<netdb.h>
#include<sys/socket.h>
#include<sys/un.h>
#include<sys/ioctl.h>
#include<netinet/in.h>
#include<net/if.h>
#include<sys/types.h>
#include<asm/types.h>
#include<features.h>/*需要裡面的glibc版本號*/
#if__GLIBC__>=2&&__GLIBC_MINOR>=1
#include<netpacket/packet.h>
#include<net/ethernet.h>/*鏈路層(L2)協議*/
#else
#include<asm/types.h>
#include<linux/if_packet.h>
#include<linux/if_ether.h>/*鏈路層協議*/
#endif
#include<netinet/if_ether.h>
#defineINLEN4
#defineMAC_BCAST_ADDR(uint8_t*)"xffxffxffxffxffxff"
voisage_quit(char*arg0);
intget_ifi(char*dev,char*mac,intmacln,structin_addr*lc_addr,intipln);
voidprmac(u_char*ptr);
intmain(intargc,char**argv)
{
if(argc!=2)
usage_quit(argv[0]);
intfd,salen,n;
u_char*mac;
charrecv_buf[120],rep_addr[16];
structin_addrlc_addr,req_addr;
structsockaddr_llreqsa,repsa;
structarp_pkt{
structether_headereh;
structether_arpea;
u_charpadding[18];
}req;
bzero(&reqsa,sizeof(reqsa));
reqsa.sll_family=PF_PACKET;
reqsa.sll_ifindex=if_nametoindex("eth0");
if((fd=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ARP)))<0){
perror("Socketerror");
exit(1);
}
mac=(char*)malloc(ETH_ALEN);
bzero(&req,sizeof(req));
if(get_ifi("eth0",mac,ETH_ALEN,&lc_addr,INLEN)){
fprintf(stderr,"Error:Gethost』sinformationfailed ");
exit(0);
}
printf("HostMACis:%02x:%02x:%02x:%02x:%02x:%02x ",*mac,*(mac+1),*(mac+2),*(mac+3),*(mac+4),*(mac+5));
printf("HostIPis:%s ",inet_ntop(AF_INET,&lc_addr,rep_addr,1024));
/*填寫乙太網頭部*/
memcpy(req.eh.ether_dhost,MAC_BCAST_ADDR,ETH_ALEN);
memcpy(req.eh.ether_shost,mac,ETH_ALEN);
req.eh.ether_type=htons(ETHERTYPE_ARP);
/*填寫arp數據*/
req.ea.arp_hrd=htons(ARPHRD_ETHER);
req.ea.arp_pro=htons(ETHERTYPE_IP);
req.ea.arp_hln=ETH_ALEN;
req.ea.arp_pln=INLEN;
req.ea.arp_op=htons(ARPOP_REQUEST);
memcpy(req.ea.arp_sha,mac,ETH_ALEN);
memcpy(req.ea.arp_spa,&lc_addr,INLEN);
inet_aton(argv[1],req.ea.arp_tpa);
bzero(recv_buf,sizeof(recv_buf));
bzero(&repsa,sizeof(repsa));
salen=sizeof(structsockaddr_ll);
if((n=sendto(fd,&req,sizeof(req),0,(structsockaddr*)&reqsa,sizeof(reqsa)))<=0){
perror("Sendtoerror");
exit(1);
}
printf("Broadcastarprequestof%s,%dbytesbesent ",argv[1],n);
while(1){
if((n=recvfrom(fd,recv_buf,sizeof(req),0,(structsockaddr*)&repsa,&salen))<=0){
perror("Recvfromerror");
exit(1);
}
if(ntohs(*(__be16*)(recv_buf+20))==2&&!memcmp(req.ea.arp_tpa,recv_buf+28,4)){
printf("Responsefrom%s,%dbytesreceived ",argv[1],n);
printf("PeerIPis:%s ",inet_ntop(AF_INET,(structin_addr*)(recv_buf+28),rep_addr,1024));
prmac((u_char*)(recv_buf+22));//prmac((u_char*)(recv_buf+6));
break;
}
else
{
printf("Haverecivedata ");
break;
}
}
free(mac);
}
intget_ifi(char*dev,char*mac,intmacln,structin_addr*lc_addr,intipln)
{
intreqfd,n;
structifreqmacreq;
reqfd=socket(AF_INET,SOCK_DGRAM,0);
strcpy(macreq.ifr_name,dev);
/*獲取本地介面MAC地址*/
if(ioctl(reqfd,SIOCGIFHWADDR,¯eq)!=0)
return1;
memcpy(mac,macreq.ifr_hwaddr.sa_data,macln);
/*獲取本地介面IP地址*/
if(ioctl(reqfd,SIOCGIFADDR,¯eq)!=0)
return1;
memcpy(lc_addr,&((structsockaddr_in*)(¯eq.ifr_addr))->sin_addr,ipln);
return0;
}
voidprmac(u_char*ptr)
{
printf("PeerMACis:%02x:%02x:%02x:%02x:%02x:%02x ",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));
}
voisage_quit(char*arg0)
{
fprintf(stderr,"Usage:%s<query_IP> ",arg0);
exit(1);
}
㈩ linux怎麼查看ip地址
linux怎麼查看ip地址塵兄談?我們一起來了解一下吧。
1、打開塵知linux伺服器,然後在桌面的空白處點擊右鍵。
2、在彈出的下拉選項里,點擊打開終端。
3、輸入查詢linux的ip地址的命令:ifconfig-a,然後回車。
4、使用查看linux的ip地址命令後,就會在終派碰端命令窗口裡顯示出ip地址。
本文章基於ThinkpadE15品牌、centos7系統撰寫的。