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系统撰写的。