Specifying the password to connect with gcloud MySQL - shell

I want to pass the password along with the user id in the single statement while connecting to the gcloud SQL instance. How will I do that? I tried a few combinations but couldn’t get any solutions.
Ex:
gcloud sql connect scegold2-f44e-mysql --user=username --password=password --quiet
It shows error around password.
Any help and guidance can I get. Am I doing anything wrong?

The expect program can be used in order to pass every argument at once to gcloud sql connect, including the password, for example using this autoconnect.exp script:
#!/usr/bin/expect -f
# Call command with "expect autoconnect.exp $INSTANCE $USER $PASSWORD",
# or place the arguments literally in the script, where the variables are.
set INSTANCE [lindex $argv 0]
set USER [lindex $argv 1]
set PASSWORD [lindex $argv 2]
set timeout -1
spawn gcloud sql connect $INSTANCE --user=$USER
expect "*Password*"
send -- "$PASSWORD\r"
expect eof
What this does is to wait for the gcloud command to prompt for the password (expect "*Password*" means wait for a line containing "Password") and then feed it the password, and then the database management system is opened as normal.
A nice guide for expect here.

As mentioned in the documentation, the flags for the "gcloud sql connect"command are:
--user=USER, -u USER
Based on this, you cannot specify the password in the same command line, and even if you create an instance by specifying "no password", it will ask you for the password (in that case you should only press enter).

Related

expect command not working as expected but working if not using expect script

So I have this script to create a user in my openvpn and the create_user.sh is working fine. But I want to create a bulk user for my openvpn and when I run this expect script it's also working fine, its response exactly to the prompt of the openvpn easyrsa. But the problem comes if I test the user and try to connect to openvpn it gives me error.
#!/usr/bin/expect
set user [lindex $argv 0]
set pass1 [lindex $argv 1]
set pass2 [lindex $argv 2]
set phrase [lindex $argv 3]
set timeout -1
spawn ./create_user.sh
expect "Enter Username:" { send -- "$user\r" }
expect "Enter PEM pass phrase:" { send -- "$pass1\r" }
expect "Verifying - Enter PEM pass phrase:" { send -- "$pass2\r"}
expect "Enter pass phrase for /etc/openvpn/pki/private/ca.key:" { send -- "$phrase\r"}
expect eof
This error I received
I know that the problem is in my PEM pass and pass phrase but when I use my create_user.sh its working fine and it can connect to the openvpn server. So I guess my expect script is not working fine when passing the values to the prompt of PEM pass phrase and pass phrase. Can somebody help me on this I'm just newbie in expect scripting.
This is the prompt when the create_user.sh run.

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

What does it mean to combine interact with return in expect?

I want to use shell and expect to log in to the server with only one command.
But when interact and return are used in one statement, I don't understand what they mean.In addition, "Password" will only appear when SSH link multiplexing is disconnected, so I hope that when "Password" does not appear, it will not send "mypassord".
So how to write scripts to deal with these two situations in a unified way?
When SSH multiplexed links were established, my script was stuck in "expect "Password"".How to deal with it?
In addition, I would like to ask what does the -o parameter mean after the interact? And what is the meaning of using interact and return together?
What's the difference between "send --" and "send "?
I have enabled SSH master connection. Before I log on to the server, I need to first enter the password, and then enter a number to represent which machine I log on to. But when SSH multiplexed links are established, the option of entering passwords becomes redundant.
#!/usr/bin/expect
# ssh command
set cmd [lindex $argv 0]
set relay_num [lindex $argv 1]
set timeout -1
# run ssh command
spawn bash -c "$cmd"
expect "Password*"
send "mypassord\r"
interact -o -nobuffer -re "Option" return
send -- "$relay_num\r"
interact
In combination with the -o switch, that line is looking at the output of the command for the regex "Option". The return will cause the interact command to end and the script continues to the send command (that is explained in the man page).
I think it would be more clear to use expect "Option" instead.

Unable to call a command on windows server by connecting through linux

I am using a following script which connects to windows server from Linux, but after connecting i have to call the a command by doing to a location under D: drive of windows followed by some folders (Where the Folder names consists of spaces eg: d:\Rakesh Tatineni\not able to execute\Manager.exe 1 1)
Above is the some how i want to execute/call a command with 2 argument 1 1.
Script using to connect to windows
#!/usr/bin/expect -f
# Expect script to supply username/admin password for remote ssh server
# and execute command.
# This script needs three argument to(s) connect to remote server:
# password = Password of remote Windows server, for Windows user.
# For example:
# ./call_engine.sh password
# set Variables
set password [lrange $argv 0 0]
set timeout -1
# now connect to remote windows box (ipaddr/hostname) with given script to execute
spawn ssh userid#<WindowsserverName>
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send "$password\r"
# send blank line (\r) to make sure we get back to gui
send "\r"
expect eof
Appreciate your help , in between what kind of code we have to include to call a command as i mentioned above "d:\Rakesh Tatineni\not able to execute\Manager.exe 1 1"
Thanks,
Rakesh T
I see that you already give the script an argument of password. So you can change your script slighlty to add two more arguments like this:
set password [lrange $argv 0 0]
set arg1 [lrange $argv 1 1]
set arg2 [lrange $argv 2 2]

how to use a shell script to supply a password when the interface asks for it

I have a script(dobrt) which upon executing asks for a password.How can i write a script which executes dobrt and automatically supplies the password it asks for.
when i execute ./dobrt -p file.txt , the system asks for a password. I want the password to be sent in automatically by the script. Here is the output
$ ./dobrt -p file.txt
Found 194 tests to execute
------------ 2010 February 11 11:27:33 ------------
Password: ***************
I tried using shell and expecxt scripts for this. here is what i did.
I have 2 scripts. I call the second script(run_dobrt.exp) from the first one(run_dobrt.sh).
Script 1 : run_dobrt.sh
#!/bin/ksh
TESTCASE_HOME="/home/abhijeet/code/testcases";
TESTCASE_LIST="file.txt";
PASSWORD="*****";
echo "Running Expect Script"
`./run_dobrt.exp $TESTCASE_HOME $TESTCASE_LIST $PASSWORD`
Script 2: run_dobrt.exp
#!/usr/local/bin/expect -f
set TESTCASE_HOME [lindex $argv 0];
set TESTCASE_LIST [lindex $argv 1];
set PASSWORD [lindex $argv 3];
set timeout 200
spawn $TESTCASE_HOME/dobrt -p $TESTCASE_HOME/$TESTCASE_LIST
expect "*?assword:*" {send -- "$PASSWORD\r";}
expect eof
Now when i run run_dobrt.sh i get the following error
run_dobrt.sh[20]: spawn: not found
How to get rid of this error and get this task done? Please help.
What is dobrt? is a self-made program? If this is the case I think you will have to recode it to parse an extra argument that accepts the password. Then you will be able to pass this passowrd to dobrt just as you do it like "-p file.txt" in the command line (through a script).
I see two problems:
In the last line of your shell script, remove the back-quotes `` around the command,
they will cause the output of the expect script to be executed as a shell command.
In the expect script, change
set PASSWORD [lindex $argv 3];
to
set PASSWORD [lindex $argv 2];
you are skipping an argument.
If the password is the only input dobrt prompts for, you could try this:
Script 1 : run_dobrt.sh
#!/bin/ksh
TESTCASE_HOME="/home/abhijeet/code/testcases";
TESTCASE_LIST="file.txt";
PASSWORD="*****";
./run_dobrt.exp $TESTCASE_HOME $TESTCASE_LIST << EOF
$PASSWORD
EOF

Resources