2016-12-18 2 views
0

IP 주소 (인수)의 역방향 조회를 확인하려고합니다. 그 결과를 txt 파일에 씁니다. IP 주소 (인수)가 이미 파일에 등록되어 있는지 확인할 수 있습니까? 그렇다면 대본에서 벗어나야합니다.이미 명령 줄 인수가 사용되었는지 확인하십시오.

내 스크립트를

import sys, os, re, shlex, urllib, subprocess 

cmd = 'dig -x %s @192.1.1.1' % sys.argv[1] 

proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE) 
out, err = proc.communicate() 

# Convert to list of str lines 
out = out.decode().split('\n') 

# Only write the line containing "PTR" 
with open("/tmp/test.txt", "w") as f: 
for line in out: 
    if "PTR" in line: 
     f.write(line) 
+0

"파일에 등록"이란 무엇을 의미합니까? – 2ps

+0

@ 2ps : 나는 OP가 그가 내용을 쓰고있는 파일에 IP가 있음을 의미한다고 생각합니다. –

+0

스크립트는 dig 결과를 txt 파일에 씁니다. 인수 IP가 로그에 기록되는지 확인하고 싶습니다. 그렇다면 대본에서 벗어나야합니다. – OmZ

답변

0

뭔가 같은 :

otherIps = [line.strip() for line in open("<path to ipFile>", 'r')] 
theIp = "192.168.1.1" 
if theIp in otherIps: 
    sys.exit(0) 

otherIpsipFile의 IP 주소 list을 포함하고는, 당신은, 그렇다면 theIpotherIps에 이미 있는지 확인 필요 스크립트를 종료하십시오. 파일이 큰 경우 지금

with open('file.txt','r') as f: 
    content = f.read() 
if ip in content: 
    sys.exit(0) 

을하고 당신은 당신과 같이 mmap를 사용할 수 있습니다 가능한 메모리 문제를 해결하려면 : 파일이 너무 크지 않으면

1

당신은 할 수

import mmap 
with open("file.txt", "r+b") as f: 
    # memory-map the file, size 0 means whole file 
    mm = mmap.mmap(f.fileno(), 0) 
    if mm.find(ip) != -1: 
     sys.exit(0) 

mmap.find(string[, start[, end]])은 잘 문서화 된 here입니다.

관련 문제