1

The need for this arose from the script below. It connects to a remote PC using anyconnect's vpn utility. First it asks for the one-time-password, connects, starts an RDP client and disconnects after the RPD client app has been closed.

if [ $# == 1 ]; then
    ONE_TIME_PWD=$1;
else
    printf "Enter the one-time password: ";
    read ONE_TIME_PWD;
fi

vpn -s connect <domain> << EOF
<user>
<pin>$ONE_TIME_PWD
EOF

# Use some RDP client here like xfreerdp or rdesktop

vpn -s disconnect <domain>

The problem is that one-time-password may change during the execution of vpn -s connect <domain>. So I was curious whether it's possible to change the script so that it lets me enter the one-time-password after <pin> has been inserted automatically? I've tried it with head -c -1 to remove the last newline char but the input was still finished. Any other solution not based on EOF is acceptable.

2 Answers 2

1

You can always do:

{
  printf '<user>\n<pin>'
  printf 'Enter the one-time password: ' > /dev/tty
  IFS= read -r otp < /dev/tty
  printf '%s\n' "$otp"
} | vpn -s connect <domain>

Which prompts for the password after vpn has been started and already fed <user>\n<pin>.

It reads the password and prints the prompts on the tty device, alternatively you could read from stdin (remove the < /dev/tty) and print the prompt on stderr (replace > /dev/tty with >&2).

If using zsh instead of bash, you could simplify that to:

{
  printf '<user>\n<pin>'
  IFS= read -rse '?Enter the one-time password: '
} | vpn -s connect <domain>
  • -s to suppress the terminal echo (that one also supported by bash), preferable to input passwords.
  • -e to echo the entered text on stdout instead of storing into a variable
  • ?prompt, prompt string sent on stderr, same syntax as ksh. bash has a -p option for that.
2
  • Something goes wrong here. The output starts with Enter the one-time password: Cisco AnyConnect Secure Mobility Client (version 4.6.02074) . Copyright (c) 2004 - 2018 Cisco Systems, Inc. All Rights Reserved. ... As you can see I have no chance to enter something
    – ka3ak
    Commented Nov 25, 2018 at 8:17
  • @ka3ak well yes. both parts of the pipeline are run together so you can enter the password after vpn has started as you asked. You can add a sleep 1 before the prompt if you want it to be printed after vpn's initialisation message. Commented Nov 25, 2018 at 8:23
0

You could probably do something similar to this -

$: head -1 <<!
hello, $(head -1<$(tty))!
!

But I don't recommend it for several reasons.

Only slightly better -

$: head -1 <<!
 hello, $(read -rs pw <$(tty); echo "$pw" )!
!

 hello, world!

Still, it's what you asked.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .