LoginSignup
3
0

More than 1 year has passed since last update.

MacでSoftEtherに接続する時の面倒なコマンドをexpectで自動化し、一つのコマンドで接続できるようにしてみた

Last updated at Posted at 2021-08-12

はじめに

最近、自宅の回線がIPv6に対応したことにより、今まで使用していたVPNの接続方法が利用できなくなってしまいました。

ついては、他に方法がないかなと探していたらSoftEther VPNが良さそうだということでこちらを利用することに。

しかし、Macで使用するには接続と切断に少々面倒なコマンドを入力せねばならず、使い勝手が悪い。

ということで、これを簡単にする方法を以下にまとめます。

必要なもの

  • SoftEther VPN
  • expect

expectについては以下をご参照下さい。
https://qiita.com/Mskmemory/items/013dff1a76e58fcdc364

接続

以下のスクリプトを作成。

connect.sh
#!/usr/bin/env bash

sudo /usr/local/vpnclient/vpnclient start

expect -c "
  set timeout 2
  
  spawn /usr/local/vpnclient/vpncmd
    
    expect \"1 - 3 を選択:\"
    send \"2\n\"
    
    expect \"IP アドレス:\"
    send \"\n\"
    
    expect \"VPN Client>\"
    send \"AccountConnect\n\"
    
    expect \"接続設定の名前:\"
    send \"[各自の接続名]\n\"
    
    expect \"VPN Client>\"
    send \"exit\n\"

  interact
"

実行権限を付与。

chmod a+x connect.sh

実行。

./connect.sh

切断

disconnect.sh
#!/usr/bin/env bash

expect -c "
  set timeout 2
  
  spawn /usr/local/vpnclient/vpncmd
    
    expect \"1 - 3 を選択:\"
    send \"2\n\"
    
    expect \"IP アドレス:\"
    send \"\n\"
    
    expect \"VPN Client>\"
    send \"AccountDisconnect\n\"
    
    expect \"接続設定の名前:\"
    send \"[各自の接続名]\n\"
    
    expect \"VPN Client>\"
    send \"exit\n\"

  interact
"

sudo /usr/local/vpnclient/vpnclient stop

実行権限を付与。

chmod a+x disconnect.sh

実行。

./disconnect.sh

できた!

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0