「サーバ構築/terminal/poderosa」


マクロ
/**
* 複数の接続を一気に行うマクロ
* MultiAutoLogin.js
* Poderosa マクロ で複数サーバーへドンドン接続しちゃう!方法 https://blog.gti.jp/post-159/
*/
import Poderosa;
import Poderosa.ConnectionParam;
import Poderosa.Terminal;
import Poderosa.Macro;
import Poderosa.View;
import System.Drawing;
import System.Threading;

var env = new Environment();
var ENCODE_TYPE = EncodingType.UTF8;
var NEWLINE_CHAR = NewLine.LF;
var SSH_PORT = 22;
var hostList = new Array();
var hostip = "192.168.56.1";
var loginuser = "USER";
var loginpw = "PASSWORD";
var loginport = "22";


hostList[0] = new Array();
hostList[0]['host'] = hostip; // ドメイン OR IP
hostList[0]['id'] = loginuser; // ログインアカウント
hostList[0]['pwd'] = loginpw; // ログインパスワード
hostList[0]['title'] = "working"; // タブのタイトル
hostList[0]['port'] = 22; // SSHのポートを変更している場合は設定する
hostList[0]['encode'] = EncodingType.UTF8; // エンコードがデフォルト値以外の場合設定する
hostList[0]['newline_char'] = NewLine.LF; // 改行コードがデフォルト値以外の場合設定する
hostList[0]['commands'] = new Array(); // コマンド発行の場合必要(任意)
hostList[0]['commands'][0] = "pwd"; // コマンド1(任意)
hostList[0]['commands'][1] = "ls -lat"; // コマンド2(任意)


hostList[1] = new Array();
hostList[1]['host'] = hostip;
hostList[1]['id'] = loginuser;
hostList[1]['pwd'] = loginpw;
hostList[1]['title'] = "log";
hostList[1]['commands'] = new Array();
hostList[1]['commands'][0] = "cd /var/www/html";
hostList[1]['commands'][1] = "pwd";


hostList[2] = new Array();
hostList[2]['host'] = hostip;
hostList[2]['id'] = loginuser;
hostList[2]['pwd'] = loginpw;
hostList[2]['title'] = "DB";
hostList[2]['commands'] = new Array();
hostList[2]['commands'][0] = "tail -f /var/log/messages";


hostList[3] = new Array();
hostList[3]['host'] = hostip;
hostList[3]['id'] = loginuser;
hostList[3]['pwd'] = loginpw;
hostList[3]['title'] = "4";
for (var i = 0; i < hostList.length; i++) {
   var ssh_port_num = SSH_PORT;
   if (hostList[i]['port'] != null && hostList[i]['port'] != SSH_PORT) {
       ssh_port_num = hostList[i]['port'];
   }
   var encoding_type = ENCODE_TYPE;
   if (hostList[i]['encode'] != null && hostList[i]['encode'] != ENCODE_TYPE) {
       encoding_type = hostList[i]['encode'];
   }
var newline_char = NEWLINE_CHAR;
if (hostList[i]['newline_char'] != null && hostList[i]['newline_char'] != NEWLINE_CHAR) {
    newline_char = hostList[i]['newline_char'];
   }
   var conn = connect(hostList[i]['host'], ConnectionMethod.SSH2, ssh_port_num, encoding_type, newline_char, hostList[i]['id'], hostList[i]['pwd'], hostList[i]['title']);
   if (hostList[i]['commands']) {
       if (hostList[i]['commands'].length > 0) {
           wait(conn, "$");
           for (var j = 0; j < hostList[i]['commands'].length; j ++) {
               sendln(hostList[i]['commands'][j], conn);
           }
       }
   }
}
env.Util.MessageBox("処理終了しました!");
function connect(host, method, port, encoding, newline_char, id, password, title) {
   var param = new SSHTerminalParam(method, host, id, password);
   param.Caption = title+"["+host+"]";
   param.Port = port;
   param.Encoding = encoding;
   param.TransmitNL = newline_char;
   var connection = env.Connections.Open(param);
   return connection;
}
function sendln(s, connection) {
   connection.TransmitLn(s);
}
function wait(connection, command) {
   Thread.Sleep(10);
   var res = connection.ReceiveData();
   while(res.indexOf(command) == -1) {
       Thread.Sleep(10);
       res += connection.ReceiveData();
   }
}
最終更新:2017年06月11日 03:30