Skip to content

Instantly share code, notes, and snippets.

@felicson
Last active December 27, 2023 11:16
Show Gist options
  • Save felicson/2f6e4f56b4a6b330b37938b30a601223 to your computer and use it in GitHub Desktop.
Save felicson/2f6e4f56b4a6b330b37938b30a601223 to your computer and use it in GitHub Desktop.
cisco anyconnect golang otp autologin
package main
import (
"fmt"
"os"
"os/exec"
"time"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
)
const (
otpSecret = "secret_param_from_otp_url"
user = "login"
password = "password"
vpnHost = "connect vpn.host.example"
vpnBin = "/opt/cisco/anyconnect/bin/vpn"
)
func main() {
code, err := totp.GenerateCodeCustom(otpSecret, time.Now(), totp.ValidateOpts{
Digits: otp.DigitsSix,
Algorithm: otp.AlgorithmSHA1,
Period: 30,
})
if err != nil {
panic(err)
}
cmd := exec.Command(vpnBin, "-s")
stdin, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
go func() {
defer stdin.Close()
_, _ = fmt.Fprintln(stdin, vpnHost)
_, _ = fmt.Fprintln(stdin, user)
_, _ = fmt.Fprintln(stdin, password)
_, _ = fmt.Fprintln(stdin, code)
}()
if err := cmd.Run(); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment