#!/usr/bin/env python
##
### nc-lp.py - listen for a tcp connection on a certain port
##
#
import sys,socket,re
def usage():
print("""\n nc-lp.py - listen for incoming TCP connection
usage: python nc-lp.py <port>""")
def listen(lport):
lsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
lsocket.bind(("", int(lport)))
lsocket.listen(1)
print("[+] Listening on port " + lport)
connection, address = lsocket.accept()
print("[*] Connection from " + str(address[0]))
while True:
try:
incoming = connection.recv(2048)
print(str(incoming))
outgoing = raw_input("@" + str(address[0]) + ">")
while outgoing == "":
outgoing = raw_input("@" + str(address[0]) + ">")
if outgoing == "exit":
print("[!] Exiting..")
exit()
connection.send(outgoing)
except socket.error:
print("[!] Error: Connection lost")
exit()
except socket.timeout:
print("[!] Error: Connection timed out")
exit()
def main():
if len(sys.argv) < 2:
usage()
exit()
search = re.compile("([0-9]{1,5})")
lport = sys.argv[1]
if search.match(lport):
try:
listen(lport)
except OverflowError:
print("[!] Error: port must be between 1-65535")
else:
print("[!] Error: port must be between 1-65535")
if __name__ == "__main__":
main()