jbrass
July 10, 2015, 10:39am
1
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.
#!/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
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.
1 Like
jbrass
July 11, 2015, 12:42am
3
I was able to get this to work by using the following 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
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
#!/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