Start a bash session from python

Question:

I want to drop into a shell for a ctf competition I am working on. I am not allowed to use pwntools for this. I want to achieve something like following from python:

import os
os.system("/bin/bash &")
print("hello world")                 # assume I am writing to a file
os.system("fg")                      # does not work (but assume resuming shell /bin/bash)

I can’t use subprocess since I need to drop into a shell. Not communicate back and forth with the bash process which would run in the background. Is there an easy way to approach this?

Asked By: spunkpike

||

Answers:

You want to use the subprocess module instead. fg is a shell built-in command that only works with job control in the shell itself.

import subprocess


p = subprocess.Popen(["bash"])
print('hello world')
p.wait()
Answered By: chepner
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.