Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a linux expect script to enter answer a prompt for password

I am having some trouble writing a script that will launch my forticlient vpn command line client and send my password when it is prompted. Here is my code:

#!/usr/bin/expect -f
set loadme "./forticlientsslvpncli --server myvpnserver --vpnuser theuser
eval spawn $loadme
expect "Password for VPN: "
send "password\r"

However, it still prompts for the vpn password. I am sure it is something simple and I am pretty new to linux scripting, but any help would be greatly appreciated!

Thanks!

like image 699
wblakenc Avatar asked Jun 20 '13 16:06

wblakenc


People also ask

How do you pass enter in Expect script?

hence typing "\r" for "Enter" key action.

How do I run an expect script in Linux?

$ yum install expect The first line defines the expect command path which is #!/usr/bin/expect . On the second line of code, we disable the timeout. Then start our script using spawn command. We can use spawn to run any program we want or any other interactive script.

How do I password a script in Linux?

The useradd command/adduser command used to create a new user on Linux and passwd command to set or change password for users.

What does expect command do in Linux?

The Linux expect command takes script writing to an entirely new level. Instead of automating processes, it automates running and responding to other scripts. In other words, you can write a script that asks how you are and then create an expect script that both runs it and tells it that you're ok.


2 Answers

#!/usr/bin/expect -f
set timeout -1
cd /usr/local/forticlientsslvpn
spawn ./forticlientsslvpn_cli --server myhost:10443 --vpnuser myuser
expect "Password for VPN:" {send -- "mypassword\r"}
expect "to this server? (Y/N)\r" {send -- "y\r"}

expect eof
like image 63
Gregory Motruk Avatar answered Oct 27 '22 00:10

Gregory Motruk


From the comment I got from glenn jackman I was able to figure out that the password prompt was not being matched. I changed my first line to #!/var/bin/expect -d which provided the necessary debugging output to find out the problem and fix it.

Thanks Glenn!

like image 21
wblakenc Avatar answered Oct 26 '22 22:10

wblakenc