Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Last active December 6, 2022 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hermann-SW/65e07365cc2586b76afb6274ece12d5b to your computer and use it in GitHub Desktop.
Save Hermann-SW/65e07365cc2586b76afb6274ece12d5b to your computer and use it in GitHub Desktop.
Remote control bash from Python
from subprocess import Popen, PIPE
from sys import argv
import re
stop = "qwertz1234567890ufugUUGUGUgUgUGuGFzR775§%!=%%54321rUF/Rtt8t8TTT4§2hj\n"
bye = ["tschüs","do_widzenia","ahoj","ciao","salut","adieu","vaarwel","farvel"]
byes = re.sub(r"['[]]*", "", str(bye))
cmd = "bash" if len(argv) == 1 else argv[1]
p = Popen(cmd, stdin=PIPE, stdout=PIPE)
while True:
i = input(cmd + ": ")
if i in bye: break
if i == "?":
print("'cmd' gets executed\n'?' for this help\nto exit, any of: "+byes)
continue
p.stdin.write(bytes(i + "\necho " + stop, 'utf-8')); p.stdin.flush()
while True:
resp = p.stdout.readline().decode(encoding="utf-8")
if resp == stop: break
print(resp, end="")
@Hermann-SW
Copy link
Author

Hermann-SW commented Dec 6, 2022

If responses are known to be 1 line always, much simpler bash1.py can be used:
https://gist.github.com/Hermann-SW/4c5b204f2eecf712b568bf85ee111a2b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment