Pressing ENTER in one console from another one - linux

How can I acknowledge the reading of an active std:cin call of a running C++ program from within another console?
Please: This IS NO DUPLICATE of e.g. Simulating ENTER keypress in bash script, because I don't want to input text to the program itself but to the std:cin that is running within a loop of that program.
So, the running program waits in the console by saying "Enter option:". Manually I would enter something and then press enter.
Entering text via
echo "some text" > /proc/18553/fd/0
works but I cannot redirect the enter-key to enter/end the text as I can by physically hitting the enter key.
This fellow here has or had the same problem:
Send a string and Enter key to a Node.js script [closed]

Related

Linux keyboard/terminal/kernel input buffer

I want to know where exactly (terminal, keyboard driver, libc, linux kernel, ....?) is stored the text which the user enters in a console application before pressing enter key.
So the use case is like that:
The user opens a terminal or runs any other console program.
The above program invokes some library call to read input from STDIN like gets, scanf functions (ultimately invokes read system call).
The user enters some text and ultimately presses enter.
The above program gets the text into the buffer.
Screenshot of this scenario until pressing enter key is given in the following screenshot 1
I'd like to know:
Where the text is stored before the user presses enter (keyboard driver, kernel, terminal, etc.?).
How long is that buffer (I assume it is a buffer).

Is there a way to replicate pressing the enter key in one terminal from another?

I have a script that runs a program that generates an email and then waits for the user to input information from that email. Because of this, the information is not available to pipe to the program on execution. It has to be done after it's running already.
I've tried a number of the different method suggestions, redirecting to the terminal (echo or cat > /dev/pts/#), mkfifo, expect, etc. Most of these methods are successful in actually sending the information to the target terminal - I actually see the information show up there. But there doesn't seem to be a way to send the equivalent of someone pressing the enter key.
Newline (using echo -en) or \n inside of the information send actually makes the cursor move in the target terminal - but it's not the same as actually pressing enter. It just scrolls the screen down one line. If I select the terminal window and press enter, it then accepts the input.
How can I do this from a different terminal (or can I?)
Or better yet, is there a way from the same script that's launched the program now waiting for input to send the program input?
Most of what I've seen has gone the route of using another terminal to try to send the information to the program.
Flow:
Simple similar example is if you were running a shell script that during its execution, runs a program that generates a PIN sent to your email, and then waits for the PIN to be entered before continuing.
In terminal 1, a bash script is run - one of the steps this script does is launch a program that generates an email and then sits at a prompt, waiting for the user to provide input (data from the email sent).
In terminal 2 (or if you can do this through terminal 1 somehow), take the value from the email and input it to the program in terminal 1, including pressing the enter key, so the program sees this value as actually entered and the program can complete its execution.
script in terminal 1:
...
if [ -f $progamToRun ]; then
$programToRun # This program sends the email and prompts the user for information from the email. It waits here...
# once information from email is entered, continue executing commands here
fi
script in terminal 2:
...
# monitor email
# retrieve information from email
# send information to program in terminal 1 that is waiting for this input
# exit script
The steps for the script in terminal 2 are working - and all of the methods I've tried to send the information to the program in terminal 1 haven't worked - that is to say, even if the information appears in the terminal through STDIN, etc. the application running doesn't recognize it as input so it's still sitting in the wait state.
I attempted to use netcat as suggested, but that's failing differently.
In terminal 1 I ran:
cat /tmp/nctest | /tmp/docl.sh -i 2>&1 | nc -l 127.0.0.1 8080 > /tmp/nctest
docl.sh contains one line, the application I am running to generate the email and wait.
In terminal 2 I ran:
nc 127.0.0.1 8080 <information from email>
When I run this in terminal 2, I immediately get the error from the program run in the script in terminal 1 that it failed. I tried this:
nc 127.0.0.1 8080
by itself to see if there was an issue passing the information on the same line I run nc, but get the same result. It's like the first terminal's nc command actually runs the application and then somehow bypasses or processes the prompt, so it's gone before I can enter the information from the email.
I also tried the command in terminal 1 without the -i and without the 2>&1, but there was no difference in the result.

pywinauto (or others): to emulate Ctrl+A & Ctrl+X anywhere (not only on specified app)

I'm looking for a way to generate a ctrl+A (select all), and then ctrl+x in a python script. I know how to generate this in a specific app (pywinauto and other modules do that). But I'm looking for a way to send these keys in any apps (in any field of the active windows). I want to launch the python script containing these keys anywhere (the script will be launch using a key shortcut. Details below (1))
EDIT: I'm NOT trying to copy/past in the command windows (cf. the 2 last sentences). My script send the keys in the command windows, but that's the problem I'm trying to solve...
Using python pywinauto (or Ctypes or other modules)
I tried several propositions listed here with the same result.
I thought pywinauto could do it. Following pywinauto latest documentation I tried that:
open an (any) app containing a text field (that's the active windows)
place the cursor where you want to make the select all + cut/past
run the script bellow using an shortcut (so you won't leave the active windows)
from pywinauto.keyboard import SendKeys
SendKeys('^a^x')
Result
The code only print ^A^X in the python console. It doesn't do what it's suppose to do in the field of the active window (where I placed my cursor): it doesn't select all + cut the text.
Using autohotkey:
The only way I found to simulate a real crtl+A ctrl+C is by using autohotkey (it works but it's not a python solution):
save the code bellow in my AHK script: select_copy.ahk
Send, ^a
Send, ^x
create another AHK script called shortcut.ahk where you will specify a shortcut to launch select_copy.ahk (shortcut.ahk sould run constantly in windows background (2))
!^+G:: Run select_copy.ahk , C:\Users\Me\Desktop
(meaning: when I hit ctrl+alt+shift+G run the script select_copy.ahk)
result:
It works. when I call the ahk, it select/cut things in the active windows.
A combination of both did not work
I tried to launch the select_copy.ahk from within a python script (using subprocess.call) but I ended up with the same result than pywinauto (or Ctypes): it only prints ^A^X in the consol, but doesnt select&cut. So I'm wondering if python could really do what autohotkey does.
(1) What the script will do: I will launch the script (using a shortcut key) on one or another html editor, it will cut all the text, parse its source code, make some change put back the datas in the clipbboard, and past it. I'm only missing the first part (select all + cut) and the last part (past).
(2) It's not the big deal since shortcut.ahk contains also all my other ahk shortcuts and scripts.
Your AutoHotKey script should work, and does on my machine. However, I recommend that you just have one shortcut.ahk file containing the following:
!^+G::
Send, ^a
Send, ^x
Return
...and then put this in your python file:
subprocess.call("C:\\Path\\To\\AutoHotKey.exe /r C:\\Path\\To\\shortcut.ahk")
replacing the paths with wherever the AutoHotKey executable is, and wherever the shortcut.ahk file is.
Just as a side note: !^+G:: triggers on Alt+Shift+Ctrl+G, not Shift+Ctrl+G as you wrote in your question:
(meaning: when I hit ctrl+shift+G run the script select_copy.ahk)
EDIT: Also, from the phrase in the python console in your question it seems like you're trying to select all and then cut it in CMD. This will not work at all. Instead, if you want to simply clear the console, just use the command cls (Windows only; use clear in Linux). If you want to copy the entire console output and then clear it (i.e. cut) you're gonna need something different.

BASH background process printings disappearing the prompt line

I have a BASH script that runs on bg and print messages to the user when according to system events.
My problem is that after the echo of these messages, the user need to press on the ENTER key in order to get back to the prompt line.
Adding new line to the printed messages didn't help since it still comes from the bg and not from the user shell.
Does anyone have an elegant & simple solution to get the user back to the prompt line?
I will appreciate any help here..
You don't need to press enter to get to a prompt. You are still at the prompt that was there before the output was printed. Try just entering a command and hitting enter. (You can also hit ctrl-l to clear the screen instead of hitting enter to input an empty command.)
The problem here is that the background/alternative process has no relationship to the running shell session and so it is simply writing output to wherever the terminal sticks it. The process might be able to use control sequences to control the output location (but I don't know if this actually works).
Other than that there isn't much to be done about this that I'm aware of. And it isn't a problem in any real way.

Provide option input of one script from another script

when login using putty autologin script runs and ask for option to select. Is it possible to provide input to that via another batch file / script?
example: when i login to say 1.1.1.10 using batch file it will popup window which is asking for input option number from 1 to 10. i want to input that option automatically.
Since you mentioned putty, I am assuming you are working on a windows workstation. And I am not sure if you are familiar with autohotkey but, you can do this pretty easily using a autohotkey code snippet. Something like this:
^+P:: ; shift-ctrl-P is the hotkey combo to activate this code
send #r ;open windows run dialog box
send \path\to\putty.exe hostname.domain.com{enter} ; stary putty and connect to server
sleep 5000 ; wait 5 seconds for connection to establish
send username{enter} ; types your username and hits enter
sleep 1000 ; wait 1 second for password prompt
send password{enter} ; types your password and hits enter
sleep 5000 ; wait 5 seconds for your autologin script to run and display menu
send 1{enter} ; type your selection and hit enter (assumes your selection is "1" here
return ; ends code snippet
You can change the keystrokes and wait periods according to you particular situation.
autohotkey is a free software (for individual use) and can be obtained from http://www.autohotkey.com

Resources