功能:主动验证给定的域名、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. Scrapy 'module' object has no attribute 'Spider'错误

    在“Scrapy入门教程”中,在创建的“dmoz_spider.py”文件中是通过 import scrapy class DmozSpider(scrapy.Spider): 的方式导入.但是用这种 ...

  2. JavaScript算法题(一) && 数组reduce使用

    可参考Array.reduce用法 1. 请编写getMissingElement函数,返回给定数组中缺少的元素(数组里的元素为0~9,只会缺失一个). Example: getMissingElem ...

  3. 使用C++11的thread取代QThread

    因为在做的工程项目里使用了Qt,而实际上不涉及到屏幕显示,工程代码里使用了QThread,且没有使用Qt核心的信号与槽,为了以后移植准备使用更加通用的C++11 stl中的thread取代QThrea ...

  4. Java中需要了解的点

    1.32位jvm.64位区别? 2.

  5. SDUT OJ 1221 亲和数 (找出某个数n所有的因子数,只需要暴力:2->sqrt(n) 即可 )

    亲和数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 如果a的因子和等于b,b的因子和等于a,且a≠b,则称a,b为亲和数对. ...

  6. 51Nod 1282 时钟 —— 最小表示法 + 字符串哈希

    题目链接:https://vjudge.net/problem/51Nod-1282 1282 时钟 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难 ...

  7. FZU2150 Fire Game —— BFS

    题目链接:https://vjudge.net/problem/FZU-2150 Problem 2150 Fire Game Accept: 2702    Submit: 9240 Time Li ...

  8. ASP.NET Session and Forms Authentication and Session Fixation

    https://peterwong.net/blog/asp-net-session-and-forms-authentication/ The title can be misleading, be ...

  9. php连接数据库步骤

    第一步:连接数据库 $link=@mysql_connect('localhost','root','root') or die('数据库连接失败!'); echo '连接成功!'; 这里数据库连接函 ...

  10. wireshark分析ssl协议

    1.什么是ssl SSL(Secure Sockets Layer 安全套接层),及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种 ...