简体   繁体   中英

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!

#!/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

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!

Your code syntax could be wrong or given password could be wrong. So you can try it on expect prompt - line by line to debug it.

You may use forticlientsslvpn_cli with Expect to feed in the password.

A complete solution available here:

https://gist.github.com/azizasm/e216bc47b54f5b68405f3c8f8b832e8a

Note: this solution will auto reconnect the if the VPN get disconnected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM