Kali Linux常用服务配置教程获取IP地址

Kali Linux常用服务配置教程获取IP地址,第1张

下面以Kali Linux为例,演示获取IP地址的方法 (1)设置网络接口为自动获取IP地址。在Kali Linux的收藏夹中单击图标,将显示所有的程序,如图1.8所示。(2)单击“设置”图标,将打开“设置”窗口,如图1.9所示。(3)选择“网络”选项,单击有线连接中的齿轮按钮,将显示“有线”对话框,如图1.10所示。(4)勾选“自动连接”复选框。然后,单击IPv4标签,将显示IPv4选项卡,如图1.11所示。(5)在该界面选择“自动(DHCP)”选项。然后,单击“应用”按钮。接下来,就可以请求获取IP地址了。执行命令如下所示: root@daxueba:~# dhclient eth0 –d Internet Systems Consortium DHCP Client 4.3.5 Copyright 2004-2016 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/eth0/00:0c:29:25:89:95 Sending on LPF/eth0/00:0c:29:25:89:95 Sending on Socket/fallback DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 4 DHCPREQUEST of 192.168.0.12 on eth0 to 255.255.255.255 port 67 DHCPOFFER of 192.168.0.12 from 192.168.0.10 DHCPACK of 192.168.0.12 from 192.168.0.100 bound to 192.168.0.12 -- renewal in 277 seconds. 从输出的信息中,可以看到成功获取到IP地址192.168.0.12。由此可以说明,搭建的DHCP服务测试成功。此时,用户查看地址租约文件dhcpd.leases,也可以看到分配的IP地址。如下所示: root@daxueba:~# cat /var/lib/dhcp/dhcpd.leases # The format of this file is documented in the dhcpd.leases(5) manual page. # This lease file was written by isc-dhcp-4.3.5 # authoring-byte-order entry is generated, DO NOT DELETE authoring-byte-order little-endian lease 192.168.0.10 {  starts 1 2018/07/02 10:15:00  ends 1 2018/07/02 10:25:00  cltt 1 2018/07/02 10:15:00  binding state active  next binding state free  rewind binding state free  hardware ethernet 00:0c:29:5c:ae:aa  client-hostname "daxueba" } lease 192.168.0.11 {  starts 1 2018/07/02 10:18:17  ends 1 2018/07/02 10:28:17  cltt 1 2018/07/02 10:18:17  binding state active  next binding state free  rewind binding state free  hardware ethernet 00:0c:29:99:92:4f  uid "\001\000\014)\231\222O"  set vendor-class-identifier = "MSFT 5.0"  client-hostname "Test" } lease 192.168.0.12 {  starts 1 2018/07/02 10:34:56  ends 1 2018/07/02 10:44:56  cltt 1 2018/07/02 10:34:56  binding state active  next binding state free  rewind binding state free  hardware ethernet 00:0c:29:25:89:95  client-hostname "daxueba" } 从输出的信息中,可以看到DHCP服务分配出去的IP地址及对应租约信息。

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <net/if.h>

#include <netdb.h>

#include <arpa/inet.h>

#include <sys/ioctl.h>

//获取地址

//返回IP地址字符串

int getlocalip(char* outip)

{

int i=0

int sockfd

struct ifconf ifconf

char buf = (char)malloc(512)

struct ifreq *ifreq

char* ip

//初始化ifconf

ifconf.ifc_len = 512

ifconf.ifc_buf = buf

if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0)

{

return -1

}

ioctl(sockfd, SIOCGIFCONF, &ifconf) //获取所有接口信息

close(sockfd)

//接下来一个一个的获取IP地址

ifreq = (struct ifreq*)buf

i = ifconf.ifc_len/sizeof(struct ifreq)

char *pos = outip

int count

for(count = 0 (count < 5 && i > 0) i--)

{

ip = inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr)

if(strncmp(ip,"127.0.0.1", 3)==0) //排除127.x.x.x,继续下一个

{

ifreq++

continue

}else

{

printf("%s\n", ip)

strcpy(pos,ip)

int len = strlen(ip)

pos = '\t'

pos += len+1

count ++

ifreq++

}

}

free(buf)

return 0

}

//——————————-函数的调用方式————————————-

int main(int argc, char** argv)

{

char ip = {'*'}

if ( getlocalip( ip ) == 0 )

{

printf("本机IP地址是: %s\n", ip )

}

else

{

printf("无法获取本机IP地址 ")

}

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7993388.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-12
下一篇2023-04-12

发表评论

登录后才能评论

评论列表(0条)

    保存