0

I'm learning Python and have a problem. I can't execute following code in Windows CMD. I tried just an "Hello World" program and it executed as expected. but when i try this program nothing at all happens. No error message or anything. Im using python 3.3 and Pycharm 3.1.


__author__ = 'Johan'

import optparse
from socket import *

def connScan(tgtHost, tgtPort):
            try:
                connSkt = socket(AF_INET, SOCK_STREAM)
                connSkt.connect((tgtHost, tgtPort))
                connSkt.send('ViolentPython\r\n')
                results = connSkt.recv(100)
                print('[+]%d/tcp open'% tgtPort)
                print('[+] ' + str(results))
                connSkt.close()

            except:
                print('[-]%d/tcp closed'% tgtPort)

            def portScan(tgtHost, tgtPorts):
                try:
                    tgtIp = gethostbyname(tgtHost)
                except:
                    print ('[-] Can not resolve '': Unknown host' % tgtHost)
                    return
                try:
                    tgtName = gethostbyaddr(tgtIp)
                    print('\n[+] Scan Results for: ' + tgtName[0])
                except:
                    print('\n[+] Scan Results for: ' + tgtIp.
                        setdefaulttimeout(1))
                    for tgtPort in tgtPorts:
                        print ('Scanning Port ' + tgtPort)
                        connScan(tgtHost. int(tgtPort))

                def main():

                    parser = optparse.OptionParser('usage%prog ' + \
                        '-H <target host> -p <target port>')

                    parser.add_option('-H', dest='tgtHost', type=str, \
                        help='specify target host')
                    parser.add_option('-p', dest='tgtPort', type='int', \
                                      help='specify target port[s] separated by comma')
                    (options, args) = parser.parse_args()
                    tgtHost = options.tgtHost
                    tgtPorts = options.tgtPort
                    if(tgtHost is None) | (tgtPorts[0] is None):
                        print('[-] You must specify a target host and port[s]')
                        exit(0)
                        portScan(tgtHost, tgtPorts)
                    if __name__== '__main__':
                        main()
thegrinner
  • 11,546
  • 5
  • 41
  • 64
user3329934
  • 23
  • 1
  • 2
  • 6
  • 1
    How do you run this script? Please [edit] your question to include the command you use. – thegrinner Feb 25 '14 at 19:29
  • 2
    is this: `if __name__== '__main__': main()` really buried deep inside `main`? If so, that's your problem. It needs to be fully unindented to be called – mhlester Feb 25 '14 at 19:30
  • The indentation of the `if __name__ == 'main':` block looks wrong. Should be aligned at the very start of the line. – elParaguayo Feb 25 '14 at 19:30
  • I tried to run in CMD "python portscan.py -H 127.0.0.1 -p 20" – user3329934 Feb 25 '14 at 19:33
  • It's an impressive leap to go from "hello world" to this in one go :-) Or are you not the original author? It may be helpful to get in touch with him or her. – Kevin Feb 25 '14 at 19:33
  • Kevin i just wanted to try helloworld to see if it would execute and it dit so i knew that the path settings or anything wasn't wrong ;) i have written this from a book "Violent Python" – user3329934 Feb 25 '14 at 19:36
  • @user3329934: in answer to your comment the `if __name__ ...` line is fine, it's just your indentation that was wrong. It's buried inside your `main()` method, so it's not called when you run the script and, as Kevin says below, you've got other indentation issues too. – elParaguayo Feb 25 '14 at 19:36
  • OK so how do i fix that? – user3329934 Feb 25 '14 at 19:54

1 Answers1

2

Your indentation is all mangled - in addition to the if __name__ == "main" problem mentioned in other answers and comments, each function is nested inside the one before it, which is probably not the way it's supposed to be. You probably want something like this:

__author__ = 'Johan'
import optparse
from socket import *

def connScan(tgtHost, tgtPort):
    try:
        connSkt = socket(AF_INET, SOCK_STREAM)
        connSkt.connect((tgtHost, tgtPort))
        connSkt.send('ViolentPython\r\n')
        results = connSkt.recv(100)
        print('[+]%d/tcp open'% tgtPort)
        print('[+] ' + str(results))
        connSkt.close()

    except:
        print('[-]%d/tcp closed'% tgtPort)

def portScan(tgtHost, tgtPorts):
    try:
        tgtIp = gethostbyname(tgtHost)
    except:
        print ('[-] Can not resolve '': Unknown host' % tgtHost)
        return
    try:
        tgtName = gethostbyaddr(tgtIp)
        print('\n[+] Scan Results for: ' + tgtName[0])
    except:
        print('\n[+] Scan Results for: ' + tgtIp.
            setdefaulttimeout(1))
        for tgtPort in tgtPorts:
            print ('Scanning Port ' + tgtPort)
            connScan(tgtHost. int(tgtPort))

def main():

    parser = optparse.OptionParser('usage%prog ' + \
        '-H <target host> -p <target port>')

    parser.add_option('-H', dest='tgtHost', type=str, \
        help='specify target host')
    parser.add_option('-p', dest='tgtPort', type='int', \
                      help='specify target port[s] separated by comma')
    (options, args) = parser.parse_args()
    tgtHost = options.tgtHost
    tgtPorts = options.tgtPort
    if(tgtHost is None) | (tgtPorts[0] is None):
        print('[-] You must specify a target host and port[s]')
        exit(0)
        portScan(tgtHost, tgtPorts)

if __name__== '__main__':
    main()
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • I tried your code now but got: File "D:/Documents and Settings/Johan/PycharmProjects/PortScanner/PortScanner.py", line 52, in if __name__== '__main__':main() File "D:/Documents and Settings/Johan/PycharmProjects/PortScanner/PortScanner.py", line 47, in main if(tgtHost is None) | (tgtPorts[0] is None): TypeError: 'NoneType' object is not subscriptable – user3329934 Feb 25 '14 at 19:50
  • How should I "space" the code then? i can not compile that program it gives me the error above – user3329934 Feb 25 '14 at 20:09