33,311
社区成员
发帖
与我相关
我的任务
分享
http://goo.gl/kX9WKW 服务器的照片
http://goo.gl/UGeU3x 服务器网址
http://goo.gl/maps/NGMkm 服务器的家
int main(int argc, char* argv[])
{
/* .... */
// 自动设置 techno.dnsdynamic.com网址密码
string cmdline = "wget --http-user=帐户 --http-passwd=密码 --no-check-certificate -q -O /tmp/techno.dnsdynamic.com"
"https://www.dnsdynamic.org/api/?hostname=techno.ns360.info\\&myip=";
cmdline += it->second;
system(cmdline.c_str());
system("cat /tmp/techno.ns360.info");
return 0;
}
使用 linux 定时执行任务功能,实现自动更新IP,完成报告
{
2013年 12月 28日 星期六 07:00:01 CST
<div>techno.dnsdynamic.com WAN<span> IP:
nochg
39.186.177.142</span> 服务器正常工作</div>
2013年 12月 28日 星期六 08:00:01 CST
<div>techno.dnsdynamic.com WAN<span> IP:
nochg
39.186.177.142</span> 服务器正常工作</div>
2013年 12月 28日 星期六 09:00:01 CST
<div>techno.dnsdynamic.com WAN<span> IP:
nochg
39.186.177.142</span> 服务器正常工作</div>
2013年 12月 28日 星期六 10:00:01 CST
<div>techno.dnsdynamic.com WAN<span> IP:
good 112.12.120.165
112.12.120.165</span> 服务器正常工作</div>
}
#define PCRE_STATIC // 静态库编译选项
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pcre.h"
#define OVECCOUNT 30 /* should be a multiple of 3 */
using namespace std;
bool DEBUG_MODE = 0;
std::string& regex_get_group(const std::string& src , const std::string& pattern ,
int group , std::string& str_group);
std::string& load_string(std::string& str, fstream& infile) // 加载文件到string
{
std::stringstream ss;
ss << infile.rdbuf();
str = ss.str();
return str;
}
void initializer_webdata(void)
{
system("touch /tmp/getwanip.txt");
// system("curl -s 192.168.1.1 | grep ipinfo >>/tmp/getwanip.txt");
system("curl -s http://www.ip138.com/ips1388.asp | grep ip_add.asp >>/tmp/getwanip.txt");
system("curl -s http://ns1.dnspod.net:6666 >>/tmp/getwanip.txt");
system("echo \" \" >>/tmp/getwanip.txt");
// system("curl -s http://myip.dnsdynamic.org/ >>/tmp/getwanip.txt");
if (DEBUG_MODE) {
system("cat /tmp/getwanip.txt");
cout << "\n----------------------初始化网络IP数据完成!----------------------\n";
}
}
// g++ -std=c++0x -lpcre -o getwanip 编译选项
int main(int argc, char* argv[])
{
string option_url;
for (int i = 0 ; i != argc ; i++)
option_url = option_url + " " + argv[i] ;
if (option_url.find("-d") != string::npos)
DEBUG_MODE = 1;
if (option_url.find("http") != string::npos) {
string cmdline = "curl -s ";
cmdline += option_url.substr(option_url.find("http"));
cmdline += " >/tmp/getwanip.txt";
system("touch /tmp/getwanip.txt");
system(cmdline.c_str());
if (DEBUG_MODE) {
cout << cmdline << endl;
system("cat /tmp/getwanip.txt");
cout << "\n----------------------初始化网络IP数据完成!----------------------\n";
}
} else
initializer_webdata();
std::fstream fs("/tmp/getwanip.txt", std::fstream::in);
std::string src; // 收集的网络 wanip数据
std::string reg = "(\\d+\\.\\d+\\.\\d+\\.\\d+)"; // 简单匹配IP地址正则公式
load_string(src, fs);
fs.close();
remove("/tmp/getwanip.txt");
std::string ipaddr;
regex_get_group(src, reg , 1 , ipaddr);
if (ipaddr == "")
return -1;
std::map<string, int> map_wanip;
while (ipaddr != "") {
if (DEBUG_MODE)
cout << ipaddr << endl;;
++map_wanip[ipaddr];
src = src.substr(src.find(ipaddr) + ipaddr.size());
regex_get_group(src, reg , 1 , ipaddr);
}
std::map< int , string> re_wanip; // 按计数器重排IP地址MAP容器
for (auto it = map_wanip.begin(); it != map_wanip.end() ; ++it) {
re_wanip.insert(make_pair(it->second , it->first));
}
auto it = re_wanip.begin(); // 最大值排最后
cout << it->second;
return 0;
}
std::string& regex_get_group(const std::string& src , const std::string& pattern ,
int group , std::string& str_group)
{
/* ............. */
}
A simple example to update a domain using the API directly;
https://username:password@www.dnsdynamic.org/api/?hostname=techno.ns360.info&myip=127.0.0.1
实际写了一个Shell 脚本可以工作
root@debian:~# cat ip.sh
#!/bin/bash
#
wget --http-user=帐号 --http-passwd=密码 --no-check-certificate \
-q -O ip.txt \
https://www.dnsdynamic.org/api/?hostname=srgb.x64.me\&myip=39.187.212.8
接下来,准备写个C/C++程序小工具,调用wget和grep 自动完成脚本的工作wget -q -O myip.txt http://www.net.cn/static/customercare/yourip.asp
grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' myip.txt
我实际是从 网关路由器获取的,路由器刷的 DD-WRT 系统
root@debian:~# cat srgb_wanip.sh
#!/bin/bash
wget -q 192.168.1.1 -O /tmp/ip.tmp
date >>/var/www/ip/index.html
echo " sRGB.vip.net 服务器正常工作" >>/var/www/ip/index.html
grep -e ipinfo /tmp/ip.tmp >>/var/www/ip/index.html
rm /tmp/ip.tmp
然后使用 定时每小时执行一次脚本 http://srgb.vicp.net/ip/
#!/bin/bash
touch /tmp/getwanip.txt
# 移动网络测试正确的
curl -s 192.168.1.1 | grep ipinfo >>/tmp/getwanip.txt # 如果DD-WRT的路由器这个最方便
curl -s http://www.ip138.com/ips1388.asp | grep ip_add.asp >>/tmp/getwanip.txt
curl -s http://ns1.dnspod.net:6666 >>/tmp/getwanip.txt
echo " " >>/tmp/getwanip.txt
curl -s http://www.net.cn/static/customercare/yourip.asp | grep ip >>/tmp/getwanip.txt # 今天挂了
# 移动网络不正确
curl -s http://myip.dnsdynamic.org/ >>/tmp/getwanip.txt
echo " " >>/tmp/getwanip.txt
curl -s http://ip.3322.net/ >>/tmp/getwanip.txt
echo " " >>/tmp/getwanip.txt
curl -s -I http://www.alibaba.com | grep ali_apache_id >>/tmp/getwanip.txt # 移动网络不正确
自己写工具,使用PCRE 再次检查IP,结果最多的肯定是 wanip
#define PCRE_STATIC // 静态库编译选项
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include "pcre.h"
#define OVECCOUNT 30 /* should be a multiple of 3 */
using namespace std;
std::string& regex_get_group(const std::string& src , const std::string& pattern ,
int group , std::string& str_group);
std::string& load_string(std::string& str, fstream& infile) // 加载文件到string
{
std::stringstream ss;
ss << infile.rdbuf();
str = ss.str();
return str;
}
// g++ -std=c++0x -lpcre 编译选项
int main()
{
std::fstream fs("/tmp/getwanip.txt", std::fstream::in);
std::string src; // 收集的网络 wanip数据
std::string reg = "(\\d+.\\d+.\\d+.\\d+)"; // 简单匹配IP地址正则公式
load_string(src, fs);
fs.close();
std::string ipaddr;
regex_get_group(src, reg , 1 , ipaddr);
if (ipaddr == "")
return -1;
std::map<string, int> map_wanip;
while (ipaddr != "") {
// cout << ipaddr << endl;;
++map_wanip[ipaddr];
src = src.substr(src.find(ipaddr) + ipaddr.size());
regex_get_group(src, reg , 1 , ipaddr);
}
std::map< int , string> re_wanip; // 按计数器重排IP地址MAP容器
for (auto it = map_wanip.begin(); it != map_wanip.end() ; ++it) {
re_wanip.insert(make_pair(it->second , it->first));
}
auto it = re_wanip.rbegin(); // 最大值排最后
cout << it->second;
return 0;
}
std::string& regex_get_group(const std::string& src , const std::string& pattern ,
int group , std::string& str_group)
{
pcre* re;
const char* error;
int erroffset;
int ovector[OVECCOUNT]; // 用来返回匹配位置偏移量的数组
// printf("原字符串 : %s\n", src.c_str());
// printf("匹配公式 : \"%s\"\n", pattern.c_str());
re = pcre_compile(pattern.c_str(), // pattern, 输入参数,将要被编译的字符串形式的正则表达式
0, // options, 输入参数,用来指定编译时的一些选项
&error, // errptr, 输出参数,用来输出错误信息
&erroffset, // erroffset, 输出参数,pattern中出错位置的偏移量
NULL); // tableptr, 输入参数,用来指定字符表,一般情况用NULL
// 返回值:被编译好的正则表达式的pcre内部表示结构
int rc = pcre_exec(re, // code, 输入参数,用pcre_compile编译好的正则表达结构的指针
NULL, // extra, 输入参数,用来向pcre_exec传一些额外的数据信息的结构的指针
src.c_str(), // subject, 输入参数,要被用来匹配的字符串
src.size(), // length, 输入参数, 要被用来匹配的字符串的指针
0, // startoffset, 输入参数,用来指定subject从什么位置开始被匹配的偏移量
0, // options, 输入参数, 用来指定匹配过程中的一些选项
ovector, // ovector, 输出参数,用来返回匹配位置偏移量的数组
OVECCOUNT); // ovecsize, 输入参数, 用来返回匹配位置偏移量的数组的最大大小
// 返回值:匹配成功返回非负数,没有匹配返回负数
if (rc < 0) {
pcre_free(re);
return str_group = "";
}
// for (int i = 0; i < rc; i++) {
// int substring_length = ovector[2 * i + 1] - ovector[2 * i]; // 字符串结束的索引 减去 开始的索引,就是字符串长度
// string str = src.substr(ovector[2 * i] , substring_length); // 分别取出每个捕获分组
// cout << "捕获分组: " << i << "\t" << str << endl;
// }
int substring_length = ovector[2 * group + 1] - ovector[2 * group]; // 字符串结束的索引 减去 开始的索引,就是字符串长度
str_group = src.substr(ovector[2 * group] , substring_length); // 获得捕获分组
pcre_free(re); // 编译正则表达式re 释放内存
return str_group;
}
http://ip.3322.net/ // 移动网络不正确
curl -s -I http://www.alibaba.com | grep ali_apache_id // 移动网络不正确
http://www.net.cn/static/customercare/yourip.asp // 今天挂了
curl -s http://www.ip138.com/ips1388.asp | grep ip_add.asp
curl -s http://ns1.dnspod.net:6666
curl -s 192.168.1.1 | grep ipinfo // 如果DD-WRT的路由器这个最方便
准备写的工具的,最后选择使用 PCRE正则库,下面是原始版本
#define PCRE_STATIC // 静态库编译选项
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include "pcre.h"
#define OVECCOUNT 30 /* should be a multiple of 3 */
using namespace std;
std::string& regex_get_group(const std::string& src , const std::string& pattern ,
int group , std::string& str_group);
int main()
{
string src = "<div class=\"info\">WAN<span id=\"ipinfo\"> IP: 39.187.212.8</span></div>"; // 要被用来匹配的字符串
string reg = "<span.*IP: (\\d+.\\d+.\\d+.\\d+)</span>"; // 将要被编译的字符串形式的正则表达式
cout << regex_get_group(src, reg , 1 , src);
return 0;
}
http://ip.3322.net/ // 移动网络不正确
curl -s -I http://www.alibaba.com | grep ali_apache_id // 移动网络不正确
http://www.net.cn/static/customercare/yourip.asp // 今天挂了
curl -s http://www.ip138.com/ips1388.asp | grep ip_add.asp
curl -s http://ns1.dnspod.net:6666
curl -s 192.168.1.1 | grep ipinfo // 如果DD-WRT的路由器这个最方便
std::string& regex_get_group(const std::string& src , const std::string& pattern ,
int group , std::string& str_group)
{
pcre* re;
const char* error;
int erroffset;
int ovector[OVECCOUNT]; // 用来返回匹配位置偏移量的数组
// printf("原字符串 : %s\n", src.c_str());
// printf("匹配公式 : \"%s\"\n", pattern.c_str());
re = pcre_compile(pattern.c_str(), // pattern, 输入参数,将要被编译的字符串形式的正则表达式
0, // options, 输入参数,用来指定编译时的一些选项
&error, // errptr, 输出参数,用来输出错误信息
&erroffset, // erroffset, 输出参数,pattern中出错位置的偏移量
NULL); // tableptr, 输入参数,用来指定字符表,一般情况用NULL
// 返回值:被编译好的正则表达式的pcre内部表示结构
int rc = pcre_exec(re, // code, 输入参数,用pcre_compile编译好的正则表达结构的指针
NULL, // extra, 输入参数,用来向pcre_exec传一些额外的数据信息的结构的指针
src.c_str(), // subject, 输入参数,要被用来匹配的字符串
src.size(), // length, 输入参数, 要被用来匹配的字符串的指针
0, // startoffset, 输入参数,用来指定subject从什么位置开始被匹配的偏移量
0, // options, 输入参数, 用来指定匹配过程中的一些选项
ovector, // ovector, 输出参数,用来返回匹配位置偏移量的数组
OVECCOUNT); // ovecsize, 输入参数, 用来返回匹配位置偏移量的数组的最大大小
// 返回值:匹配成功返回非负数,没有匹配返回负数
if (rc < 0) {
pcre_free(re);
return str_group = "";
}
// for (int i = 0; i < rc; i++) {
// int substring_length = ovector[2 * i + 1] - ovector[2 * i]; // 字符串结束的索引 减去 开始的索引,就是字符串长度
// string str = src.substr(ovector[2 * i] , substring_length); // 分别取出每个捕获分组
// cout << "捕获分组: " << i << "\t" << str << endl;
// }
int substring_length = ovector[2 * group + 1] - ovector[2 * group]; // 字符串结束的索引 减去 开始的索引,就是字符串长度
str_group = src.substr(ovector[2 * group] , substring_length); // 获得捕获分组
pcre_free(re); // 编译正则表达式re 释放内存
return str_group;
}