4

I understand, we can execute Linux Shell Commands using subprocess

  import subprocess
  subprocess.call(["ls", "-l"])

What if i want to run CTRL+C action over the terminal?

My Use Case is:

   1> Open a linux screen
   2> Run a command over first window
   3> Then create a window in the same screen
   4> Run another command over the second window

It is pretty obvious i want to automate some part of my daily routine.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
innosam
  • 427
  • 1
  • 5
  • 18
  • 1
    I don't know if this can be done through a shell script, you're probably better off finding the module in python that you can use to invoke keystrokes, then you'll invoke the value '^c' where ^=CTRL, %=ALT and I haven't done it in a long time, but there are special characters for the other special keys also – pythonian29033 Aug 07 '13 at 10:48
  • You may have a look at [os.kill](http://docs.python.org/3/library/os.html) and [signals](http://docs.python.org/3/library/signal.html) in Python. Finding the pid is another matter, but anyway, you need a way to identify the command you want to close. –  Aug 07 '13 at 10:51

1 Answers1

0

Aah! found a solution for you;

if you use ubuntu or backtrack (a debian based linux flavour) then you can install this: apt-get install xautomation

and invoking keystrokes with this is a little easier, for people who like coding in English, but slightly more complicated:

from subprocess import Popen, PIPE

control_f4_sequence = '''keydown Control_L
key F4
keyup Control_L
'''

shift_a_sequence = '''keydown Shift_L
key A
keyup Shift_L
'''

def keypress(sequence):
    p = Popen(['xte'], stdin=PIPE)
    p.communicate(input=sequence)

keypress(shift_a_sequence)
keypress(control_f4_sequence)
pythonian29033
  • 5,148
  • 5
  • 31
  • 56
  • 1
    This seems doable. And specifically to achieve my usecase, of creating a screen and passing commands to it. For example we want to mimick pressing Ctrl+C in the xc session, we can run screen -x xc -X eval "stuff \\003". Typing a word followed by the enter key would be screen -X -S xc eval "stuff Hello\\012". This worked brilliantly to automate my task. [link] (http://www.gnu.org/software/screen/manual/screen.html#Paste) – innosam Aug 07 '13 at 17:14
  • yea, me and my uni buddies made people super scared scheduling scripts that would just do random thing, people would go on in the communal computer labs and scream, "someone's hacking into this computer!" lmfu – pythonian29033 Aug 07 '13 at 20:07