Input password to bash script, save, and enter when needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Input password to bash script, save, and enter when needed
# 1  
Old 07-10-2015
Input password to bash script, save, and enter when needed

I am looking for a way to start a script and have it prompt for a password that will be used later on in the script to SSH to another host and to SFTP. I don't want the password to be hard coded. Below is my script with the actual IP's and usernames removed.

Code:
#!/usr/bin/expect -f
#!/usr/bin/bash
spawn -noecho bash




expect "ompsat5"

send "echo 'STARTING OUTAGE REPORT SCRIPT'\r"

expect "ompsat5"

USER=username

read -s -p "Password for $USER: " PASS



#######################################
#LOG INTO OMP 4                       #
#######################################

send "ssh username@10.10.10.10\r"
expect "Password:"

send "password\r"

#######################################
#LOG INTO OMP 4 DONE                  #
#######################################


#######################################
#RUN ALARM SCRIPT                     #
#######################################

expect "ompaus4"
send "cd /home/username/outagereport/modules\r"
send "pwd\r"
send "./austinreport.py\r"
send "exit\r"

#######################################
#RUN ALARM SCRIPT DONE                #
#######################################

#######################################
#RUN COMMAND FOR SCHERTZ ALARMS       #
#######################################
expect "ompsat5"
send "\n"
expect "Connection to"
send "./5outage.py\r"


#########################################
#DONE RUNNING COMMAND FOR SCHERTZ ALARMS#
#########################################

######################################
#SFTP REPORT TO SW 5                 #
######################################

send "sftp username@10.10.10.10\r"
expect "Password:"

send "password\r"
expect "sftp"
send "cd /home/username/outagereport/modules\r"
send "get AustinOutageReport.txt\r"
send "exit\r"

######################################
#DONE SFTP REPORT TO SW 5            #
######################################

############################################
#CD, RM, COMBINE, FORMAT, DISPLAY REPORT   #
############################################

expect "ompsat5"
send "cd /home/username/outagereport/modules\r"
send "rm masterreport.txt\r"
send "cat AustinOutageReport.txt outagereport.txt>>masterreport.txt\r"
send "./reportsed\r"
send "clear\r"
send "rm finalreport.txt\r"
send "./finalsed\r"
send "cat finalreport.txt\r"

#################################################
#DONE CD, RM, COMBINE, FORMAT, DISPLAY REPORT   #
#################################################

interact

# 2  
Old 07-10-2015
I suggest using ssh keys. This will make it fully automatic and not require having the expect brute-forcing tool publicly installed on your system.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 07-11-2015
Found Solution

I was able to get this to work by using the following code:


Code:
set user [exec whoami]
send_user "$user"

send_user "Your Austin OMP Password: "
expect_user -re "(.*)\n"
set YourAustinOMPPassword $expect_out(1,string)

this runs the command of
Code:
whoami

and then stores it as a variable.

it then prompts for the password of the user for the omp

it expects a regular expression from the user

the RE is then set as a variable that can be called later in the script for SSH and SFTP

---------- Post updated at 11:42 PM ---------- Previous update was at 11:39 PM ----------

here is the full sanitized code

Code:
#!/usr/bin/expect -f
#!/usr/bin/bash
spawn -noecho bash




expect "ompsat5"

send "echo 'STARTING OUTAGE REPORT SCRIPT'\r"

expect "ompsat5"

set user [exec whoami]
send_user "$user"

send_user "Your Austin OMP Password: "
expect_user -re "(.*)\n"
set YourAustinOMPPassword $expect_out(1,string)



#######################################
#LOG INTO OMP 4                       #
#######################################

send "ssh $user@10.10.10.10\r"
expect "Password:"

send "$YourAustinOMPPassword\r"

#######################################
#LOG INTO OMP 4 DONE                  #
#######################################


#######################################
#RUN ALARM SCRIPT                     #
#######################################

expect "ompaus4"
send "cd /home/$user/outagereport/modules\r"
send "pwd\r"
send "./austinreport.py\r"
send "exit\r"

#######################################
#RUN ALARM SCRIPT DONE                #
#######################################

#######################################
#RUN COMMAND FOR SCHERTZ ALARMS       #
#######################################
expect "ompsat5"
send "\n"
expect "Connection to"
send "./5outage.py\r"


#########################################
#DONE RUNNING COMMAND FOR SCHERTZ ALARMS#
#########################################

######################################
#SFTP REPORT TO SW 5                 #
######################################

send "sftp $user@10.10.10.10\r"
expect "Password:"

send "password\r"
expect "sftp"
send "cd /home/$user/outagereport/modules\r"
send "get AustinOutageReport.txt\r"
send "exit\r"

######################################
#DONE SFTP REPORT TO SW 5            #
######################################

############################################
#CD, RM, COMBINE, FORMAT, DISPLAY REPORT   #
############################################

expect "ompsat5"
send "cd /home/$user/outagereport/modules\r"
send "rm masterreport.txt\r"
send "cat AustinOutageReport.txt outagereport.txt>>masterreport.txt\r"
send "./reportsed\r"
send "clear\r"
send "rm finalreport.txt\r"
send "./finalsed\r"
send "cat finalreport.txt\r"

#################################################
#DONE CD, RM, COMBINE, FORMAT, DISPLAY REPORT   #
#################################################

interact

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to automatically enter password in a script?

Hi I'm working with AIX 6.1 I would like to ssh to a server without entering password ( to monitor something) but there's no way to do that by authentication keys, so I need to write a script which can ssh to that server without entering password ( no need to hide passsword in the script, just an... (9 Replies)
Discussion started by: bobochacha29
9 Replies

2. Shell Programming and Scripting

Make a password protected bash script resist/refuse “bash -x” when the password is given

I want to give my long scripts to customer. The customer must not be able to read the scripts even if he has the password. The following command locks and unlocks the script but the set +x is simply ignored. The code: read -p 'Script: ' S && C=$S.crypt H='eval "$((dd if=$0 bs=1 skip=//|gpg... (7 Replies)
Discussion started by: frad
7 Replies

3. Shell Programming and Scripting

Loop logic, enter into respective IF as per enter input file name

have three big data file, however I just need to see the mentioned below one line form the all the file which has SERVER_CONNECTION Value File 1 export SERVER_CONNECTION=//dvlna002:10001/SmartServer File2 export SERVER_CONNECTION=///SmartServer File3 export... (1 Reply)
Discussion started by: Nsharma3006
1 Replies

4. Shell Programming and Scripting

function terminating if i give input as space or no input and enter

HI i have written a script to ask input from the user. this script should promote the user for y/n input. if user enters anyother input then y/n the script promotes him again. this below code is working fine for all the cases. except for space and enter " if i give space and enter it is... (2 Replies)
Discussion started by: BHASKARREDDY006
2 Replies

5. Shell Programming and Scripting

Enter an input and reference another line in ksh script

Hi I have a file like so: Code: Frank Peter Tony Robert Mike 1 2 3 4 5 5 4 2 3 1 4 3 1 5 2 My out should look like this: Peter Tony Mike and so on.... I have the first part done to ask the user to... (8 Replies)
Discussion started by: bombcan1
8 Replies

6. Shell Programming and Scripting

Script to automatically enter a password

I need to retrieve thousands of lines of information from hundreds of nodes. Each node requires a passowrd in order to retrieve the information. Fortunately, the password is the same for each one of them. So I am trying to come up with a script that would allow me to include the password so I can... (0 Replies)
Discussion started by: Ernst
0 Replies

7. Shell Programming and Scripting

How to enter a return key in bash script?

Hi, I'm porting an install script from AIX to Red Hat (2.6.18-164.el5 #1 SMP) I have this script working in both AIX and HP-UX. The script is a wrapper for a Micro Focus Server Express install program. It responds to the install program questions with a here-now list. Responses includes... (14 Replies)
Discussion started by: duker61
14 Replies

8. Shell Programming and Scripting

enter password at prompt during a script?

I'm using rsync with the "-e ssh" option so of course it asks for a password using a prompt. Is there a way to tell a script to expect a prompt, wait for it, and give a password when it arrives? There is a way to give rsync a password as part of its options using a file, but it only works with... (2 Replies)
Discussion started by: davidstvz
2 Replies

9. Shell Programming and Scripting

How to enter a password in the script automatically when prompted?

Hi Friends, We need to create a script which will invoke a command with diffrent parameters. The command invoked needs the password. So how automatically we can enter password in the script to the command? example.: #!/bin/ksh for par in `cat parfile` do # Here is the main command... (1 Reply)
Discussion started by: sourabhsharma
1 Replies

10. Shell Programming and Scripting

BASH: Any Way to Get User Input Without Requiring Them to Hit the Enter Key?

I'm working on making a menu system on an HP-UX box with Bash on it. The old menu system presents the users with a standard text menu with numbers to make selections. I'm re-working the system and I would like to provide something more akin to iterative search in Emacs. I have a list of 28... (2 Replies)
Discussion started by: deckard
2 Replies
Login or Register to Ask a Question