-1

What's the equivalent, in Python of:

echo 'blah' | netcat 123.123.123.123 6666

I found this answer but I can imagine there is a simpler way, isn't there one?

Community
  • 1
  • 1
Basj
  • 41,386
  • 99
  • 383
  • 673
  • 1
    Your link is a good way. Send via sockets always need some parameters code line. Netcat is usefull for it, because it does it automatically. But the portion of code isn't so big, or complex...! – Aeldred Apr 05 '16 at 07:12
  • 1
    Possible duplicate of [Netcat implementation in Python](http://stackoverflow.com/questions/1908878/netcat-implementation-in-python) – tobspr Apr 05 '16 at 07:15
  • why can't you use `subprocess.popen` on this command? – venpa Apr 05 '16 at 07:18
  • @tobspr : you didn't totally read my question. I already mentioned your duplicate question in my post. I asked *if there exists another way to do this*. This is not a duplicate. – Basj Apr 05 '16 at 07:30
  • @user3 : it should work on windows too, netcat is not available (except if I install an equivalent). That's why I want a 100% python solution – Basj Apr 05 '16 at 07:31
  • @Basj if so, there is no simpler way, except if you use an external library. You have to setup the socket manually, so the answer mentioned in the linked post is probably the shortest you will find. – tobspr Apr 05 '16 at 07:33

1 Answers1

1

If you only hace to do what you asked, I'd use popen:

import subprocess

test_cmd = 'echo \'blah\' | netcat 123.123.123.123 6666'

process = subprocess.Popen(test_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
err = process.stderr.read()
print(str(err))

It should work, but I didn't test it. Anyway, this shall give you an idea about what you have to do.

My answer works for a unix based distro. If you're looking for a cross-platform solution, your provided link should do the trick.

Another way that I find it useful:

import socket


class Netcat:

    def __init__(self, ip, port):
        self.buff = ""
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.connect((ip, port))

    def read(self, length = 1024):
        """ Read 1024 bytes off the socket """
        return self.socket.recv(length)

    def read_until(self, data):

        """ Read data into the buffer until we have data """
        while not data in self.buff:
            self.buff += self.socket.recv(1024)

        pos = self.buff.find(data)
        rval = self.buff[:pos + len(data)]
        self.buff = self.buff[pos + len(data):]

        return rval

    def write(self, data):
        self.socket.send(data)

    def close(self):
        self.socket.close()

After you created the class, just use it as you need:

from netcat import Netcat

# start a new Netcat() instance
nc = Netcat('127.0.0.1', 53121)

# get to the prompt
nc.read_until('>')

# start a new note
nc.write('new' + '\n')
nc.read_until('>')

# set note 0 with the payload
nc.write('set' + '\n')
nc.read_until('id:')

If you compare your provided link with this solution, you'll see that you have to you sockets, one way or another. So I don't think there's any other simpler way for doing what you need.

  • sorry, but this doesn't work for all platforms (e.g. windows). As mentioned in a previous comment, I'd like a cross platform solution – Basj Apr 05 '16 at 07:33
  • 1
    You didn't specify this in your question. Please add it ^^ –  Apr 05 '16 at 07:33