pyrasite-shell
- Give it a pid, get a shell¶
You can easily drop into a shell and execute commands in a running process
using the pyrasite-shell
.
$ pyrasite-shell
Usage: pyrasite-shell <PID>
$ pyrasite-shell $(pgrep -f "ipython")
Pyrasite Shell 2.0beta9
Connected to 'ipython'
Python 2.7.2 (default, Oct 27 2011, 01:40:22)
[GCC 4.6.1 20111003 (Red Hat 4.6.1-10)] on linux2
>>> print(x)
foo
>>> globals()['x'] = 'bar'
Source¶
import sys
import pyrasite
def shell():
"""Open a Python shell in a running process"""
if not len(sys.argv) == 2:
print("Usage: pyrasite-shell <PID>")
sys.exit(1)
ipc = pyrasite.PyrasiteIPC(int(sys.argv[1]), 'ReversePythonShell')
ipc.connect()
print("Pyrasite Shell %s" % pyrasite.__version__)
print("Connected to '%s'" % ipc.title)
prompt, payload = ipc.recv().split('\n', 1)
print(payload)
try:
import readline
except ImportError:
pass
# py3k compat
try:
input_ = raw_input
except NameError:
input_ = input
try:
while True:
try:
input_line = input_(prompt)
except EOFError:
input_line = 'exit()'
print('')
except KeyboardInterrupt:
input_line = 'None'
print('')
ipc.send(input_line)
payload = ipc.recv()
if payload is None:
break
prompt, payload = payload.split('\n', 1)
if payload != '':
print(payload)
except:
print('')
raise
ipc.close()
if __name__ == '__main__':
shell()