issue with this program and sockets

issue with this program and sockets

Postby Comp_guymn » Tue Sep 17, 2013 6:46 am

Hey guy, IM n00b at python and im trying to follow this book to compile a tcp port scanner. im using idle in 2.7.5 and I tried writing my own but in the process I wanted to test out the actually coding in the book and it coming up with errors, the coding seems logical correct after comparing with socket module itself so im not sure why, please throw some feedback , thankyou

from socket import *
def connScan(tgtHost, tgtPort):
try:(
connSkt = socket(AF_INET, SOCK_STREAM) # <-----------INVAILD SNYTAX ERROR
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 "[-] Cannot resolve '%s': 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='string', \
help='specify target host')
parser.add_option('-p', dest='tgtPort', type='string', \
help='specify target port[s] separated 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()
Last edited by Yoriz on Tue Sep 17, 2013 4:34 pm, edited 1 time in total.
Reason: First post lock
Comp_guymn
 
Posts: 3
Joined: Tue Sep 17, 2013 6:40 am

Re: issue with this program and sockets

Postby Yoriz » Tue Sep 17, 2013 4:37 pm

Hi, welcome to the forum.
Indentation is important in python code, you code needs to be posted inside code tags to preserve the indentation.
Please read the 'new users read this' link in my signature for how you should post your code and errors.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: issue with this program and sockets

Postby micseydel » Tue Sep 17, 2013 5:32 pm

Get rid of the open parenthesis following the colon for your try.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1271
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to Networking

Who is online

Users browsing this forum: No registered users and 0 guests