Until user input matches variable do - bash

Okay, so I'm trying to create a username/password login script of sorts. (may not be the most secure idea I'm still working on it) ;)
My script will load variables to compare to from file like this. (right now I'm just working on password portion)
./path/to/variables.conf
This file will contain a variable called
PASS=SOME_VALUE
I plan to use read to obtain the variable that will be compared
read -p "Enter your password:" CPASS;
Now the part I'm missing (how I envision it working)
while "$CPASS" doesn't match "$PASS" do
read -p "Wrong password, try again:" CPASS;
Thank you & any help is appreciated.

This should do it. You just need [ ] braces and the != operator to compare strings in bash:
PASS=SOME_VALUE
read -p "Enter your password:" CPASS
while [ "$CPASS" != "$PASS" ]; do
read -p "Wrong password, try again:" CPASS
done
Also note it would be highly advisable to pass the -s parameter to read, so that the entered password is not echoed back to the user. From the read section of man bash:
-s Silent mode. If input is coming from a terminal, char-
acters are not echoed.
Note though that there will also be no newline echoed back to the user when the user hits ENTER, so you'll have to manually insert a newline after every read so that lines are properly formatted. So you should probably replace your read statements with something like this:
read -s -p "Enter your password:" CPASS
echo

Related

How can I login to a program in Linux with a .sh script?

I'm sure that this is somewhere on stack overflow, but I've been researching this for hours and none of the answers work.
I need to answer prompts in a .sh script to log in to different accounts automatically.
My script will not answer the prompts automatically.
#!/bin/bash
program9000 login
That part works fine.
Then i get a series of prompts and cannot figure out how to get the .sh script to reply automatically. I will be replying to these 4 prompts the same way every time.
Would you like to enter a new key?
I would like to reply
y
How would you like to authenticate?
1. enter password
2. do something else
3. do something else
q. Quit
I would like to reply
1
Enter your password, please
I would like to reply
mypassword
Enter your Domain name, please
I would like to reply
https://mydomain.mine.com
Please note that I am not writing the prompts, they are coming from program9000. I just want to reply with the same 4 responses every time.
The following have not worked in my .sh script
yes
echo
send
What should the following lines of my script look like?
Here are some things that don't work
#!/bin/bash
program9000 login
send "y"
send "1"
send "fakepassword"
send "https://fakedomain.com"
#!/bin/bash
program9000 login
{ send -- "y" }
{ send -- "1" }
{ send -- "fakepassword"
{ send -- "https://fakedomain.com" }
You can use expect language, available almost with every *nix distributive
You can send multiline "answerfile" or string to your program using redirection
program9000 login <<< "
y
1
fakepassword
https://fakedomain.com"
As others have already mentioned, use expect.
apt show expect
...
Description: Automates interactive applications
Expect is a tool for automating interactive applications according to a script.
Following the script, Expect knows what can be expected from a program and what
the correct response should be.
...
See the expect man page here
To install it on Debian or it's derivatives:
apt install expect
You can use expect.
My English is terrible.So I write a example.
fakeprogram9000.sh
assert_var()
{
if [ $1 != $2 ]; then
echo err; exit 1
fi
}
read -p 'Would you like to enter a new key?' var1
assert_var $var1 'y'
read -p 'How would you like to authenticate?' var1
assert_var $var1 '1'
read -p 'Enter your password, please' var1
assert_var $var1 'fakepassword'
read -p 'Enter your Domain name, please' var1
assert_var $var1 'https://fakedomain.com'
echo success
example.sh
#!/bin/bash
expect<<EOF
spawn bash ./fakeprogram9000.sh login
expect "Would you like to enter a new key?" {send "y\r"}
expect "How would you like to authenticate?" {send "1\r"}
expect "Enter your password, please" {send "fakepassword\r"}
expect "Enter your Domain name, please" {send "https://fakedomain.com\r"}
expect eof
EOF

Pass user inputs from one script to another during runtime

I have a requirement where ScriptA.sh has commands to ask for User's inputs and perform a set of actions. I want to automate this by creating another script which will read the questions asked from output of ScriptA.sh and provide the necessary values in runtime.
ScriptA.sh as follows :-
echo "Enter the CR Number"
read varnamecr
echo "CR Number is" $varnamecr
echo "Loading the config set. Choose Option From Below set
1.JAN
2.FEB
3.MAR"
read optionchoosen
echo "Option Choosen is :" $optionchoosen
echo "Will run the script/load configuration is this Ok ?[y/N]"
read userinput
echo "Proceed further, User has pressed ->"$userinput"<--Key"
How to write the second script to achieve this. Tried spawn and few other commands in the second script, but no luck. Please help me with this.
Since you're not specifying any shell in your tag, this is a possible, albeit crude, solution in ksh. It's using the coprocess capability of that shell (pretty sure it's not supported in bash although please don't quote me on that one)
#!/bin/ksh
./ScriptA.sh |&
while read -p Dummy; do
print $Dummy
case $Dummy in
"Enter the CR Number")print -p "CR123456"
;;
"3.MAR")print -p "3"
;;
"Will run the script"*)print -p "y"
;;
esac
done
The output gives :
Enter the CR Number
CR Number is CR123456
Loading the config set. Choose Option From Below set
1.JAN
2.FEB
3.MAR
Option Choosen is : 3
Will run the script/load configuration is this Ok ?[y/N]
Proceed further, User has pressed ->y<--Key
Will input remain same everytime? If so you can create wrapper of this script to provide required input.
cat wrapper
./ScriptA.sh <<!
123
2
y
!

How to pass variables from a bash script to TCL?

I have never used TCL before but am needing to use it in order to script commands in a tool we use. I have a bash script running that obtains some information from AD, which it will then pass to the TCL script to use. here is my bash script which runs without any issue.
echo "Enter username for LDAP Search"
read USERNAME
export USERNAME
echo "Enter password"
read -s PASSWORD
export PASSWORD
echo "What user do you want to add to Centrify?"
read CENTRIFY_USER
export CENTRIFY_USER
OBJECTSID=`ldapsearch -H ldap://my.domain.com:389 -D "$USERNAME#MY.REALM.COM" -w $PASSWORD -x -b "DC=my,DC=domain,DC=com" "(&(objectCategory=user)(sAMAccountName=$CENTRIFY_USER))" | grep objectSid | cut -d " " -f2`
SID=`/home/mydirectory/convert_objectSid_to_sid.sh $OBJECTSID`
export SID
echo "Adding user to Centrify..."
/home/mydirectory/add_users_to_centrify.sh
"add_users_to_centrify.sh" is the tcl script that is then called, but I get the error error during execution: can't read "USERNAME": no such variable in the tcl script.
Here are the contents of that:
#!/bin/sh
# \
exec adedit "$0" ${1+"$#"}
package require ade_lib
puts $env(USERNAME)
puts $env(PASSWORD)
puts $env(SID)
puts $env(CENTRIFY_USER)
bind my.domain.com $USERNAME {$PASSWORD}
Another issue, when the tcl script is called, all of the arguments I'm passing get printed, including the password. I had thought exporting would be the safest way to do this as it should only set the environment variables for this subshell and not print them. What's happening here?
The password is getting printed because you're explicitly printing the password (puts $env(PASSWORD)).
The error seems very clear: there is no variable in the tcl script named USERNAME. You could set one like this (and similarly for PASSWORD):
set USERNAME $env(USERNAME)
Or you could just use the environment variables directly:
bind my.domain.com $env(USERNAME) {$env(PASSWORD)}

How to pass consecutive multiple arguments and enter to a command inside shell script?

I have created a simple shell script. In this script one command require user input as 1 1 1 ENTER ENTER ENTER ENTER etc.
How can i pass these user inputs to script?
Regards,
Ankit
If you want to pass three lines consisting of the string "1" followed by an arbitrary number of blank lines to the command cmd, you can do:
yes "" | sed -e 1,3s/^/1/ | cmd
What shell are you using?
Typically ,You can pass user input using read.
Here is an example using bash.
#!/bin/bash
# Ask User to Input Data
echo "Enter your Number"
#Store user input to a variable
read userNumber
#Process user input. In this case display it back
echo "You entered $userNumber"

Pass a variable in a shell script

I'm new to Unix...I have a shell script that calls sqlplus. I have some variables that are defined within the code. However, I do not feel comfortable having the password displayed within the script. I would appreciate if someone could show me ways on how to hide my password.
One approach I know of is to omit the password and sqlplus will
prompt you for the password.
An approach that I will very much be interested in is a linux
command whose output can be passed into the password variable. That
way, I can replace easily replace "test" with some parameter.
Any other approach.
Thanks
#This is test.sh It executes sqlplus
#!/bin/sh
export user=TestUser
export password=test
# Other variables have been ommited
echo ----------------------------------------
echo Starting ...
echo ----------------------------------------
echo
sqlplus $user/$password
echo
echo ----------------------------------------
echo finish ...
echo ----------------------------------------
You can pipe the password to the sqlplus command:
echo ${password} | sqlplus ${user}
tl;dr: passwords on the command line are prone to exposure to hostile code and users. don't do it. you have better options.
the command line is accessible using $0 (the command itself) through ${!#} ($# is the number of arguments and ${!name} dereferences the value of $name, in this case $#).
you may simply provide the password as a positional argument (say, first, or $1), or use getopts(1), but the thing is passwords in the arguments array is a bad idea. Consider the case of ps auxww (displays full command lines of all processes, including those of other users).
prefer getting the password interactively (stdin) or from a configuration file. these solutions have different strengths and weaknesses, so choose according to the constraints of your situation. make sure the config file is not readable by unauthorized users if you go that way. it's not enough to make the file hard to find btw.
the interactive thing can be done with the shell builtin command read.
its description in the Shell Builtin Commands section in bash(1) includes
-s Silent mode. If input is coming from a terminal, characters are not echoed.
#!/usr/bin/env bash
INTERACTIVE=$([[ -t 0 ]] && echo yes)
if ! IFS= read -rs ${INTERACTIVE+-p 'Enter password: '} password; then
echo 'received ^D, quitting.'
exit 1
fi
echo password="'$password'"
read the bash manual for explanations of other constructs used in the snippet.
configuration files for shell scripts are extremely easy, just source ~/.mystuffrc in your script. the configuration file is a normal shell script, and if you limit yourself to setting variables there, it will be very simple.
for the description of source, again see Shell Builtin Commands.

Resources