Welcome to Tech Support Forum home to more then 136,000 problems solved. Issues have included: Spyware, Malware, Virus Issues, Windows, Microsoft, Linux, Networking, Security, Hardware, and Gaming Getting your problem solved is as easy as:
1. Registering for a free account
2. Asking your question
3. Receiving an answer

Registered members:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* See fewer ads.
* And much more..

 



Want to know how to post a question? click here Having problems with spyware and pop-ups? First Steps
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
LinkBack Thread Tools
Old 04-08-2009, 04:58 AM   #1 (permalink)
Registered User
 
Join Date: Apr 2009
Posts: 3
OS: Windows 7 (Build 7000)


Question Help with Python Port Scanner Please

I started to learn python a few weeks ago and thought a port scanner would be a great way to learn a new language (string manip., networking, variables).

I even created a GUI for it using Tkinter, but I have two main problems:

1. The port scanner (Before I created the GUI) scanned until it found one open port, was supposed to close it, and the scan further. But, once it found an open port, it just flew through the others without scanning them, only displaying that it was trying to connect and went on to the next port.

2. Finding the first problem (Not being able to solve it), I decided to try tkinter.
It all worked until I needed to get the IP to scan from the user through a text box.

After creating the text box and retrieving the IP from it, there always seems to be a new line string after it?! This does not happen with the text boxes for the port to start and end.

Here is the full code of the port scanner as it is:

Code:
import socket
import Tkinter
print 'Modules Imported.'

win = Tkinter.Tk()
win.title('Port Scanner by Ianvdl')
win.geometry('400x180')
print 'Window Done.'

IP_Input = Tkinter.Text(win)
IP_Input.pack(expand=Tkinter.YES, fill=Tkinter.NONE)
IP_Input.place_configure(width=120, height=20)
IP_Input.place_configure(x=40, y=10)
print 'IP_Input Done.'

PortS = Tkinter.Text(win)
PortS.pack(expand=Tkinter.YES, fill=Tkinter.NONE)
PortS.place_configure(width=80, height=20)
PortS.place_configure(x=40, y=35)
print 'PortStart Done.'

PortE = Tkinter.Text(win)
PortE.pack(expand=Tkinter.YES, fill=Tkinter.NONE)
PortE.place_configure(width=80, height=20)
PortE.place_configure(x=40, y=60)
print 'PortEnd Done.'

ScanButton = Tkinter.Button(win, text='Scan')
ScanButton.pack(expand=Tkinter.YES, fill=Tkinter.NONE)
ScanButton.place_configure(x=10, y=150)
ScanButton.place_configure(width=120, height=20)
print 'Scanbutton Done.'

Label1 = Tkinter.Label(win, text='Percentage Done: Not Started.')
Label1.pack(expand=Tkinter.YES, fill=Tkinter.NONE)
Label1.place_configure(x=10, y=95)
Label1.place_configure(width=200, height=20)
print 'Label1 Done.'

IP_Input.insert(0.0, '127.0.0.1')
print IP_Input.get(0.0, Tkinter.END) #Debugging

PortS.insert(0.0, '1')
PortE.insert(0.0, '65000')
print 'Load Done.'
IP_Input.focus()

################################################################

def Scan():
    print 'Scan Called.' #Debugging
    IP = str(IP_Input.get(0.0, Tkinter.END))
    print IP #Debugging
    Start = int(PortS.get(0.0, Tkinter.END))
    End = int(PortE.get(0.0, Tkinter.END))
    TestSocket = socket.socket()
    CurrentPort = Start
    OpenPorts = 0
    print 'Starting scan...'
    HowFar = int(CurrentPort/End * 100)
    ProgText = HowFar, r'%'
    Label1.config(text=('Percentage Done:', ProgText))
    
    while CurrentPort <= int(End):
        print 'Attempting Connection at:', IP, ':', CurrentPort
        print IP #Debugging
        print CurrentPort #Debugging
        try:
            TestSocket.connect((str(IP), CurrentPort)) #Test the connection
            print 'Open at:', IP, ':', CurrentPort
            TestSocket.close()
            print 'Closed the connection.'
            CurrentPort += 1 
            OpenPorts += 1
        except:
            CurrentPort += 1 
        finally:
            HowFar = int(CurrentPort/End * 100)
            ProgText = HowFar, r'%'
            Label1.config(text=ProgText)
    else:
        print 'Scan completed from port:', Start, 'to port', End
        print 'Amount of Ports open:', OpenPorts
        HowFar = int(CurrentPort/End * 100)
        ProgText = HowFar, r'%'
        Label1.config(text=ProgText)

################################################################

ScanButton = Tkinter.Button(win, text='Scan', command=Scan)
ScanButton.pack(expand=Tkinter.YES, fill=Tkinter.NONE)
ScanButton.place_configure(x=10, y=150)
ScanButton.place_configure(width=120, height=20)

Tkinter.mainloop()
I know the code is very sloppy, and I should probably use threading to scan, but I have just started with Python.

Any help will be appreciated.
Ianvdl is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Important Information
Join the #1 Tech Support Forum Today - It's Totally Free!

TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free.

Join TechSupportforum.com Today - Click Here

Old 04-08-2009, 02:53 PM   #2 (permalink)
Tech, Networking Forums
 
Squashman's Avatar
 
Join Date: Apr 2005
Location: 1265 Lombardi Ave.
Posts: 1,142
OS: All of the above


Re: Help with Python Port Scanner Please

The best port scanner in the world I believe was written in Python. NMAP. You can download the program and I believe it comes with the source code.
Squashman is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 04-09-2009, 04:07 AM   #3 (permalink)
Registered User
 
Join Date: Apr 2009
Posts: 3
OS: Windows 7 (Build 7000)


Re: Help with Python Port Scanner Please

Thank you for the reply.

I have heard of NMAP before, but I think it might be a little difficult to master the source code.

But I'll check it out anyway. ;D
Ianvdl is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 04-09-2009, 04:14 AM   #4 (permalink)
Registered User
 
Join Date: Apr 2009
Posts: 3
OS: Windows 7 (Build 7000)


Re: Help with Python Port Scanner Please

Thank you for the reply.

I have heard of NMAP before, but I think it might be a little difficult to master the source code.

But I'll check it out anyway. ;D
Ianvdl is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 10:32 PM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85