获取外网IP(一)(解包需要优化)

/*
 * getip.c
 *
 *  Created on: 2013-7-11
 *      Author: Administrator
 */

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
typedef enum {
	false, true
} bool;

int searchip (char *buf,char *ip_cur)
{
	char *ptr = NULL;
	char *end = NULL;
	//char ip[32] = {0};
	ptr = strstr(buf,"Current IP Address:");
	ptr = ptr + 20;
	end = strstr(ptr,"<");
	printf("end:%s\n",end);
	printf("ptr:%s\n",ptr);
    memcpy(ip_cur,ptr,(end-ptr)/sizeof(char));
    printf("ip_cur:%s\n",ip_cur);

	}

int main() {

	bool flag;
	int sock;
	char **pptr = NULL;
	struct sockaddr_in destAddr;
	struct hostent *ptr = NULL;
	char destIP[128];
	char szBuffer[] =
			{
				"GET / HTTP/1.1\r\nUser-Agent: curl/7.19.7 (i486-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15\r\nHost: checkip.dyndns.org\r\nAccept: */*\r\n\r\n" };
	char res[1024] = {0};/********** 需要初始化*********/

	//初始化socket
	sock = socket(AF_INET, SOCK_STREAM, 0);
	if (-1 == sock) {
		perror("creat socket failed");
		exit(0);
	}

	bzero((void *) &destAddr, sizeof(destAddr));
	destAddr.sin_family = AF_INET;
	destAddr.sin_port = htons(80);
	//首先是获取www.ip138.com的IP地址http://city.ip138.com/ip2city.asp
	//ptr = gethostbyname("www.ip138.com");
	ptr = gethostbyname("checkip.dyndns.org");
	if (NULL == ptr) {
		perror("gethostbyname error");
		exit(0);
	}
	//对于www.ip138.com的每个IP进行尝试连接,直到有一个连接成功为止,或者无法连接,则退出程序
	for (flag = false, pptr = ptr->h_addr_list; NULL != *pptr; ++pptr) {
		inet_ntop(ptr->h_addrtype, *pptr, destIP, sizeof(destIP));
		printf("addr:%s\n", destIP);
		destAddr.sin_addr.s_addr = inet_addr(destIP);
		if (-1 != connect(sock, (struct sockaddr *) &destAddr,
						sizeof(struct sockaddr))) {
			flag = true;
			break;
		}
	}

	if (false == flag) {
		perror("connect failed");
	}

	//发送获取IP的数据包给www.ip138.com
	if (strlen(szBuffer) != send(sock, szBuffer, strlen(szBuffer), 0)) {
		perror("send error");
		exit(0);
	}
	//接收数据包
	if (-1 == recv(sock, res, 1024, 0)) {
		perror("recv error");
		exit(0);
	}
        printf("res:\n%s\n", res);
        char ip_cur[32] = {0};
        searchip(res,ip_cur);
        //searchip(res);
        printf("ip_cur:%s\n",ip_cur);
	return 0;
}

代码功能:获取外网的ip。通过模拟curl发送GET数据到相关的IP查询的网站,然后根据response的数据包解包获取需要的I外网IP的消息

模拟的curl指令:

curl "http://checkip.dyndns.org/" 2>/dev/null|awk '{print $6}'|cut -d '<' -f1

可以使用的curl:

  1. 1 curl "http://checkip.dyndns.org/" 2>/dev/null|awk '{print $6}'|cut -d '<' -f1 
  2. 2 curl -s "http://checkip.dyndns.org/"|cut -f 6 -d" "|cut -f 1 -d"<"         
  3. 3 w3m -dump http://submit.apnic.net/templates/yourip.html | grep -P -o '(\d+\.){3}\d+'      
  4. 4 curl -s "http://checkip.dyndns.org/"| sed 's/.*Address: \([0-9\.]*\).*/\1/g' 
  5. 5 curl -s "http://checkip.dyndns.org/"|cut -d "<" -f7|cut -c 26- 
  6. 6 curl ifconfig.me        -----------重点推荐这个,实在是好记 
  7. 7 curl icanhazip.com

编译:gcc -o getip1 getip1.c 

代码运行结果:
root@hj-desktop:/mnt/hgfs/share/program_test/testgetip# ./getip1
addr:216.146.43.70
addr:91.198.22.70
res:
HTTP/1.1 200 OK
Content-Type: text/html
Server: DynDNS-CheckIP/1.0
Connection: close
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 104


<html><head><title>Current IP Check</title></head><body>Current IP Address: 112.4.139.21</body></html>


end:</body></html>


ptr:112.4.139.21</body></html>


ip_cur:112.4.139.21
ip_cur:112.4.139.21

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值