from subprocess import Popen, PIPE
import re
import pygeoip
def get_traceroute_res(hostname, max_hop=10):
p = Popen(['traceroute', '-m', str(max_hop), hostname], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate()
rc = p.returncode
return output
def get_all_ip(result):
reip = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')
return [i for i in reip.findall(result) if i]
if __name__ == '__main__':
result = get_traceroute_res('comcast.com')
ip_list = get_all_ip(result)
gi = pygeoip.GeoIP("GeoLiteCity.dat")
for ip in ip_list:
ip_info = gi.record_by_addr(ip)
try:
print ip_info['country_name'], ip_info['latitude'], ip_info['longitude'], ip
except Exception, error:
pass
结果如下:
Japan 35.69 139.69 106.187.33.3
Japan 35.69 139.69 106.187.33.3
Japan 35.69 139.69 124.215.199.125
Japan 35.69 139.69 124.215.199.125
Japan 35.69 139.69 124.215.194.178
Japan 35.69 139.69 124.215.194.162
Japan 35.69 139.69 124.215.194.177
Japan 35.69 139.69 203.181.100.66
Japan 35.69 139.69 203.181.100.202
Japan 35.69 139.69 111.87.3.46
Japan 35.69 139.69 111.87.3.54
Japan 35.69 139.69 124.215.192.126
Japan 35.69 139.69 124.215.192.126
United States 38.0 -97.0 68.86.85.154
United States 38.0 -97.0 68.86.94.126
gcc:Desktop zookeep$ netcat 10.223.138.163 12345
ls | grep test
test
perl -e 'print "hello"';
hello
python -c "print 123"
123
uname -a
Darwin gcc.local 13.3.0 Darwin Kernel Version 13.3.0: Tue Jun 3 21:27:35 PDT 2014; root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
2. -v选项,打印出得到的消息,-n选项,只接受点分IP地址,不再进行DNS解析:
如对于google.com
gcc:~ zookeep$ ping -c 4 google.com
PING google.com (74.125.200.113): 56 data bytes
64 bytes from 74.125.200.113: icmp_seq=0 ttl=48 time=117.342 ms
64 bytes from 74.125.200.113: icmp_seq=1 ttl=48 time=122.505 ms
64 bytes from 74.125.200.113: icmp_seq=2 ttl=48 time=118.082 ms
64 bytes from 74.125.200.113: icmp_seq=3 ttl=48 time=134.491 ms
--- google.com ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 117.342/123.105/134.491/6.864 ms
gcc:~ zookeep$ netcat -v -n 74.125.200.113 80
74.125.200.113 80 (http) open
gcc:~ zookeep$ nc -v localhost 12345
nc: connectx to localhost port 12345 (tcp) failed: Connection refused
found 0 associations
found 1 connections:
1: flags=82
outif lo0
src 127.0.0.1 port 64609
dst 127.0.0.1 port 12345
rank info not available
TCP aux info available
Connection to localhost port 12345 [tcp/italk] succeeded!
lll
5.一个复杂点的扫描实例,-r随机扫描端口列表,-w3超时时间3秒,-z使用0输入输出:
gcc:~ zookeep$ nc -v -n -r -w3 -z 127.0.0.1 10-15
nc: connectx to 127.0.0.1 port 15 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 11 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 10 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 13 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 12 (tcp) failed: Connection refused
nc: connectx to 127.0.0.1 port 14 (tcp) failed: Connection refused
不记得第一次在哪里看到这句话(You had me at hello.),一开始不知道什么意思,后来百度,类似中文“一见钟情”的意思。不过,还是觉得原文更有画面感:两个陌生人,也许在他人相互介绍后,笑着说句"hello"(不过惯例应该是Nice to meet you啊,哈哈),然后内心各种澎湃。而一见钟情貌似有很多种方式,我能想到的多半是男生面对漂亮女生,瞬间感觉fucking high,就像You are beautiful中所描述的一样。
我曾有过一见钟情,不过还好,没有在fucking high后,因再也见不到而投海自尽。为什么是一见钟情而不是You had me at hello,因为我的hello还未说出口就没有然后了。可见,'You had me at hello.'和"一见钟情"还是有区别的,可以想象的画面是这样的:两人互有好感以后,有天谈论起什么时候有的感觉,在耳边小声地说:You had me at hello.而一见钟情根据我的经历可能就没有那种耳鬓厮磨的甜腻了。极端者如James Blunt所演绎的那样。