Provide option input of one script from another script - linux

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

Related

Pressing ENTER in one console from another one

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]

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.

Automate installation of binary in linux

I have a Bourne-Again shell script text executable named engine.bin that I want to install.
If I install the executable manually ./engine.bin I get a screen with the EULA I have to accept (by pushing space), then accept it by writing yes and then enter the installation path by typing /usr/local/engine.
Now I want to do the installation automatically through provisioning scripts without manual interaction. Is there a way to do this? I do not know if the installer accepts any parameters, unfortunately the thing is undocumented.
Based on the suggestion of bill-agee and jgr208 I wrote the following which is working for me:
#!/usr/bin/expect -f
set timeout -1
spawn /tmp/engine.bin
expect {
-gl "*Press SPACE or PAGE DOWN key to continue, U or PAGE UP key to scroll back*" { send -- " "; exp_continue }
-gl "*yes/no*"
}
send -- "yes\r"
expect -gl "*press ENTER to accept the default*"
send -- "/tmp/tce\r"
expect eof
If the executable allows you to spam input at it without waiting for each separate prompt to appear, you might be able to accomplish this with bash.
For example, this script will run program_that_takes_several_lines_of_input.py and send it four lines of input - three with text and one blank line:
#!/bin/bash -eux
./program_that_takes_several_lines_of_input.py <<EOD
first line
one enter keypress later
yet another line of input after the empty line above
EOD
If you need to stop and wait for each prompt to appear, the cram Python package may be a good fit for this scenario - I find it useful for tasks like this where you only need to send a few lines of input, but each line of input is different.
See:
https://bitheap.org/cram/
https://pypi.python.org/pypi/cram
Expect would also work, but I find that I reach working solutions a bit faster when using cram than with Expect.
pexpect is a great choice as well! See:
https://pexpect.readthedocs.org/en/stable/

Refresh browser using bash script

The goal is to create a bash script to refresh a webpage every five seconds and I've been struggling to do it. I know crontab would probably work butI'd rather just do it as a bash script...
So I have this code right here:
#!/bin/bash
echo "This script will reload/refresh a webpage every 5 seconds"
while true;
do
wget http://website.com/ >/dev/null
sleep 2;
done
and it works for the most part. It downloads the webpage but doesn't refresh it like I want it to. I've tried adding an '-e' after 'wget' but it says its an invalid command. I've also used 'curl' instead of 'wget' and I like how 'curl' shows feedback rather than also downloading but again, it doesn't refresh the page...
I've also tried something along the lines of this:
#!/bin/bash
while true;
do
iceweasel -remote "openURL(website.com)"
sleep 10;
done
It does its job and opens the webpage I want, but it keeps adding tab after tab every 10 seconds. I tried to use the killall command to close the tab before it goes through the loop again, that way it would be something similar what I'm going for but it doesn't work either.
It's probably something very simple but I just really don't know what I'm doing wrong.
If you don't mind having a script that presses F5 or ctrl+r automatically for you at a gui web browser, then you can do what sjsam suggested and use xdotool.
(Make sure you install xdotool)
while true ; do
sleep 2
xdotool key ctrl+r
done

how to get terminal properties

I very frequently work on multiple items in parallel and end up running some long tests or regression after working on something. I usually add a mail -s "foo" id < /dev/null at the end to know when a task ends so that I don't have to baby sit a long test or regression.
I was trying to automate this, where in I don't have to type the mail every time, just call a script (alias this to fewer characters - optimizing on how much I type) and the script figures out the test/regression from the cwd and sends an email. I thought it would be useful to send the terminal title or the screen session name in the email. Is there a way I can extract the terminal title or Linux screen window name?
You can find the screen session name, when attached, in $STY (the window number is available in $WINDOW if you need it):
$ echo "$STY"
6367.sessionname
You can find the uniquely identifying tty/pty device with tty:
$ tty
/dev/pts/34
Titles and such are features of xterm and not of the terminal itself, so terminals programs have a hard time deducing it.
Here's an example using both of the above to show the screen name if any, or the tty device otherwise:
mail -s "${STY:-$(tty)} done" < /dev/null
Screen also has a "wait for silence" feature where you can get a notification when processes in other screen windows have stopped outputting.

Resources