User:Plume

From ICO wiki

Skriptimiskeeled Quest 2

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Autor: Priit Lume
# Ryhm: AK51
# Skript kontrollib argumendina antava  ipv4 aadressi lahtiseid
# porte. Kontrollitavad pordid v6etakse teise argumendina antavast
# failist, kus need peavad paiknema yhekaupa eraldi ridadel. 
# Litsentsinfo: Skript on koostatud IT Kolledzhi aine 
# skriptimiskeeled arvestust88ks. Autor ei sea piiranguid selle 
# kasutamiseks va. skripti esitamine hindamiseks enda nime alt. 
# versioon 0.2 (02.02.2013)

from sys import argv
from socket import *  

#Argumentide arvu kontroll
if len(argv) != 3:
	print 'Peab olema 2 argumenti - ip aadress ja failinimi'
	exit(3)
#Muutujad
addr = argv[1]
fail = argv[2]
check_ports = []
open_ports = []

#ip aadressi kontroll
try:
    inet_aton(addr)
except IOError, err:
    print 'Pole korrektne ip aadress'
    exit(4)
#faili olemasolu ja lugemis6iguse kontroll
try:
    open(fail, 'r').read()
except IOError, err:
    print 'IOError: ', err
#Failist portide lugemine ja korrastamine
with open(fail, 'r') as (f):
    for i in f.read().split():
        check_ports.append(int(i))
    f.close()
check_ports.sort()

print 'Kontollin aadressil %s porte %s' % (addr, check_ports)
for port in check_ports:    
    s = socket(AF_INET, SOCK_STREAM)
    if s.connect_ex((addr, port)) == 0:
        open_ports.append(port)
    s.close()

if len(open_ports) == 0:
    print 'Ykski ette antud portidest pole lahti'
    exit(2)

if len(open_ports) < len(check_ports):
    print 'M6ni ette antud portidest on kinni'
    exit(2)

#Ylej22nud portide kontroll
for port in range(1, 10001):
    if port in check_ports:
        continue
    s = socket(AF_INET, SOCK_STREAM)
    if s.connect_ex((addr, port)) == 0:
        print 'Peale ette antud pordide on veel lahtiseid porte'
        exit(1)
    s.close()
 
if open_ports == check_ports:
    print 'Vaid ette antud pordid on lahti'
    exit(0)