How to print description of port - python

So I made a port scanner and I want to give description to the ports. So like printing the port with the description. I was thinking my best bad is to make the description on a file and then print it but I don't know how to do that. Or on the file I could put the port and then the description separate them with a ":", search for that port on the file and get the description.
def scan_port(port):
try:
timeout = 10.0
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
result = sock.connect_ex((remoteHost,port))
identify_port()
if result == 0:
print(colored("[+] Port {}: Open".format(port), 'green'))
sock.close()

Related

Socket is Showing All Ports closed

I was trying to scan ports through socket but it's show all ports closed. Here is my code:
import socket
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = input(" Please Input Ip address To Scan ")
#port = input(" ENter The Port ")
def portscanner(host):
for port in range(1,150):
if sock.connect_ex((host,int(port))):
print(f"{port} Is Closed")
else:
print("port is open")
portscanner(host)
Try creating a connection inside the forloop. And make sure that the input is in valid form.
You can do that using
try and catch near the sock.connect_ex to check whether you are actually sending valid host or not.
To make things faster you can use settimeout(0.25) inside the for loop too.
I meant to do this -
for port in range(start, end):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(.25)
result = sock.connect_ex((host, port))
if result == 0:
print(port,'port is open')
sock.close()

Python socket client works only for one iteration

I am trying to implement sockets with python.the following code works well without the while loop..but with the while loop, for the second iteration , it gets stuck in s.sendall().could you please suggest how to fix this ?
def main():
host = socket.gethostname()
port = 11111
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
print "Connected to Server!"
while True:
print "Enter your Command:"
command = raw_input()
if(command):
try:
sock.sendall(repr(command))
except socket.error:
print "Socket Error Occured"
data = sock.recv(1024)
if data:
print('Received', repr(data))
else:
print "No Data"
else:
os.system("clear")
sock.close()
if __name__ == "__main__":
main()
Hello
I don't have all the information I need to make this post as
informed as I'd like, however I can make some educate
d guesses and try my best to explain what I think is going wrong. Are you ready?? Lets get into it.
So,
You start off by making a tcp socket and then connecting to a server hosted locally on port 11111
host = socket.gethostname()
port = 11111
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
next you enter a loop
while True:
#input command, request eval
Here your goal is to take user input and send it to a server to eval. You do that with
#get user input
print "Enter your Command:"
command = raw_input()
#send command to server for eval
sock.sendall(repr(command))
#receive then print eval
data = sock.recv(1024)
print('Received', repr(data))
this works and sends commands as you'd expect, although sending repr(command) might not be what you want to send
command = "1+1"
eval(command)
//2
eval(repr(command))
//'1+1'
Now
Here is where I have to make some assumptions
Early on you connected to a server
sock.connect((host, port))
I'm assuming that the server accepts your connection evals your command and sends the answer back. Something like
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind('localhost',11111)
sock.listen()
while True:
conn, addr = sock.accept()
command = conn.recv(1024)
sock.sendall(eval(command))
If this is the case then your connection might fail because your eval server runs eval once and then accepts a new connection.
That means that your client can no longer send to, or recieve data from, the server
I hope this helps.

python - print out ports with their names

I have the following code written in 2.7 python:
#...import stuff
remoteServer = raw_input("Enter a remote host to scan: ")
remoteServerIP = socket.gethostbyname(remoteServer)
print "Please wait, scanning remote Host", remoteServerIP
try:
for port in xrange(1, 1024):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((remoteServerIP, port))
if result == 0:
print "port {}: open".format(port)
sock.close
except KeyboardInterrupt:
print "\nexiting..."
sys.exit()
Output:
Enter a remote host to scan: www.myexamplesite.com
Please wait, scanning remote Host xxx.xxx.xx.xx
port 21: open
port 22: open
...
But the problem is that I also want to know which ports are used and for what they are used just like:
#... as usual
port 1 httpserver
port 2 chat server
...
but this is only printing the ports from 1 to 1024
is there a function/way to do this?
socket.getservbyport() will translate port numbers into the service expected to be running on that port (via /etc/services), but won't actually communicate over the port to find out what is really running.

port scanning an IP range in python

So I'm working on a simple port scanner in python for a class (not allowed to use the python-nmap library), and while I can get it to work when passing a single IP address, I can't get it to work using a range of IPs.
This is what I have:
#!/usr/bin/env python
from socket import *
from netaddr import *
# port scanner
def port_scan(port, host)
s = socket(AF_INET, SOCK_STREAM)
try:
s = s.connect((host, port))
print "Port ", port, " is open"
except Exception, e:
pass
# get user input for range in form xxx.xxx.xxx.xxx-xxx.xxx.xxx.xxx and xx-xx
ipStart, ipEnd = raw_input ("Enter IP-IP: ").split("-")
portStart, portEnd = raw_input ("Enter port-port: ").split("-")
# cast port string to int
portStart, portEnd = [int(portStart), int(portEnd)]
# define IP range
iprange = IPRange(ipStart, ipEnd)
# this is where my problem is
for ip in iprange:
host = ip
for port in range(startPort, endPort + 1)
port_scan(port, host)
So when I run the code, after adding print statements below
host = ip
print host # added
and then again after
port_scan(port, host)
print port # added
I end up with the following output:
root#kali:~/Desktop/python# python what.py
Enter IP-IP: 172.16.250.100-172.16.250.104
Enter port-port: 20-22
172.16.250.100
20
21
22
172.16.250.101
20
21
22
...and so on
Thanks in advance everyone!
I appreciate any help that I can get!
code picture for reference, slightly different
output picture for reference
The problem turned out to be an issue with using the netaddr.IPRange, as suggested by #bravosierra99.
Thanks again everyone!

IOS smallSocket and python

I'm working on an IOS app.
I'm starting with a python server on mac that should connect to an iphone and print data sent from iphone.
the connection seems to be established but python print infinite " b " " as data... I don't know why.
the strange thing is that it happens also with cocoaAsynchronousSocket
this is the server
#!/usr/bin/python
import time
import socket
import sys
addr = sys.argv[1]
port = 4444
if not addr :
print ("No host address specified, plese specify an address", files=sys.stderr)
sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
print ("connecting...")
try:
sock.connect ((addr, port))
except socket.error:
print ("unable to connect to", addr)
sys.exit(0)
print ("Connected to", addr)
while 1:
data = sock.recv(0)
data2 = sock.recv(1)
# if not data: break
print (data)
print (data2)
and this is some code that i use to create the connection
- (IBAction)openPressed:(id)sender {
socket = [Socket socket];
[socket listenOnPort:4444];
[socket acceptConnection];
[socket writeString:#"connection accepted"];
}
Why did u add this line:
data = sock.recv(0)
Besides that, your sever, while client might be a better name, seems good.
If it doesn't print what you expect, I suggest that you use some sniffer tools, like wireshark, to check what it really receives.

Categories