How to write shell script which handles interactive inputs? - shell

I have a command which takes 2 arguments.
When I run the command manually, I do this way:
cmd -i xyz.dat
hit enter
enter password in the prompt
hit enter
confirm password
My script needs to do the above operations without expecting user to enter password. I can hardcode the passwords in the file, but need a way to run this command successfully when I execute the shell script.
As on Feb 7th,
I have expect installed on my AIX. When I type expect at the command prompt, the prompt changes to expect 1.1> OS is 64 bit AIX
I have followed the instructions mentioned in the below comment, but I keep getting error - could not execute the command; no such file or directory"? I am able to manually run this command from same directory I am running the script. Besides that I have given the complete path of the command and the
file.
I am pasting another program I tried to su with root password as below: i get the same error message when I run the test program. I doubt if this is something related to quotes.
#!/bin/bash
set timeout 20
spawn "su"
expect "Password:" { send:"temp123\r" }
interact
Can someone please help me fix this error?

Sounds like you want to use expect. Here is a page with some examples.
So for your command you would want something like:
#!/usr/bin/expect
set timeout 20
spawn "cmd -i xyz.dat"
expect "<your password prompt" { send "<your password>\r" }
expect "<your password confirmation prompt" { send "<your password>\r" }
interact

Related

Sending password with expect in shell script

I have a small problem. I'm trying to scp to a remote server and have the script enter my password for me. In short, I'm trying to automate the password entry process in the prompt. However, every time I run my code I keep receiving the prompt to enter my password. Please help. Thank you.
#!bin/expect
for dest in $(<destinations.txt); do
set pass "password"
scp -r ${dest}:/home/car02fv/QATest.txt /home/car02fv/
expect "car02fv#goxsd1703's password:"
send pass
interact
done

Running shell script as admin using expect on OS X

I am trying to let non-admin users run a script in a shared environment that requires root privileges. I was going to build either an AppleScript or Automator application that invokes expect in order to pass the login credentials (yes, I know the dangers, it will all be execute only). However I get the login to work, but the shell script I am trying to run doesn't seem to be executing, since it returns instantly. I am new to expect and I believe I have an error in my script. Ideas anyone?
#!/usr/bin/expect
set timeout 60
set password "MyPass"
set command "bash /Applications/mampstack-5.4.36-0/ctlscript.sh start"
eval spawn login admin
expect "assword:"
send "$password\n"
expect "$"
send "$command\n"
expect "$"
send exit
expect "$"
send exit
Don't do it that way. Instead, edit /etc/sudoers to add the following lines (or, on a default config of MacOS, you can create a file in /etc/sudoers.d with the following content):
# allow use by GUI apps (AppleScript, Automator), which don't have a TTY
Defaults:%staff !requiretty
# on MacOS, all humans are members of the staff group
%staff ALL=(ALL) NOPASSWD: /Applications/mampstack-5.4.36-0/ctlscript.sh start
...then, have your AppleScript or Automator code run:
sudo -u admin /Applications/mampstack-5.4.36-0/ctlscript.sh start
...which will require no password at all.

BASH scripting for username/password constructs

I want to write a simple bash script using ncat to open a connection to a ISP and its port.
The first command would be:
nc address port
Upon doing this, I am prompted first to provide a username. I must hit ENTER, and then I will be prompted to provide a password and then I must hit ENTER again.
After this, I want to open a Terminal process window. Can anyone point me to sufficient resources for this type of scripting?
I know the username and password already, but I'm not too sure how to work around the fact that I must provide it and then hit enter. I'm also unsure how to open a new Terminal proceses.
Thanks in advance!
Check out expect script
Expect
Example:
# Assume $remote_server, $my_user_id, $my_password, and $my_command were read in earlier
# in the script.
# Open a telnet session to a remote server, and wait for a username prompt.
spawn telnet $remote_server
expect "username:"
# Send the username, and then wait for a password prompt.
send "$my_user_id\r"
expect "password:"
# Send the password, and then wait for a shell prompt.
send "$my_password\r"
expect "%"
# Send the prebuilt command, and then wait for another shell prompt.
send "$my_command\r"
expect "%"
# Capture the results of the command into a variable. This can be displayed, or written to disk.
set results $expect_out(buffer)
# Exit the telnet session, and wait for a special end-of-file character.
send "exit\r"
expect eof
The secret lies in the HEREDOC
You can solve this problem with something akin to:
$ command-that-needs-input <<EOF
authenticate here
issue a command
issue another command
EOF
Look at the link I provided for here documents - it includes support for variable substitution and lots of other useful things. Enjoy!

Writing strings to standard input in bash script

I'm trying to automate a startup of a specific service with bash
When the service is started with init.d (/etc/init.d/openvpn.custom) it is promting for username and then password - and then it connects
The auth-user-pass from-file is not possible with the installed version, and it cannot be upgraded because of dependencies
So i'm trying to write a simple bash scripts that executes the init.d script, sleeps for a bit, inputs the username, returns, sleeping a bit, inputting the password - you'll get the flow.
like http://pastebin.com/qWHX7Di5
I've experimented with echo, but it doesent seem to work
This is for a rather legacy firewall i'm asked to keep connected.
Is this even possible?
I would use expect instead of bash. You can still call it from within bash if you need to do other tasks as well.
In expect, the script would be something like the following (untested):
#!/usr/bin/expect -f
set username "username"
set password "password"
spawn /etc/init.d/openvpn.custom start
expect "Username:"
send "$username\r"
expect "Password:"
send "$password\r"
expect eof
You'd want to change the expect "Username:" & expect "Password:" lines to match the actual login prompts that are output by your init.d script.
See the expect man page for further details.
You can try using a here-doc:
/path/to/init.d << END
$username
$password
END

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"

Resources