问题
I have a expect script written to perform a login operation. The login requires the 2fa code, this code can be obtained by executing the shell command 2fa my_login
. Now how do I execute the 2fa command and pass it's output to the send of my script?
I need to do something like send -- "$(2fa my_login)"
. The output of the $(2fa my_login) should be passed to the send.
My script
#!/usr/bin/expect -f
set timeout -1
spawn /home/local/apps/forticlientsslvpn/64bit/forticlientsslvpn_cli --server vpn.vpn-domain.com:10443 --vpnuser user-id
expect "Password for VPN:"
send -- "pass\n"
expect "Enter the time-based OTP generated in your device."
send -- "$(2fa my_login)\n"
expect eof
回答1:
Using the sexpect
#!/bin/bash
export SEXPECT_SOCKFILE=/tmp/sexpect-bc-iGciUZ.sock
sexpect spawn -close-on-exit /home/local/apps/forticlientsslvpn/64bit/forticlientsslvpn_cli --server vpn.vpn-domaincom:10443 --vpnuser user
sexpect expect -glob "Password for VPN:"
sexpect send -enter -- "pass"
sexpect expect -glob "Enter the time-based OTP generated in your device."
auth=$(2fa my_login)
sexpect send -enter -- $auth
sexpect wait
来源:https://stackoverflow.com/questions/63447519/how-to-use-the-output-of-the-shell-command-in-expect-scripts-send