功能:主动验证给定的域名、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. lint (software)

    lint (software) - Wikipedia https://en.wikipedia.org/wiki/Lint_(software) A linter or lint refers to ...

  2. Do not use the <section> element as a generic container; this is what <div> is for, especially when the sectioning is only for styling purposes.

    Do not use the <section> element as a generic container; this is what <div> is for, espe ...

  3. BZOJ5379: Tree

    BZOJ5379: Tree Description JudgeOnline/upload/201806/1.pdf 题解Here! 题目大意就是:1. 换根.2. 对$LCA(u,v)$的子树修改. ...

  4. JS简单正则得到字符串中特定的值

    这里就直接看演示样例吧.演示样例的目的是为了获取 a 字符串中的 c02806015 <script language="javascript"> var a = '礼 ...

  5. 转载-STM32片上FLASH内存映射、页面大小、寄存器映射

    原文地址:http://blog.chinaunix.net/uid-20617446-id-3847242.html 本文以STM32F103RBT6为例介绍了片上Flash(Embedded Fl ...

  6. gson如何转化json数组

    String.JsonObject.JavaBean 互相转换 User user = new Gson().fromJson(jsonObject, User.class); User user = ...

  7. Vue源码探究-源码文件组织

    Vue源码探究-源码文件组织 源码探究基于最新开发分支,当前发布版本为v2.5.17-beta.0 Vue 2.0版本的大整改不仅在于使用功能上的优化和调整,整个代码库也发生了天翻地覆的重组.可见随着 ...

  8. js实现网页多少秒后自动跳转到指定网址

    在网上搜了一下,关于这个技术处理有多种方法,我只记下我在视频里学到的三种: 1.用一个response.sendRedirect("目标页面.jsp\.htm");实现直接跳转: ...

  9. c#-关于自动属性的思考

    参考:c#-关于自动属性的思考 我的理解:自动属性跟 公有字段 一模一样,编程习惯而已.目前是这么认为的. 自动属性:public string Name{  get;  set } 公有字段:pub ...

  10. JQuery判断div(控件)是否为隐藏

    以下是JavaScript 中判断div是否为隐藏代码引用片段: if (div.style.display == "none") { div.style.display = &q ...