Sending password with expect in shell script - shell

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

Related

How to pass username/password into ambari-server sync-ldap command

I am trying to run a playbook with sync ldap command :
ambari-server sync-ldap --all
The thing is, after the executing of the command, it asks for username and then password.
Is there anyway to pass the username and password automatically from echo or using a script shell, without having to pass it manually ?
Check out expect.
An simple example is to use the following script.
#!/usr/bin/expect -f
ambari-server sync-ldap --all
# wait until the prompt shows "enter username". Change this to adapt to your application.
expect "enter username"
send "your_username\r"
# wait until the prompt shows "password: ". Change this to adapt to your application.
expect "password: "
send "yourPAssWoRD\r"
interact
Disclaimer: I have never used ambari-server command before.
try with below script
# cat /tmp/ambari-server-sync-ldap-unattended.sh
#!/usr/bin/expect
set timeout 20
spawn /usr/sbin/ambari-server sync-ldap --groups=/etc/ambari-server/ambari-groups.csv
expect "Enter Ambari Admin login:" { send "admin\n" }
expect "Enter Ambari Admin password:" { send "notTheRealPasswordOfCourse\n" }
interact

Bash script - second login after logging in with SSH

After you succesfuly SSH into IP Cisco phone, another login prompt (snoopyplus login:) and after that Password:appears.
I want to create a script that spawns ssh, connects to the device, logs in again in the second login prompt and then does something. So far I tried something like that:
$!/usr/bin/expect -f
spawn ssh user#ip
expect "assword:"
send "pw"
expect "snoopyplus login:"
send "user2"
expect "Password:"
send "pw2"
Script connects with SSH and ends up on the snoopyplus login:I tried to expect different words (login, ogin, snoopy..) instead of the "snoopyplus login" but it never worked. I tested it with sshpass but it ends up in the same place.
I don't want to use SSH key and I don't mind having password visible.
Is there a way to do it?
Thank you
I would put a sleep interval after you send password the first time, this will allow expect to have time. Most of my expect use is in python with pexpect using sendline which appends a carriage return, it looks like you would need to add this manually in Tcl. Also with my experience between multiple different mfgs and OS's, expecting the end of your prompt ('>', "#", "%", ":") mitigates some issues.

How to write shell script which handles interactive inputs?

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

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

Resources