How to use the output of the shell command in expect script's send

笑着哭i 提交于 2021-01-29 13:49:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!