option not getting selected automatically - shell

I am writing a sell script to automate some steps for server , first i have to switch user then server gives me areas to select then i have to select 1 for area and then i have to run a perl program lmsyscon which give me four option then i have to select 3 . then it again gives me 2 option and i have to select 2nd by deafult .here is my script
su poletst -c ". /try/lamtst/.profile Test"
expect -c 'spawn lmsyscon; send 3\r"; interact'
expect -c 'send "1\r"; interact'
the problem is last line is not executing and it is not selecting 2 by default

Hmm... This is too complicated. Besides, your su command has no effect on following expect invocations (those are run as your current user).
Instead, create an expect script that does all 3 steps: changes the user and runs stuff. This is easier.

Related

Bash script to operate another script

I am trying to create a bash script to operate another bash script through CRON without the need for human intervention.
The script needs to be able to interact with the other script so that it accomplishes:
Enter
Press a number..
Then takes you to another section of the script where you need to enter another number..
Then enter another number..
Press enter again..
I can't get the script to hit Enter correctly. What I have so far, "echo | ./module1.sh" flickers, even tried "echo "\n"" which doesn't work.
#!/bin/bash
cd /home/usernamehere/scripts
echo | ./module1.sh
echo "1"
This script requires a person to sit at the terminal while it finishes what it needs to or be run in a tmux session with the user safely exiting the session.
If everything is read from stdin (as opposed to from the terminal device--which is what passwd and screen editors do), and the script requires you to enter ENTER, 1, 2 and 3, you can run it with
printf '\n1\n2\n\3\n' | ./module1.sh
An alternative is with a here-document (read your shell's manual page):
./module1.sh << EOF
1
2
3
EOF

Send keystroke to Dockerfile, Ubuntu

I'm creating Dockerfile script and it has a command line that executes a program and requires user input 1 from keyboard as selected option to go to further steps.
Xdotool, man yes or expect cannot help in this situation.
Update source-code:
First off, download and extract RevoMath library, navigate to RevoMath folder then execute the install script.
...
RUN wget -q https://mran.microsoft.com/install/mro/3.2.4/RevoMath-3.2.4.tar.gz
RUN tar -xzf RevoMath-3.2.4.tar.gz
RUN cd RevoMath/
RUN ./RevoMath.sh
...
Install script has some select options as follow:
echo "1. Install MKL"
echo "2. Uninstall MKL"
echo "3. Exit utility"
We need to enter 1 from keyboard to install. How can we do it via Docker command?
Any help would be appreciated!
If I correctly understand you, you would like to add echo 1 | before ./RevoMath.sh in your Dockerfile:
...
RUN cd RevoMath/ && echo 1 | ./RevoMath.sh
...
BTW: In your example this lines will not work as you expected:
RUN cd RevoMath/
RUN ./RevoMath.sh
Because each RUN is an independent execution.
You should use && if you want to execute RevoMath.sh script from specific folder (see my example in the beginning)
I suggest to use redirect from standard input.
For example install.sh required some input(s) from user at execution time.
Suppose you need to enter 1 as a response to first interaction(questions) and
then you have another response as y for further interaction then it's good to use redirect from stdin.
$#>install.sh <EOF
$#>1
$#>y
$#>EOF
This way whenever script is waiting for inputs it will answer as 1 for the first question and y for the second question.

Shell script to change user, specify some input but then interact with the shell

I am writing script to change user from root to poletst (another user ). When I change user server ask me among three areas from which I have to select by pressing 1,2 or 3
I am writing a shell script to automate some step and there is a step which involve changing user and selecting area 1 by default.
su - poletst
1
It is not working. It takes me to the user but doesn't change the area. How to perform this?
You can do this:
expect -c 'spawn su - poletst; send "1\r"; interact'
Or if it doesn't work, try this:
expect -c 'spawn su - poletst; expect "prompt"; send "1\r"; interact'
Just replace prompt with one of the strings in the last line of the message that is shown to you when you're being asked for the area.
You can as well place it as a script
#!/usr/bin/env expect -f
spawn su - poletst
expect "prompt"
send "1\r"
interact
Save it to a file like su-poletst.exp and just run expect -f su-poletst.exp.

Preparation of shell script

Recently I have been involved for the preparation of the shell script on SunOS with csh shell. I will have multiple queries but first the short program isn't working.
[username]% expect - << EOF
Spawn telnet 74.125.71.103
expect "Password:"
send "google\r"
EOF
The following error pops up:
/bin/csh: Event not found
[username]% expect: Command not found
Please advise.
This script should run in following manner :
Telent the IP and use the existing passwd (explicitly given in the script).
After the telnet, it shows MENU options
MB station
RC
ODU
AP
SU
Exit
type 1 // a "MB station" MENU options will open i.e.
1 - Show
2 - Unit Control
type 2 // UC MENU options will open i.e
1 - Change Password
2 - Reset
type 1 //change passwd MENU options will open i.e.
1 - Change PC Password
2 - Change LU Password
3 - Change Admin Password
type 3 // to change ADMIN passwd
MB station - Change Admin Password
Enter New Password : XYZ enter
Re-enter Password : XYZ enter
New password accepted
3 times escape // to escape from telnet
1.MB station
2. RC
3. ODU
4. AP
5. SU
6. Exit
type 6 // to exit
Exit? [Y/N] y
Connection to host lost.
then move to step with different IP. The IP values will be given by the user one-time while executing the script at the prompt e.g. ./pass-change IPs.txt
The Event not found message implies you're trying to do some kind of history substitution. This normally involves the ! character. Did you type something with a ! character in it at some point?
And expect: Command not found means just what it says: the shell wasn't able to find the expect command. Is it installed? If not, you should install it if you can, or ask a system administrator to install it for you, or, failing that, obtain the source and build and install it under your home directory.
Once you fix that, there's an expect command called spawn, and it's case-sensitive; Spawn won't be recognized.
If you're specifically asking how to accomplish the above without using expect, please update your question to make that clear.
I have a working script below, you are getting error because i believe you are trying to run the script using some sh or other command. Try to run it as ./script. I used below sample script for file transfer but i did teh key exchange manyally by ssh back and forth which is one time activity.
#!/usr/bin/expect -f
set timeout 130
spawn ssh "idk#server.com"
expect "password: "
send "pass#1234\r"
expect "$ "
send "sh /home/nathalok/HTML/run.sh\r"
expect "$ "
send "exit\r"

How to make a cronjobbable script to clock uploads?

I asked a similar question over here:(Upload clocking script), but then changed it and realized it's a different question now.
I want to make a script called uploadtimer.sh which uploads files to various hosts and then notes the time the upload takes in a log file. Thanks to those who have helped me, I have a partial solution but I'm stuck:
I used this as a crontab (testing it for the first ten minutes after 11 am)
1,2,3,4,5,6,7,8,9,10 11 * * * /usr/bin/time -a /usr/local/apache/sites/$MYEMPLOYER/upload_test/output.log /usr/local/apache/sites/$MYEMPLOYER/upload_test/uploadtimer.sh
upload_test/ has these files in it:
upload_test/output.log
upload_test/uploadtest.gif
upload_test/uploadtimer.sh
output.log is a text file that I renamed as a .log file. It's blank. Before I put it there, I was getting messages that said "this file doesn't exist". I thought it'd create one for me but I guess not. After I created it, it kept saying in the crontab mails that I couldn't write to it until I changed its permissions, so I did.
uploadtest.gif is a gif
uploadtimer.sh is this:
#!/bin/sh
HOST='--myhost--'
USER='--login--'
PASSWD='--password--'
FILE='uploadtest.gif'
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0
The message that cron sends to me is:
0.00user 0.00system 0:00.02elapsed 0%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+253minor)pagefaults 0swaps
But no ftp has taken place and nothing it written in output.log.
Create another script that runs the second command. Call the second script from cron.
You will need to make sure you are using absolute paths for everthing because cron starts in a different directory than the script.
Your questions:
Where do I put this part, which tells it to save the data to a log?
You edit your crontab file to add a cron entry. The cron entry contains information regarding what command to run and when to run it. For example, here is a cron entry that you might use:
0 2 * * * /usr/bin/time -a ~/output.log ~/uploadtimer.sh
This entry says to run the command at 2AM every day (minute 0, hour 2, every day of the month, every month, every day of the week). For more info on the crontab syntax, you can check out http://www.adminschoice.com/docs/crontab.htm .
To make a cron entry, use the command crontab -e which will load up a vi text editor. Enter the line above and then save it. This creates the cron job. If you don't know vi, just follow this:
from the shell, enter crontab -e to start editing
press i to enter Insert mode
enter the line as above
press ESC to exit Insert mode
press : to switch to Command mode - you will see the cursor at the bottom of the screen
enter wq and hit Enter - this Writes and Quits the file. You should see a message like crontab: installing new crontab.
If you run into any problems using vi, use ESC a few times to exit whatever mode it's in, do : to enter Command mode, and then q! to force a quit (i.e., quit without saving anything), and then try again from the beginning.
It refers to the script itself so does it need to be separate from the script?
Yes.
You might want to check out "ncftp". It would condense your upload command into "ncftpput -u $USER -p $PASSWD $HOST / $FILE", and then you wouldn't need the script at all, just put the whole command into your crontab:
0 2 * * * /usr/bin/time -a ~/output.log ncftpput -u $USER -p $PASSWD $HOST / $FILE

Resources