0

Say I do nc example.com 1234 and it returns two numbers split by a newline, which I must add together and input back in to it. The numbers change if I close the connection, so how would I get the output of netcat, do math on it and input it again all in one connection?

0

For anyone else with the same issue, you would probably be a lot better off using python sockets.

Some example code that would solve the issue in this question:

import socket

#AF_INET for IPv4, SOCK_STREAM for TCP (as opposed to UDP).
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Tell the socket what IP and port number to connect to (must be in two brackets because it needs a tuple).
clientsocket.connect(('example.com', 1234))

#Recieve 1024 bytes of data.
data = clientsocket.recv(1024)

#Split the recieved data by newlines (returns a list).
data = data.split('\n')

#The first and second elements in our list should be the two numbers we need to add together, so we do that.
result = int(data[0]) + int(data[1])

#Send our result to the server.
clientsocket.send(str(result))

#Recieve any response from the server and print it to the screen.
data = clientsocket.recv(1024)
print data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy