0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ponderosa ssh 接続クライアント備忘録

Posted at

 概要

SSHの接続クライアントでjavascriptを利用したマクロにより多種多様な接続自動化などを行うことができる。

ダウンロード

javascript を利用した接続

import Poderosa;
import Poderosa.ConnectionParam;
import Poderosa.Terminal;
import Poderosa.Macro;

var env = new Environment();
// The default character encoding
// e.g. (japanese) EncodingType.EUC_JP, EncodingType.SHIFT_JIS, EncodingType.UTF8
var ENCODE_TYPE = EncodingType.UTF8;
// default line feed code
// e.g.
// NewLine.CR:'\r'
// NewLine.LF:'\n'
// NewLine.CRLF:'\r','\n'
var NEWLINE_CHAR = NewLine.CR;
// default ssh port number
var SSH_PORT = 22;
// Variable in which to store the connection destination
var hostData = new Array();
// Array to store the connection destination
var hostList = new Array();
// path of the secret key
var secretKeyPath = "C:\\Program Files (x86)\\Poderosa\\Macro\\id_rsa"; // Please specify their own PC's path

// ---- 1 ---- //
// hostData = new Array();
hostData['host'] = "xxx.xxx.xxx.xxx"; // domain OR IP
hostData['id'] = "username"; // login account
hostData['pwd'] = "password"; // login password
hostData['secret_key'] = secretKeyPath;
hostData['title'] = "HumanEclipse"; // title of tab
hostData['port'] = 22; // You set when you set the SSH port
hostData['encode'] = EncodingType.UTF8; // If encoding is other than the default value
hostData['newline_char'] = NewLine.CR; // If encoding is other than the default value

// 接続後に実行するコマンドを記載
hostData['commands'] = new Array();
// hostData['commands'][0] = "ssh xxx.xxx.xxx.xxx";
// hostData['commands'][1] = "ls";
// hostData['commands'][2] = "xxxxxx";
// hostData['commands'][3] = "xxxxxx";

// オープンするhostsをリストに加える。
hostList.push(hostData);

// 2つ目のホストデータを追加する。
hostData['host'] = "xxx.xxx.xxx.xxx"; // domain OR IP
hostData['id'] = "username"; // login account
hostData['pwd'] = "password"; // login password
hostData['secret_key'] = secretKeyPath;
hostData['title'] = "Title"; // title of tab
hostData['port'] = 22; // You set when you set the SSH port
hostData['encode'] = EncodingType.UTF8; // If encoding is other than the default value
hostData['newline_char'] = NewLine.CR; // If encoding is other than the default value

hostList.push(hostData);

// SSH への接続
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'];
  }
  // hostsに接続する。
  var conn = connect(hostList[i]['host'], ConnectionMethod.SSH2, ssh_port_num, encoding_type, newline_char, hostList[i]['id'], hostList[i]['pwd'], hostList[i]['title'], hostList[i]['secret_key']);
  // For some reason,If you do not sleep here, the command is not run
  sleep(0.5);

  // ログイン後にコマンドを実行する。
  if (hostList[i]['commands']) {
    if (hostList[i]['commands'].length > 0) {
      for (var j = 0; j < hostList[i]['commands'].length; j ++) {
        sendln(hostList[i]['commands'][j], conn);
      }
    }
  }
}

// SSH connection
function connect(host, method, port, encoding, newline_char, id, password, title, secret_key) {
  var param = new SSHTerminalParam(method, host, id, password);
  param.Caption = title;
  param.Port = port;
  param.Encoding = encoding;
  param.TransmitNL = newline_char;
  param.AuthType = AuthType.PublicKey;
  param.IdentityFile = secret_key;
  var connection = env.Connections.Open(param);
  return connection;
}

function sendln(s, connection) {
  connection.TransmitLn(s);
}

function sleep( T ){
  var d1 = new Date().getTime();
  var d2 = new Date().getTime();
  while( d2 < d1 + 1000 * T ) { // wait for T seconds
    d2=new Date().getTime();
  }
  return;
}
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?