2017-10-06 2 views
0

나는 scapy를 기반으로 ARPscanner를 만들려고합니다. 인터넷에서 코드를 찾았으므로 결과를 .txt 파일에 저장하도록 수정해야합니다. 누구든지 저를 도울 수 있습니까?scapy 명령의 데이터를 텍스트 파일로 가져 오시겠습니까?

repr(network) 
text_file = open("Output.txt", "w") 
text_file.write(repr(network)) 

을하지만 난 그냥 빈 파일을 가지고, 작동하지 않습니다

lena = int(raw_input("Enter Number : ")) 
print(lena) 

logging.basicConfig(format='%(asctime)s %(levelname)-5s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG) 
logger = logging.getLogger(__name__) 

def long2net(arg): 
    if (arg <= 0 or arg >= 0xFFFFFFFF): 
     raise ValueError("illegal netmask value", hex(arg)) 
    return 32 - int(round(math.log(0xFFFFFFFF - arg, 2))) 

def to_CIDR_notation(bytes_network, bytes_netmask): 
    network = scapy.utils.ltoa(bytes_network) 
    netmask = long2net(bytes_netmask) 
    net = "%s/%s" % (network, netmask) 
    if netmask < 16: 
     logger.warn("%s is too big. skipping" % net) 
     return None 

    return net 

def scan_and_print_neighbors(net, interface, timeout=1): 
    logger.info("arping %s on %s" % (net, interface)) 
    try: 
     ans, unans = scapy.layers.l2.arping(net, iface=interface, timeout=timeout, verbose=True) 
     for s, r in ans.res: 
      line = r.sprintf("%Ether.src% %ARP.psrc%") 
      try: 
       hostname = socket.gethostbyaddr(r.psrc) 
       line += " " + hostname[0] 
      except socket.herror: 
       # failed to resolve 
       pass 
      logger.info(line) 
    except socket.error as e: 
     if e.errno == errno.EPERM:  # Operation not permitted 
      logger.error("%s. Did you run as root?", e.strerror) 
     else: 
      raise 
if __name__ == "__main__": 
    if lena == 1: 
     for network, netmask, _, interface, address in scapy.config.conf.route.routes: 
      # skip loopback network and default gw 
      if network == 0 or interface == 'lo' or address == '127.0.0.1' or address == '0.0.0.0': 
       continue 

      if netmask <= 0 or netmask == 0xFFFFFFFF: 
       continue 

      net = to_CIDR_notation(network, netmask) 
      if interface != scapy.config.conf.iface: 
       # see http://trac.secdev.org/scapy/ticket/537 
       logger.warn("skipping %s because scapy currently doesn't support arping on non-primary network interfaces", net) 
       #continue 
      if net: 
       scan_and_print_neighbors(net, interface) 
       repr(network) 
       text_file = open("Output.txt", "w") 
       text_file.write(repr(network)) 

      elif lena == 3 : 
       print("Bye Bye ") 

은 내 스크립트에이 코드를 추가했습니다.

+0

어떤 도움이 – evilcode1

+0

hellooooooooooooooo 여기에 하나라도 도움을 주시기 바랍니다 – evilcode1

답변

0

해결 :

if __name__ == "__main__": 
import sys 
sys.stdout = open('textout.txt', 'w') 
... rest of your code here ... 
sys.stdout.close() 
관련 문제