2

Just typed up an banner grabber and port scanner from the 'Violent Python' book by TJ 'O Connor, I'm not getting any syntax errors when I run it, but I don't get any output what so ever either, can someone tell me what's potentially wrong? The books written in python 2.6, i'm using 2.7, I don't know if that's the issue maybe? Any help would be greatly appreciated! The book also had 'import socket as Ú' but that got syntax errors so I took it out, not sure what it did anyway

import optparse
import socket


def connScan(tgtHost,tgtPort):
        try:
                connSkt= socket(AF_INET,SOCK_STREAM)
                connSkt.connect((tgtHost,tgtPort))
                connSkt.send('Violent Python\r\n')
                results= connSkt.recv(1024)
                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 "[-] Cannot resolve '%s': Unkonwn host" % tgtHost
                return
        try:
                tgtName= gethostbyaddr(tgtIP)
                print '\n[+]Scan results for: ' + tgtIP
                setdefaulttimeout(1)
                for tgtPort in tgtPorts:
                        print 'Scanning port ' + tgtPort
                        connScan(tgtHost,int(tgtPort))
        except:
                print 'exception granted'    

def main():

        parser = optparse.OptionParser('usage %prog -h'+'<target host> -p <target port>')
        parser.add_option('-h', dest='tgtHost', type='string', help='specify target host')
        parser.add_option('-p', dest='tgtPort', type='int', help='specify target port[s] seperated by comma')
        (options,args) = parser.parse_args()

        tgtHost= options.tgtHost
        tgtPorts= str(options.tgtPort).split(',')

        if (tgtHost == None)|(tgtPorts[0] == None):
                print '[*] You must specify a target host and port[s]'
                exit(0)
                portScan(tgtHost,tgtPorts)
        if __name__=='__main__':
                main()
2
  • import module as x imports the module under a different name. A widely used example would be import numpy as np. This makes it possible to access the module under the different name. Commented Sep 5, 2017 at 11:25
  • is your if __name__ == '__main__': statement actually included in the function main or is that a SO copy/paste typo? Commented Sep 5, 2017 at 11:28

1 Answer 1

1

The reason nothing is happening is because your code consists entirely of function declarations. At no point do you actually tell python to run anything.

That job is supposed to be done by this if statement:

if __name__=='__main__':
        main()

However, you have mistakenly indented too much, thus making it a part of the main() function. For the code to work, you need to unindent it like so:

def main():
    parser = optparse.OptionParser('usage %prog -h'+'<target host> -p <target port>')
    parser.add_option('-h', dest='tgtHost', type='string', help='specify target host')
    parser.add_option('-p', dest='tgtPort', type='int', help='specify target port[s] seperated by comma')
    (options,args) = parser.parse_args()

    tgtHost= options.tgtHost
    tgtPorts= str(options.tgtPort).split(',')

    if (tgtHost == None)|(tgtPorts[0] == None):
            print '[*] You must specify a target host and port[s]'
            exit(0)
            portScan(tgtHost,tgtPorts)

if __name__=='__main__': # NOT a part of the main()
        main()

As for import socket as Ú, the purpose of this line is to import the module called socket, but give it an alias, in this case Ú. From then on, instead of referring to it as socket in your code, you refer to it as Ú.

Sign up to request clarification or add additional context in comments.

1 Comment

@donovon_duck Be sure to mark the answer as accepted if it solved your issue, and best of luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.