2016-06-19 10 views
0

簡単なIPアドレス抽出タスクを実行しているうちに、プログラムが正常に動作していることがわかりました。しかし、Webクローリングのための完全なプログラムでは、それは生き残ることができず、不均一な結果をもたらします。ウェブスキャンでipを抽出する方法

これは、IPアドレスのための私のコードスニペットです:

#!/usr/bin/python3 

    import os 
    import re 

    def get_ip_address(url): 
     command = "host " + url 
     process = os.popen(command) 
     results = str(process.read()) 
     marker = results.find("has address") + 12 
     n = (results[marker:].splitlines()[0]) 
     m = re.search('\w+ \w+: \d\([A-Z]+\)', n) 
     if m is not None: 
      url_new = url[8:] 
      command = "host " + url_new 
      process = os.popen(command) 
      results = str(process.read()) 
      marker = results.find("has address") + 12 
      return results[marker:].splitlines()[0] 

    print(get_ip_address("https://www.yahoo.com")) 

ウェブクローリングのための完全なプログラムは次のようになります。

[email protected]:~/Desktop/web_scanning# python3 main.py 
    106.10.138.240 
    Enter the Company Name: Yahoo 
    Enter the complete url of the company: https://www.yahoo.com/ 
    /bin/sh: 1: Syntax error: "(" unexpected 

#!/usr/bin/python3 

    from general import * 
    from domain_name import * 
    from ip_address import * 
    from nmap import * 
    from robots_txt import * 
    from whois import * 

    ROOT_DIR = "companies" 
    create_dir(ROOT_DIR) 

    def gather_info(name, url): 
     domain_name = get_domain_name(url) 
     ip_address = get_ip_address(url) 
     nmap = get_nmap('-F', ip_address) 
     robots_txt = get_robots_txt(url) 
     whois = get_whois(domain_name) 
     create_report(name, url, domain_name, nmap, robots_txt, whois, ip_address) 

    def create_report(name, full_url, domain_name, nmap, robots_txt, whois, ip_address): 
     project_dir = ROOT_DIR + '/' + name 
     create_dir(project_dir) 
     write_file(project_dir + '/full_url.txt', full_url) 
     write_file(project_dir + '/domain_name.txt', domain_name) 
     write_file(project_dir + '/nmap.txt', nmap) 
     write_file(project_dir + '/robots_txt.txt', robots_txt) 
     write_file(project_dir + '/whois.txt', whois) 
     write_file(project_dir + '/ip_address.txt', ip_address) 

    x = input("Enter the Company Name: ") 
    y = input("Enter the complete url of the company: ")  
    gather_info(x , y) 

入力は、このようなルックスを入力します

ip_address.txtの出力は次のとおりです。

hoo.com/ not found: 3(NXDOMAIN) 

見られるようなプログラムは、実行時にうまく動作し、106.10.138.240はまだまた、私はこの/ binに/ shの構文エラーが来た方法を見つけることができなかったip_address.txt で別の何かを保存するように、IPを提供します。助けてください...

答えて

0

Iあなたのimport文でないワイルドカードを使用する2番目のジョー・リンの提案。あなたの名前空間を大きく汚染し、奇妙な行動を起こす可能性があります。

Pythonはあるあなたはおそらく、HTTPリクエストをrequestsurllib3パッケージを活用する必要があるコマンドを実行するため慎重subprocessを使用して、ウェブスクレイピングのためのscrapyパッケージをチェックアウトするように、「付属の電池」。それぞれのオブジェクトとメソッドが返すデータには、抽出しようとしているものがある可能性があります。

  • https://docs.python.org/2/library/subprocess.html
  • http://docs.python-requests.org/en/master/
  • できるだけ怠惰にし、従来技術」に依存しています。

    def get_ip_address(url): 
        command = "host " + url 
        process = os.popen(command) 
        .... 
    

    私はシェルでこのコマンドを実行した場合、それは文字通り、このミラーになります:私は次のことがわかりget_ip_addressの最初の数行で「

    host http://www.foo.com 
    

    man hostを行うとし、マニュアルページを読む:

    host is a simple utility for performing DNS lookups. It is normally 
        used to convert names to IP addresses and vice versa. When no arguments 
        or options are given, host prints a short summary of its command line 
        arguments and options. 
    
        name is the domain name that is to be looked up. It can also be a 
        dotted-decimal IPv4 address or a colon-delimited IPv6 address, in which 
        case host will by default perform a reverse lookup for that address. 
        server is an optional argument which is either the name or IP address 
        of the name server that host should query instead of the server or 
        servers listed in /etc/resolv.conf. 
    

    あなたはhostは、IPアドレスまたはホスト名だけが必要な場合にURLを入力します。 URLには、スキーム、ホスト名、およびパスが含まれます。 hostと対話するように選択したホスト名を明示的に抽出する必要があります。 URLは/ディテールパス情報を含んでも含まなくてもよいことを考えると、あなたはそれを解明する必要があります。

    url= "http://www.yahoo.com/some_random/path" 
    
    # Split on "//" to extract scheme 
    _, host_and_path = url.split("//") 
    
    # Use .split() with maxsplit 1 to break this into pieces as desired 
    hostname , path = host_path.split("/", 1) 
    
    # # Use 'hostname' as input to the command 
    command = "host " + url 
    ... 
    

    私は質問は、この問題に関連して、コードのすべてを提供しているとは思いません。エラー出力は、従来のPythonスタックトレースではなく、シェルベースであると思われます。get_something関数のうちの1つは、Popenを使用して、いくつかのシェルコマンドを実行します。

    +0

    私は、メインファイルにわずかなバリエーションを試して、ip_address()のdomain_nameを渡して、スムーズに動作します。私はワイルドカードのコンセプトに同意し、それを世話しようとします。 –

    +0

    私はホスト名について提案したことを試してみました。私はあなたの考え方が好きです...「できるだけ怠惰で、先行技術に頼ってください」 –

    0

    申し訳ありません申し訳ありませんが、コメントを追加するには十分な評判がないので、私はここに提案を掲載します。

    process = os.popen(command)からdef get_ip_address(url)に問題があると思います。 commandを印刷して、有効かどうかを確認することができます。問題のほかに

    、ちょうどいくつかの提案:

    1. は、それがコードをトレースする読者が難しくなりますから、輸入に*を使用しないようにしてください。

    2. pdbは、pythonデバッガであり、中小規模のプロジェクトでもシンプルだがパワフルです。それを使用する最も簡単な方法は、コードを行ごとに実行できるように、プログラムを停止する行の前にimport pdb; pdb.set_trace()を追加することです。