ネットワーク構成を保存・復元する
なんか前にも作った気もするんだけど…
<?xml version="1.0" encoding="Shift_JIS" standalone="yes" ?> <package> <job> <script language="JScript"><![CDATA[ // CUI で実行する var shell = new ActiveXObject ("WScript.Shell"); shell.Run ("cmd /c cscript //Job:main \"" + WScript.ScriptFullName + "\"", 1, false); ]]></script> </job> <job id="main"> <script language="JScript"><![CDATA[ // ネットワーク設定オブジェクト var netconf = function () { // クロージャの皆さん var shell = new ActiveXObject ("WScript.Shell"); var fileSystemObject = new ActiveXObject ("Scripting.FileSystemObject"); var parentFolder = fileSystemObject.GetFolder (fileSystemObject.GetParentFolderName (WScript.ScriptFullName)); var configList = new Array (); // 設定ファイルリスト更新 var updateConfigList = function () { configList.length = 0; // 配列をリセット for (var itr = new Enumerator (parentFolder.Files); !itr.atEnd (); itr.moveNext ()) { var fileName = itr.item ().Name; if (fileName.match (/\.nsh$/i)) configList.push (fileSystemObject.GetBaseName (fileName)); } }; // 設定保存 var storeConfig = function () { WScript.StdOut.Write ("ファイル名 (拡張子なし): "); var name = WScript.StdIn.ReadLine (); var path = fileSystemObject.BuildPath (parentFolder.Path, name + ".nsh"); try { // ファイルを開く var textStream = fileSystemObject.CreateTextFile (path, true); } catch (exception) { WScript.StdErr.WriteLine ("ファイルの作成に失敗しました。"); WScript.StdErr.WriteLine (exception.description); return true; } // netsh コマンド実行 var command = "netsh -c interface dump "; var exec = shell.Exec (command); // コマンドの出力をファイルに書き込む while (exec.Status == 0 || !exec.StdOut.AtEndOfStream) textStream.WriteLine (exec.StdOut.ReadLine ()); textStream.Close (); WScript.StdOut.WriteLine (name + ".nsh に設定を保存しました。"); // 空行 WScript.StdOut.WriteLine (); // メニュー再表示 showMenu (); return true; }; // 設定を復元 var loadConfig = function (number) { if (number < 0 || number >= configList.length) return true; // netsh コマンドを使ってスクリプトを実行する var command = "netsh -f \"" + fileSystemObject.BuildPath (parentFolder.Path, configList [number] + ".nsh") + "\""; WScript.StdOut.WriteLine (command); var exec = shell.Exec (command); // コマンドの出力を画面表示 while (exec.Status == 0 || !exec.StdOut.AtEndOfStream) WScript.StdOut.WriteLine (exec.StdOut.ReadLine ()); // 空行 WScript.StdOut.WriteLine (); return true; } // メニュー表示 var showMenu = function () { // リスト更新 updateConfigList (); // メニューを出力 WScript.StdOut.WriteLine ("S - 現在のネットワーク構成を保存"); for (var i = 0; i < configList.length; i ++) WScript.StdOut.WriteLine ((i + 1) + " - ネットワーク構成設定 (" + configList [i] + ")"); WScript.StdOut.WriteLine ("M - メニュー表示"); WScript.StdOut.WriteLine ("Q - 終了"); WScript.StdOut.WriteLine ("コマンドを入力してください。"); WScript.StdOut.WriteLine (); return true; }; // コマンドをリスン var listen = function () { WScript.StdOut.Write (">"); var input = WScript.StdIn.ReadLine (); switch (input.substr (0, 1).toUpperCase ()) { case "S": return storeConfig (); case "M": return showMenu (); case "Q": return false; default: if (input.match (/^exit$/i)) return false; if (isNaN (input)) return true; var number = input - 1; return loadConfig (number); } }; // オブジェクトを返す return { menu: showMenu, listen: listen }; } (); // メニュー表示 netconf.menu (); // 実行ループ while (true) { if (!netconf.listen ()) break; } ]]></script> </job> </package>
トラックバック
- このエントリーにトラックバック:
- http://frog.raindrop.jp/cgi-bin/mt/mt-tb.cgi/2527
コメント