0

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
Santhosh Tpixler
  • 361
  • 4
  • 12

1 Answers1

2

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
Santhosh Tpixler
  • 361
  • 4
  • 12