Yamahaルータのちょこちょこスクリプト
リモートワーク需要で、YamahaのルータのL2TP設定することが多くなってきたんですが……。
基本ユーザ数が多すぎてコピペとトンネル数字入替がめんどくさいのと、手動で”no “をCtrl+Vするのがだるくなってきたのでスクリプト作りました。
JavaScriptでざーっと作ってあるので、HTMLコピペしてあげればデスクトップとかでも動きます。
パスに文字入れたいとかあればー。
自分で使うついでに置いておくので使う人いたらどうぞー。
Windowsのコマンドプロンプトで動くPerlも置いておきます。
#!/usr/bin/perl
use strict;
use warnings;
use Encode;
use utf8;
binmode STDOUT, ":encoding(cp932)";
# L2TPユーザたくさんつくるPerl
# 文字
my @moji = qw/a b c d e f g h i j k m n p q r s t u v w x y z A B C D E F G H J K L M N P Q R S T U V W X Y Z 2 3 4 5 6 7 8 9/;
# 前情報入力
my $minline = 1;
my $maxline = 10;
my $ident = "";
my $psk = "";
my $kpav = 0;
my $zfill = 0;
my $uc = 0;
my $pc = 0;
my $output = "";
print "開始トンネル番号(default:1):";
$minline = <STDIN>;
print "終了トンネル番号(default:10):";
$maxline = <STDIN>;
print "ユーザ識別子(default:vpnuser):";
$ident = <STDIN>;
print "事前共有鍵 ※8文字以上(default:sharedkey):";
$psk = <STDIN>;
print "キープアライブ利用有無 1=有,0=無(default:1):";
$kpav = <STDIN>;
print "ユーザ序数0埋桁数(default:2):";
$zfill = <STDIN>;
print "ユーザ数(default:10):";
$uc = <STDIN>;
print "パスワード桁数(default:8):";
$pc = <STDIN>;
print "出力ファイル名(default:l2tp.txt):";
$output = <STDIN>;
# 改行削除
chomp($minline);
chomp($maxline);
chomp($ident);
chomp($psk);
chomp($kpav);
chomp($zfill);
chomp($uc);
chomp($pc);
chomp($output);
# 初期値入力
if ($minline eq "") { $minline = 1; }
if ($maxline eq "") { $maxline = 10; }
if ($ident eq "") { $ident = "vpnuser"; }
if ($psk eq "") { $psk = "sharedkey"; }
if ($kpav eq "") { $kpav = 1; }
if ($zfill eq "") { $zfill = 2; }
if ($uc eq "") { $uc = 10; }
if ($pc eq "") { $pc = 8; }
if ($output eq "") { $output = "l2tp.txt"; }
# 出力ファイルの末尾が.txtじゃない場合.txtを付ける
if ($output !~ /^.+\.txt$/) { $output .= ".txt"; }
# エラー
my @errors;
if ($minline !~ /^\d+$/) { push(@errors, "開始トンエル番号が数値でないです。"); }
if ($maxline !~ /^\d+$/) { push(@errors, "終了トンエル番号が数値でないです。"); }
if ($zfill !~ /^\d+$/) { push(@errors, "ユーザ序数桁数が数値でないです。"); }
if ($uc !~ /^\d+$/) { push(@errors, "ユーザ数が数値でないです。"); }
if ($pc !~ /^\d+$/) { push(@errors, "パスワード桁数が数値でないです。"); }
if (length($psk) < 8) { push(@errors, "事前共有鍵は8文字以上です。"); }
if ($kpav != 1 && $kpav != 0) { push(@errors, "キープアライブ有無は1か0です。"); }
if ($maxline < $minline) { push(@errors, "開始トンネル番号が、終了トンネル番号より大きいです。"); }
# エラーがあれば終了
if (scalar(@errors) > 0)
{
printf("%s", join("\n", @errors));
exit(1);
}
# 置き換え処理
if ($kpav == 0) { $kpav = "off"; }
else { $kpav = "on"; }
# 出力
open my $file, ">:encoding(cp932)", $output;
flock($file, 2);
truncate($file, 0);
seek($file, 0, 0);
# 認証
printf $file "pp select anonymous\n";
for (my $i = 1; $i <= $uc; $i++)
{
my $rand = "";
for (my $j = 0; $j < $pc; $j++) { $rand .= $moji[int(rand(@moji))]; }
printf $file "pp auth username %s%0".$zfill."d %s\n", $ident, $i, $rand;
}
print $file "\n";
# トンネル
for (my $i = $minline; $i <= $maxline; $i++)
{
printf $file "tunnel select %d\n", $i;
printf $file " tunnel encapsulation l2tp\n";
printf $file " ipsec tunnel %d\n", $i;
printf $file " ipsec sa policy %d %d esp aes-cbc sha-hmac\n", $i, $i;
printf $file " ipsec ike keepalive log %d off\n", $i;
printf $file " ipsec ike keepalive use %d %s\n", $i, $kpav;
printf $file " ipsec ike nat-traversal %d on\n", $i;
printf $file " ipsec ike pre-shared-key %d text %s\n", $i, $psk;
printf $file " ipsec ike remote address %d any\n", $i;
printf $file " l2tp tunnel auth off\n";
printf $file " l2tp tunnel disconnect time off\n";
printf $file " l2tp keepalive use on\n";
printf $file " ip tunnel tcp mss limit auto\n";
printf $file " tunnel enable %d\n", $i;
}
print $file "\n";
# トランスポート
for (my $i = $minline; $i <= $maxline; $i++)
{
printf $file "ipsec transport %d %d udp 1701\n", $i, $i;
}
close($file);
1;
てきとーに雑につくったやつなんで中身についてはのーこめんとで……。
KintoneのカーソルAPIをPerlから使う 【Apache2.4/Ubuntu14.04】WebDAVサーバの設定