1

After the subprocess command is executed, the cmd window asks for the enter key to be pressed. I would like the python script to continue and automatically press enter. my current code is as follows:

import subprocess

import pynput

from pynput.keyboard import Key, Controller

Keyboard = Controller()

subprocess.call("plink -i /Users/joshs/OneDrive/Documents/Downloads/newkey.ppk kali@xx.xxx.xxx.xx", shell=True)

Keyboard.press(Key.Return)

Keyboard.release(Key.Return)
Axe319
  • 4,255
  • 3
  • 15
  • 31
Smudger67
  • 11
  • 1
  • 1
    Are you sure it ends? Did you place a `print()` after the `subprocess.call()` to determine that? – Axe319 Jan 28 '22 at 17:29
  • Do you need to use `shell=True`? – azelcer Jan 28 '22 at 17:30
  • 1
    Do you mean you want to feed input into the `plink` subprocess? Your current code waits for it to exit, then continues pressing keys locally. What you want would be more suitable for Paramiko or `pexpect`. Tangentially perhaps see also https://stackoverflow.com/questions/37586811/pass-commands-as-input-to-another-command-su-ssh-sh-etc – tripleee Jan 28 '22 at 17:31
  • @axe319 I have tried a print at the end and it is the same result – Smudger67 Jan 28 '22 at 18:42
  • @azelcer I believe shell=True is needed for the plink command to work https://docs.python.org/3/library/subprocess.html – Smudger67 Jan 28 '22 at 18:46
  • @tripleee thank you i will look into this tomorrow! – Smudger67 Jan 28 '22 at 18:47
  • Try `subprocess.run(['plink', '-i', '/Users/joshs/OneDrive/Documents/Downloads/newkey.ppk', 'kali@xx.xxx.xxx.xx'])`, without `shell=True` If plink always waits for a key, you can try passing ìnput="\n"`. – azelcer Jan 28 '22 at 19:03
  • `plink` definitely does not need a shell. No external command does. You need a shell if your command line uses shell features (wildcard expansion, redirection, builtins like `cd`, etc). See also [Actual meaning of `shell=True` in `subprocess`](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) On Windows (which I guess is implied by `plink`) you can pass the command as a single string even without `shell=True` but this is not properly portable to other platforms. – tripleee Jan 28 '22 at 20:19
  • @triplee thank you for clarifying that! – Smudger67 Jan 28 '22 at 21:47
  • @azelcer Thank you, I will try that tomorrow and get back to you – Smudger67 Jan 28 '22 at 21:47

1 Answers1

0

I have found a work-around where I can pass commands at the end of the plink command. Pexpect does not work with Windows and I had endless dramas with the windows version. As you can tell from the code below, the final program is to open VNC viewer, automatically establish connection with my AWS EC2 instance and make sure the environment is still suitable for my needs (as the instance sometimes requires tightvnc to be reinitialised).

import subprocess

subprocess.call('C:/Users/joshs/OneDrive/Documents/VNCViewer')
subprocess.call("plink -i /Users/joshs/OneDrive/Documents/Downloads/newkey.ppk kali@xx.xxx.xxx.xx sudo apt install xfce4 xfce4-goodies -y tightvncserver; sudo apt-get install gnome-core kali-defaults kali-root-login desktop-base -y ; tightvncserver -geometry 1920x1080; sudo gunzip /usr/share/wordlists/rockyou.txt.gz")
subprocess.call("plink -L 5901:localhost:5901 -N -i /Users/joshs/OneDrive/Documents/Downloads/newkey.ppk kali@xx.xxx.xxx.xx")
subprocess.call(['C:\Program Files\RealVNC\VNC Viewer'])
tripleee
  • 175,061
  • 34
  • 275
  • 318
Smudger67
  • 11
  • 1