# Cisco anyconnect command task

**URL:** https://community.royalapps.com/t/cisco-anyconnect-command-task/1604
**Category:** Royal TSX (for macOS)
**Created:** [May 9, 2022, 5:09pm UTC](https://community.royalapps.com/t/cisco-anyconnect-command-task/1604 "2022-05-09T17:09:20Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![peted826](https://community.royalapps.com/letter_avatar_proxy/v4/letter/p/9fc348/32.png) [@peted826](https://community.royalapps.com/u/peted826)
#### Post date: [May 9, 2022, 5:09pm UTC](https://community.royalapps.com/t/cisco-anyconnect-command-task/1604/1 "2022-05-09T17:09:20Z")

</div>

Can someone help me out, this works in terminal but I cannot set it up as a command task, I must be missing something??

```auto
/opt/cisco/anyconnect/bin/vpn -s connect vpn.company.org <<"EOF"
username
password
y
exit
EOF

```

---

<div class="post-metadata">

### Author: ![matt\_warren](https://community.royalapps.com/user_avatar/community.royalapps.com/matt_warren/32/266_2.png) [@matt\_warren](https://community.royalapps.com/u/matt_warren)
#### Post date: [May 9, 2022, 7:51pm UTC](https://community.royalapps.com/t/cisco-anyconnect-command-task/1604/2 "2022-05-09T19:51:52Z")

</div>

I think the command task statement has to be a single line. Try escaping the line endings?

Side note, I’d put the un/pw into Custom Properties so they can be easily changed and stored as Protected Fields. Then reference the $CustomProperties.name$ in the command task.

---

<div class="post-metadata">

### Author: ![peted826](https://community.royalapps.com/letter_avatar_proxy/v4/letter/p/9fc348/32.png) [@peted826](https://community.royalapps.com/u/peted826)
#### Post date: [May 9, 2022, 9:17pm UTC](https://community.royalapps.com/t/cisco-anyconnect-command-task/1604/3 "2022-05-09T21:17:01Z")

</div>

thanks for the reply, it appears that I can get it launched with:

```shell
/opt/cisco/anyconnect/bin/vpn -s connect vpn.company.org

```

in the command task but no matter how I try to pass in the creds, it just won’t work. very odd

---

<div class="post-metadata">

### Author: ![peted826](https://community.royalapps.com/letter_avatar_proxy/v4/letter/p/9fc348/32.png) [@peted826](https://community.royalapps.com/u/peted826)
#### Post date: [May 9, 2022, 9:22pm UTC](https://community.royalapps.com/t/cisco-anyconnect-command-task/1604/4 "2022-05-09T21:22:37Z")

</div>

thanks for the reply, I can launch anyconnect with

```shell
/opt/cisco/anyconnect/bin/vpn -s connect vpn.soundtransit.org

```

in the command task but I cannot seem to be able to pass credentials for some reason

---

<div class="post-metadata">

### Author: ![pascal\_reutener](https://community.royalapps.com/user_avatar/community.royalapps.com/pascal_reutener/32/376_2.png) [@pascal\_reutener](https://community.royalapps.com/u/pascal_reutener)
#### Post date: [May 12, 2022, 12:26pm UTC](https://community.royalapps.com/t/cisco-anyconnect-command-task/1604/5 "2022-05-12T12:26:40Z")

</div>

Hi @petedykinga

I created a script for this and I run it inside the ‘Terminal’ connection of Royal TSX.

The address is taken from the ‘Computer Name’:

 ![image](https://community.royalapps.com/uploads/default/original/2X/a/a68a42696eb60464c24df68e29bcc3bea5bc3d65.png)

The password is taken from the ‘Credentials’ section.

And the script is placed in the ‘Custom Commands’ section:

 ![image](https://community.royalapps.com/uploads/default/original/2X/8/8d0246461bea0747ecc29af035b774e1f0cf5a0f.png)

The number of the Group Name (in case you have to select a group upon connecting) is taken from ‘Custom Field 1’ under ‘Custom Fields’.

The script also asks you for a 2FA password in case you need to enter one.

The script also checks if there is already a Cisco AnyConnection VPN connection active. If true, it asks you whether you want to keep it or to disconnect.

```bash
#!/bin/zsh
# title : ciscoVpnConnectAF 4 RoyalTSX.sh
# description : This script will connect/disconnect VPN via Cisco AnyConnect with 2 Factor Authentication
# author : Pascal Reutener preutener@a-f.ch (a&f systems ag)
# date : 2021-07-29
# version : 1.3
# usage : as custom command in Royal TSX
# notes : The Number of the Group Name is taken from 'Custom Field 1' in Royal TSX
# change history
# 2020-03-19 | PRE : initial release
# usage: ./ciscoVpnConnectAF.sh
# 2020-03-20 | PRE : remove storage of credentials in clear text file
# and instead use a variable
# cosmetic changes
# 2020-11-23 | PRE : added groupName variable
# 2021-07-29 | PRE : Make group name/number dynamic (taken from 'CustomField1')
#==============================================================================

# configure
var_uid=$EffectiveUsername$
var_pwd="$EffectivePassword$"
var_ciscopath="/opt/cisco/anyconnect/bin"
var_url=$URI$
# Group Name Number is taken from Royal TSX's Custom Field 1
var_groupNameNumber=$CustomField1$

connect_vpn(){	# ask for second password	echo " Please enter your OTP (One-Time Password) [ENTER]: "	read -s var_passcode	echo "\n Your One-Time Password is: $var_passcode \n\n"sleep 1read -r -d '' var_logininfo <<- EOM $var_groupNameNumber $var_uid $var_pwd $var_passcode y	EOM# connect	$var_ciscopath/vpn -s <<< "$var_logininfo" connect $var_url}

disconnect_vpn(){	# disconnect	$var_ciscopath/vpn disconnect}

if /opt/cisco/anyconnect/bin/vpn status | grep Connected > /dev/null
thenecho "\n\n --> You have currently an active Cisco AnyConnect VPN connection.\n Do you want to disconnect?\n" select yn in "Yes" "No"; do	case $yn in Yes ) disconnect_vpn; break;; No ) exit;;	esacdone
elseecho "\n\n --> You are currently NOT connected to $var_url"connect_vpn
fi

```

Use at your own risk.

I hope this helps 🙂

—Best regards, Pascal

---

<div class="post-metadata">

### Author: ![peted826](https://community.royalapps.com/letter_avatar_proxy/v4/letter/p/9fc348/32.png) [@peted826](https://community.royalapps.com/u/peted826)
#### Post date: [May 23, 2022, 9:44pm UTC](https://community.royalapps.com/t/cisco-anyconnect-command-task/1604/6 "2022-05-23T21:44:49Z")

</div>

That did it, thanks for the help!!
