Bob Ebert
Bob Ebert

Reputation: 1401

socket can't communicate with netcat bash

On a vm I used the command: nc -l -p 8221 -e /bin/bash and made a python3 script:

def netcat():
    print ("starting connection")    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("192.168.1.60", 8221))
    while True:
        user = input("what to send?: ")
        s.sendall(bytes(user, "utf-8"))
        time.sleep(5)
        word = "bob"
        data = s.recv(4096)
        if data == b"":
            pass
        else:
            data = data.decode("utf-8")
            print ("Received:", repr(data))
    print ("Connection closed.")
    s.shutdown(socket.SHUT_WR)
    s.close()
netcat()

this script doesn't work. By don't work I mean when I run a command with my python script, lets say "pwd", it just loads but never runs. When, instead of running the python script I would run nc 192.168.1.60 8221, it would work fine. Any ideas why?

Upvotes: 2

Views: 1251

Answers (1)

a3f
a3f

Reputation: 8657

From input()'s documentation:

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

But Bash is operating in canonical mode and won't process input till a new line arrives. This won't happen, leading to recv blocking forever.

add a + '\n' after the user = input("what to send?: ") to fix it.

Upvotes: 2

Related Questions