Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Netcat (NC) is a wonderful little program that system security analysts hate. Yo

ID: 3730373 • Letter: N

Question

Netcat (NC) is a wonderful little program that system security analysts hate. You're going Write a single program, in python, that: 1. Listener When invoked with a '-L' lhg the program listens on the user supplied host, to write a large subset of its functionality in this assignment. port, and protocol. For exampl: NC -L 127.0.0.1 8000 TCP would listen on 127.0.0.1 (local host) port 8000 for TCP packets. Have the program write its output to stdout. (remember to import sys) 2. Talker When invoked with a -S' flag the program echoes stdin to the user supplied host, port, and protocol. For example: NC -S 127.0.0.1 8000 UDP would emit UDP packets on 127.0.0.1 (local host) port 8000. The full version of Netcat can read from one (ip,port) and ocho to another, but that will be a tad difficult for you to debug. It's a fairly trivial generalization of the program you write for this assigument.

Explanation / Answer

Here is a single python program for both Listener and Talker.

import socket             
import sys

#reading commandline arguments
option   = sys.argv[1]
ip   = sys.argv[2]
port   = sys.argv[3]
prot   = sys.argv[4]

#Listener part to listen on the user supplied host, port and protocol
if option == "-L":
   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   s.bind((ip, int(port)))
   s.listen(2)

   while True:
       c, addr = s.accept()   
       print c.recv(1024)

#Talker part to emit the user supplied host, port and protocol
if option == "-S":
   c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   c.connect((ip, int(port)))
   c.send("ip = " + ip)
   c.send(" port = " + port)
   c.send(" protocol = " + prot)