功能:主动验证给定的域名、IP对是否真正的关联

思路:

  1、一开始通过修改hosts文件,把待验证的域名、IP对添加到文件里,然后用wget尝试访问,再恢复hosts文件重新验证下一对

  2、后来了解到curl命令可以带参数的形式指定访问域名的解析IP,于是改用curl验证。但是要在防火墙上关闭DNS服务,要不然会主动请求外网的DNS服务。

主动验证的脚本如下

#curl www.google.com -L -i --resolve www.google.com:80:123.34.35.41 -o index.html
curl $ -L -i --resolve $:$ -o index.html
if [ ! -f "index.html" ]; then
echo false
else
echo true
fi
if [ -f "index.html" ]; then
rm index.html
fi

参数意义:

-L  允许重定向后继续访问重定向的URL

-i  输出返回的http头部

-o  将输出信息输出到指定文件里

--resolve  指定待访问域名的解析IP,注如果解析IP不对,curl会去尝试访问外网的DNS服务器来获得真正的IP,所以个人觉得应该将程序部署在qiang内

读输入文件调用shell脚本的Java程序

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle; import org.apache.log4j.Logger; /*
* Date:2017-09-30
* Author:glt
* */ public class RunShell { static Logger log = Logger.getLogger(RunShell.class); public static void main(String[] args){
ResourceBundle config = ResourceBundle.getBundle("filePath");
String inputPath = config.getString("inputPath");
String outputPath = config.getString("outputPath");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String today = sdf.format(new Date());
File input = new File(inputPath + today + "-validating.txt");
File output = new File(outputPath + today + "-validated.txt");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(input));
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(output, true));
} catch (IOException e1) {
e1.printStackTrace();
}
String line = null;
try {
while((line = br.readLine()) != null){
String[] fields = line.split("\t");
long seq = Long.parseLong(fields[0]);
String ip = fields[1];
String domain = fields[2];
String shpath = "./addHosts.sh " + ip + " " + domain;
log.info(shpath);
Process ps = Runtime.getRuntime().exec(shpath);
ps.waitFor();
BufferedReader console = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String consoleLine = null;
while ((consoleLine = console.readLine()) != null) {
sb.append(consoleLine).append("\n");
}
console.close();
String result = sb.toString();
log.info(seq + "\t" + result);
if(result.contains("false")){
bw.write(seq + "\t" + ip + "\t" + domain + "\t-1\n");
}else{
bw.write(seq + "\t" + ip + "\t" + domain + "\t1\n");
}
bw.flush();
}
br.close();
bw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}

关闭DNS服务

sudo iptables -A OUTPUT -p tcp --dport  -j DROP
sudo iptables -A OUTPUT -p udp --dport -j DROP

恢复DNS服务

ps:后面会讲一讲为什么这种方法能够验证域名-IP对


参考:http://www.ruanyifeng.com/blog/2011/09/curl.html

   http://www.cnblogs.com/lihuobao/p/6434341.html

   https://yq.aliyun.com/articles/40772

   http://www.cnblogs.com/grimm/p/5362096.html

   http://www.linuxidc.com/Linux/2016-09/134941.htm

域名IP主动验证(一)的更多相关文章

  1. apache本地域名ip重定向vhosts

    apache本地域名ip重定向,使本机通过指定域名访问到指定ip路径. 1.apache配置apache/conf/httpd.conf  : 开启配置 Include conf/extra/http ...

  2. 请求一个域名ip的缓存用处

    前言 摘录自操作系统,这一段的内容很有启发,稍微加上自己的理解,写一篇博客记录一下. 缓存 缓存成功解决了速度不匹配设备之间的数据传输,并且在一般情况下,是整个系统的瓶颈:缓存的出现,有效减少了低速I ...

  3. 域名ip自动跳转 跳转指定页面的js

    域名ip自动跳转 跳转指定页面的js 为了应对百度审核,需要客户的网站在个别地区跳转到另一个页面,就搞到了这段代码,屡试不爽,超实用.下面把地址换成你要访问的网站url地址或者文件url地址即可.超实 ...

  4. 内网客户 通过 公网域名/ip 访问内网web服务器 出错

    在一内部局域网中, client  内网地址为 10.0.0.2     web  服务器内网地址为 10.0.0.1    外网地址为  211.6.15.1    域名为  xx.love.com ...

  5. [LeetCode] Validate IP Address 验证IP地址

    In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...

  6. 网站用域名能访问,用域名IP不能访问的原因分析

    原因分析:一般虚拟主机是不能直接输入IP进行访问的 因为一个IP下有很多网站 ,只能用域名进行访问.如果想IP也能访问,必须网站有独立的IP地址,不是共享IP.如果是IIS的话,要把主机头对应的域名去 ...

  7. IP地址验证

    /** * 验证IP地址 * * @param 待验证的字符串 * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false ...

  8. 更改EBSserver域名/IP

    more: 341322.1 : How to change the hostname of an Applications Tier using AutoConfig 338003.1 : How  ...

  9. ip完整验证详情

    不想跳坑就看一下 之前一直不太会写正则表达式,很多要用到正则表达式的都直接百度,像上次要用正则表达式验证是否是合法的ip地址,然后就上网找,结果就是没找到一个对的,今天就为大家贡献一下,写个对的,并做 ...

随机推荐

  1. Spark理论学习笔记(一)

    1.调度 分为FIFO和FAIR两种模式 创建调度池:sc.setLocalProperty("spark.scheduler.pool", "pool6") ...

  2. ActiveMQ P2P模型 观察者消费

    生餐者: package clc.active.listener; import org.apache.activemq.ActiveMQConnectionFactory; import org.t ...

  3. ⭐register_chrdev、register_chrdev_region以及alloc_chrdev_region之间的区别

    register_chrdev:Linux2.6.30之前所用,不用定义cdev:但 如果是register_chrdev 注册的话,这个时候,分配的次设备号,是从0~255,这样子的话,就分配的范围 ...

  4. Python return语句用法分析

    return 语句 程序运行到所遇到的第一个return即返回(退出def块),不会再运行第二个return. 要返回两个数值,写成一行即可: def a(x,y): if x==y: return ...

  5. codeforces 673D D. Bear and Two Paths(构造)

    题目链接: D. Bear and Two Paths time limit per test 2 seconds memory limit per test 256 megabytes input ...

  6. python中的异常处理try/except/finally/raise

    异常发生在程序执行的过程中,如果python无法正常处理程序就会发生异常,导致整个程序终止执行,python中使用try/except语句可以捕获异常. try/except 异常的种类有很多,在不确 ...

  7. BZOJ_4276_[ONTAK2015]Bajtman i Okrągły Robin_线段树优化建图+最大费用最大流

    BZOJ_4276_[ONTAK2015]Bajtman i Okrągły Robin_线段树优化建图+最大费用最大流 Description 有n个强盗,其中第i个强盗会在[a[i],a[i]+1 ...

  8. NSCoding

    在IOS的开发中,小数据量的持久化都用NSUserDefaults来实现,但是NSUserDefaults只能保存NSString, NSNumber, NSDate, NSArray, NSDict ...

  9. CMake命令之list

    用途:提供一些列表操作 list(LENGTH <list><output variable>)  list(GET <list> <elementindex ...

  10. 入口函数WinMain

    int WINAPI WinMain() HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd ); ...