Search code examples
bashvpncisco

Connect CISCO Anyconnect VPN via bash


As title says, trying to connect vpn via bash. The following script seemed closest to the answer I'm looking for:

#!/bin/bash
/opt/cisco/anyconnect/bin/vpn -s << EOF
connect https://your.cisco.vpn.hostname/vpn_name
here_goes_your_username
here_goes_your_passwordy
EOF

When I run this the vpn starts but then exits without an error and without connecting. This seems to be caused by the -s. If I remove this parameter the VPN will start but none of the commands (ie connect vpn, username, password) will be entered. From what I read the -s option will allow the username/password to be passed. Help!


Solution

  • I had to download the expect packages (yum install expect). Here is the code I used to automate vpn connection

    #!/usr/bin/expect
    
    eval spawn /opt/cisco/anyconnect/bin/vpn connect vpn.domain.com
    
    expect "Username: " { send "username\r" }
    expect "Password: " { send "password\r" }
    
    set timeout 60
    expect "VPN>"