φ(.. ) 備忘録
amazonアソシエイト

2021年10月11日月曜日

指定したフォルダでminttyを起動する

右クリックの送るメニューに、minttyを起動するメニューを追加することで、マウス操作で指定したフォルダをカレントディレクトリとしたminttyを起動する。
ネットワークフォルダ(UNCパス)にも対応。
メニューの作り方は以下。
任意のフォルダに「minttyを起動する.js」を作成する。スクリプトの内容は以下。
var dir=WScript.Arguments.Unnamed(0);
var fs = new ActiveXObject( "Scripting.FileSystemObject" );
if( fs.FolderExists(dir) ){
    var sh = new ActiveXObject( "WScript.Shell" );
    sh.CurrentDirectory = dir;
    sh.Run( "C:\\Cygwin\\bin\\mintty.exe" );
}
エクスプローラーで「shell:sendto」を開き、作成したスクリプトのショートカットを配置。ショートカットの名前がそのままメニューの名前になるので、「minttyを開く」に名前を変更。

minttyで開きたいフォルダを右クリックし、「送る」→「minttyを開く」を選択すると、指定フォルダでminttyが起動する。


hubotの文字化け対策

IRCボットのhubotはISO-2022-JPな環境で使うと文字化けする。なぜなら、hubotはUTF-8を前提として作られているから!

そのため、ZNC等のIRC proxyを介して接続するのが一般的だが、今回はhubotを直接いじって、文字化けを解決する手法を見つけたので紹介する。

#以前、irc.coffeeを改変して対応していたが、それより簡単にできる方法を見つけたので記事にする。
まずは、こちらの記事を参考にインストール。

次に、必要なモジュールをインストール。

$ cd ~/hubot
$ npm install jschardet
$ npm install iconv

次に、irc.jsのパッチを作成する。

$ cd ~/hubot/node_modules/irc/lib
$ cp irc.js irc.js.org
$ vi irc.js.patch
以下の内容をirc.js.patchとして保存。
--- irc.js.org  2021-10-06 21:44:28.692961900 +0900
+++ irc.js      2021-10-06 22:09:47.822961900 +0900
@@ -30,6 +30,9 @@ var CyclingPingTimer = require('./cyclin

 var lineDelimiter = new RegExp('\r\n|\r|\n')

+var Jschardet = require('jschardet');
+var Iconv = require('iconv').Iconv;
+
 function Client(server, nick, opt) {
     var self = this;
     self.opt = {
@@ -108,6 +111,11 @@ function Client(server, nick, opt) {
     }

     self.addListener('raw', function(message) {
+       for(i in message.args){
+            detectResult = Jschardet.detect(message.args[i]);
+           iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE');
+           message.args[i]=iconv.convert(message.args[i]).toString();
+       }
         var channels = [],
             channel,
             nick,
@@ -934,7 +942,10 @@ Client.prototype.send = function(command
         util.log('SEND: ' + args.join(' '));

     if (!this.conn.requestedDisconnect) {
-        this.conn.write(args.join(' ') + '\r\n');
+       iconv = new Iconv('UTF-8','ISO-2022-JP//TRANSLIT//IGNORE')
+       str = iconv.convert(args.join(' ')).toString()
+//      this.conn.write(args.join(' ') + '\r\n');
+        this.conn.write(str + '\r\n');
     }
 };
次に、irc.jsにパッチをあてる。
$ patch -p1 irc.js < irc.js.patch
これでhubotを再起動すれば、文字化けは解消しているはず。 notice発言や日本語チャンネル名も問題なくjoin可能!

2021年9月26日日曜日

既存UbuntuサーバをWindows10のWSL2に置換する!(其の六)

 其の六:WSL2にapache2とMySQL、phpmyadminをインストールする

今回は、WSL2のUbuntu 20.04 LTSに、apache2(ウェブサーバ)とMySQL、phpmyadminをインストールする。非常に簡単だった。


■apache2(ウェブサーバ)のインストール

パッケージをインストールして、起動用のバッチを編集。

$ sudo apt install apache2
$ vi wsl2_apach2_start.bat
起動用のバッチの内容は以下。Windows10側でportproxyの設定とWSL側でapache2を起動する。なぜかlistenport=80でアクセスできなかったので、listenport=3000で実施する。
@echo off
for /f "usebackq" %%t in (`wsl -d Ubuntu-20.04 exec hostname -I`) do set IP=%%t
netsh.exe interface portproxy delete v4tov4 listenport=3000
netsh.exe interface portproxy add v4tov4 listenport=3000 connectaddress=%IP% connectport=80
wsl -d Ubuntu-20.04 -u root service apache2 start
上記のバッチを管理者権限で実行すると、localhost:3000でアクセスできる。

■MySQLのインストール

パッケージをインストール。
$ sudo apt install mysql-server
次に、mysqlを起動し、rootのパスワード設定。
$ sudo service mysql start
$ sudo mysql_secure_installation
起動用バッチ(wsl2_mysql_start.bat)を作成。内容は以下。
@echo off
wsl -d Ubuntu-20.04 -u root service mysql start
PC再起動時は上記起動用バッチを管理者権限で実行すれば、MySQLを起動できる。

■phpmyadminインストール

パッケージをインストール。
$ sudo apt install phpmyadmin
コンフィグレーションが実行されるので、質問に答える。Webサーバは先ほどインストールしたapache2を選択しOK。


あとは、全部YesでOK(途中でパスワードを聞かれるので、パスワードも入力)。
コンフィグレーションを正しく設定できるとapache2側の/etc/apache2/conf-availableと/etc/apache2/conf-enabledにphpmyadmin設定が自動で設定されるので、apache2を再起動。
$ sudo service apache2 restart
apache2再起動後、localhost:3000/phpmyadminでアクセスできる。ユーザ名はphpmyadmin、パスワードはコンフィグレーション時に設定したもの。

2021年9月24日金曜日

Windows 10の操作をJScriptで拡張する

Windows 10の標準で搭載されているスクリプト(JScript)でWindows10の操作を拡張し、もっと便利に使う。

スクリプト言語をJScriptにしたのは、とくにツール等を追加することなく使用できる点や、もともとjavascriptを趣味で書いたことがあり、言語仕様がある程度わかるため。


■『送る』メニューに『パスをコピー』を追加する。

マウス右クリックのメニューにマウスで選択したファイルやフォルダの絶対パスをクリップボードにコピーする機能を追加する。

まずは、スクリプト(パスをコピー.js)を適当なフォルダに作成。スクリプトの内容は以下。

var args = WScript.Arguments;
var str=""
for(var i=0;i<args.length;i++){
  str+=args.Unnamed(i)+"\n";
}
var clip = WScript.CreateObject('WScript.Shell').Exec('clip');
clip.StdIn.Write(str);
clip.StdIn.Close();
このスクリプトのショートカットを作成し、『shell:sento』フォルダに配置。
使い方はパスをコピーしたいファイルやフォルダを選択(複数可)し、送るメニューのパスをコピーを選択。

クリップボードにパスがコピーされるので、Ctrl+vで貼り付け。すると、以下のようにファイルの絶対パスを張り付けることができる。
チャット等で、相手に共有ファイルの在りかを伝えるときとかに便利。

■クリップボードの内容をソートする。
エクセルとかのデータを一旦クリップボードにコピーしたあと、スクリプトを起動することで、クリップボード内でデータをソートする。
まずは、スクリプト(クリップボードをソート.js)を適当なフォルダに作成。スクリプトの内容は以下。
var clip = WScript.CreateObject("htmlfile").parentWindow.clipboardData;
var str=clip.getData("text");
var tbl=str.split("\n");
tbl.sort();
var clip = WScript.CreateObject('WScript.Shell').Exec('clip');
clip.StdIn.Write(tbl.join("\n"));
clip.StdIn.Close();
ソートしたいデータをクリップボードにコピー。
先ほど作成したスクリプト(クリップボードをソート.js)を「開く」またはダブルクリック。

Ctrl+vでソート済みのデータを張り付けることができる。
エクセルだとあまり活用する機会はないけれど、テキストデータを扱う場合には結構便利。

■クリップボードの内容をユニークソートする。
エクセルとかのデータを一旦クリップボードにコピーしたあと、スクリプトを起動することで、クリップボード内でデータをユニークソートする。
まずは、スクリプト(クリップボードをuniqソート.js)を適当なフォルダに作成。スクリプトの内容は以下。
var clip = WScript.CreateObject("htmlfile").parentWindow.clipboardData;
var str=clip.getData("text");
var tbl=str.split("\n");
var utbl = new Array();
for(var i=0;i%lt;tbl.length;i++){
  utbl[tbl[i]]="";
}
var stbl=new Array();
for(key in utbl){
  stbl.push(key);  
}
stbl.sort();
var clip = WScript.CreateObject('WScript.Shell').Exec('clip');
clip.StdIn.Write(stbl.join("\n"));
clip.StdIn.Close();
ユニークソートしたいデータをクリップボードにコピー。
先ほど作成したスクリプト(クリップボードをuniqソート.js)を「開く」またはダブルクリック。
Ctrl+vでユニークソート済みのデータを張り付けることができる。
クリップボードのデータのうち、重複しているデータを排除し、ソートした結果を張り付けることが可能。これは結構便利でしばしば使う。


2021年9月23日木曜日

既存UbuntuサーバをWindows10のWSL2に置換する!(其の五)

其の五:hubotの文字化けを直す

前回、hubotが無事インストールできたので、文字化けを対策する。

hubotで扱う文字コードはUTF-8が前提となっており、会社で使っているISO-2022-JPのチャットサーバーだと、文字化けを起こしてしまう。

以前perlのnkfモジュールを用いて対策を行ったことがあったが、coffeeスクリプトで対策できることが分かったので、そちらで対策する。

まずは、hubotをインストールしたディレクトリに移動して、必要なモジュール(jschardet、iconv)を追加インストールする。

$ cd ~/hubot
$ npm install jschardet
$ npm install iconv
次に、irc.coffeeに修正を加える。
$ cd ~/hubot/node_modules/hubot-irc/src
$ cp irc.coffee irc.coffee.org
$ vi irc.coffee
修正内容(diffイメージ)は以下で、最初にrequireで追加したモジュールをロード。
日本語のチャンネル名が指定された場合に備えて、チャンネル名をUTF8からISO-2022-JPに変更する処理を追加。
sendメソッドとnoticeメソッドに文字コードをUTF-8からISO-2022-JPに変更する処理を追加。messageやchannelを受け取るbotListener(message,notice等)に受け取った文字をUTF-8に変更する処理を追加。
ちなみに、iconv生成時のパラメータで指定している『TRANSLIT//IGNORE』はコード変換失敗時にエラーを無視するためのおまじないみたい。
 # Irc library
 Irc = require 'irc'

+# for ISO-2022-JP
+Jschardet = require 'jschardet'
+Iconv = (require 'iconv').Iconv
+
 class IrcBot extends Adapter
   send: (envelope, strings...) ->
     # Use @notice if SEND_NOTICE_MODE is set
@@ -15,6 +19,8 @@
       return console.log "ERROR: Not sure who to send to. envelope=", envelope

     for str in strings
+      iconv = new Iconv('UTF-8','ISO-2022-JP//TRANSLIT//IGNORE')
+      str = iconv.convert(str).toString()
       @bot.say target, str

   topic: (envelope, strings...) ->
@@ -52,6 +58,8 @@
       if not str?
         continue

+      iconv = new Iconv('UTF-8','ISO-2022-JP//TRANSLIT//IGNORE')
+      str = iconv.convert(str).toString()
       @bot.notice target, str

   reply: (envelope, strings...) ->
@@ -131,11 +139,15 @@

     do @checkCanStart

+    iconv = new Iconv('UTF-8','ISO-2022-JP//TRANSLIT//IGNORE')
+    roomsValue = iconv.convert(process.env.HUBOT_IRC_ROOMS).toString()
+
     options =
       nick:     process.env.HUBOT_IRC_NICK or @robot.name
       realName: process.env.HUBOT_IRC_REALNAME
       port:     process.env.HUBOT_IRC_PORT
-      rooms:    process.env.HUBOT_IRC_ROOMS.split(",")
+#      rooms:    process.env.HUBOT_IRC_ROOMS.split(",")
+      rooms:    roomsValue.split(",")
       ignoreUsers: process.env.HUBOT_IRC_IGNORE_USERS?.split(",") or []
       server:   process.env.HUBOT_IRC_SERVER
       password: process.env.HUBOT_IRC_PASSWORD
@@ -189,15 +201,27 @@

     if options.connectCommand?
       bot.addListener 'registered', (message) ->
+        detectResult = Jschardet.detect(message)
+        iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+        message = iconv.convert(message).toString()
+
         # The 'registered' event is fired when you are connected to the server
         strings = options.connectCommand.split " "
         self.command strings.shift(), strings...

     bot.addListener 'names', (channel, nicks) ->
+      detectResult = Jschardet.detect(channel)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      channel = iconv.convert(channel).toString()
+
       for nick of nicks
         self.createUser channel, nick

     bot.addListener 'notice', (from, to, message) ->
+      detectResult = Jschardet.detect(message)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      message = iconv.convert(message).toString()
+
       if from in options.ignoreUsers
         console.log('Ignoring user: %s', from)
         # we'll ignore this message if it's from someone we want to ignore
@@ -209,6 +233,10 @@
       self.receive new TextMessage(user, message)

     bot.addListener 'message', (from, to, message) ->
+      detectResult = Jschardet.detect(message)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      message = iconv.convert(message).toString()
+
       if options.nick.toLowerCase() == to.toLowerCase()
         # this is a private message, let the 'pm' listener handle it
         return
@@ -231,6 +259,10 @@
       self.receive new TextMessage(user, message)

     bot.addListener 'action', (from, to, message) ->
+      detectResult = Jschardet.detect(message)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      message = iconv.convert(message).toString()
+
       console.log " * From #{from} to #{to}: #{message}"

       if from in options.ignoreUsers
@@ -247,9 +279,17 @@
       self.receive new TextMessage(user, message)

     bot.addListener 'error', (message) ->
+      detectResult = Jschardet.detect(message)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      message = iconv.convert(message).toString()
+
       console.error('ERROR: %s: %s', message.command, message.args.join(' '))

     bot.addListener 'pm', (nick, message) ->
+      detectResult = Jschardet.detect(message)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      message = iconv.convert(message).toString()
+
       console.log('Got private message from %s: %s', nick, message)

       if process.env.HUBOT_IRC_PRIVATE
@@ -267,21 +307,37 @@
       self.receive new TextMessage({reply_to: nick, name: nick}, message)

     bot.addListener 'join', (channel, who) ->
+      detectResult = Jschardet.detect(channel)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      channel = iconv.convert(channel).toString()
+
       console.log('%s has joined %s', who, channel)
       user = self.createUser channel, who
       user.room = channel
       self.receive new EnterMessage(user)

     bot.addListener 'part', (channel, who, reason) ->
+      detectResult = Jschardet.detect(channel)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      channel = iconv.convert(channel).toString()
+
       console.log('%s has left %s: %s', who, channel, reason)
       user = self.createUser '', who
       user.room = channel
       self.receive new LeaveMessage(user)

     bot.addListener 'kick', (channel, who, _by, reason) ->
+      detectResult = Jschardet.detect(channel)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      channel = iconv.convert(channel).toString()
+
       console.log('%s was kicked from %s by %s: %s', who, channel, _by, reason)

     bot.addListener 'invite', (channel, from) ->
+      detectResult = Jschardet.detect(channel)
+      iconv = new Iconv(detectResult.encoding,'UTF-8//TRANSLIT//IGNORE')
+      channel = iconv.convert(channel).toString()
+
       console.log('%s invited you to join %s', from, channel)

       if from in options.ignoreUsers

本来なら、環境変数もしくはコマンドパラメータで変換する文字コードを指定すべきだが、今回はそこまでやってない。
上記修正が正しく動作するか確認するために、hubot-scriptsのhttp-post-say.coffeeを使ったため、それの設定方法も残しておく。
まず、hubotインストールディレクトリに移動して、hubot-scripts.jsonを編集。
$ cd ~/hubot
$ vi hubot-scripts.json
hubot-scripts.jsonの内容は以下。他に追加したいスクリプトがあればカンマ(,)区切りで追加すればよい。
["http-post-say.coffee"]
hubotの待ち受けポートは、デフォルト8080になっている。変更する場合には、以下のようにhubot起動スクリプトに環境変数PORTの設定処理を追加し、起動すればよい。
$ vi runbot.sh
#!/bin/bash
# hubot http port setting
export PORT=9999
# runhubot
export HUBOT_IRC_NICK="hubot"
export HUBOT_IRC_ROOMS="#test01,#test02"
export HUBOT_IRC_SERVER="localhost"
#export HUBOT_IRC_PASSWORD="hoge"
bin/hubot -a irc
$ .runbot.sh 
http-post-say.coffeeを使って、http経由でhubotに発言させるにはcurlコマンド等で以下のようにすればよい。
$ curl -X POST http://localhost:9999/hubot/say -d message=文字化けた? -d room='#test01'
ISO-2022-JPのチャットサーバって、世の中一般では、レアなんですかね。。。うちの会社だけなのかなぁ。。。。

あと、おまけで、hubotの発言をnoticeにする方法がわかった。すごく簡単で、起動時に環境変数HUBOT_IRC_SEND_NOTICE_MODEにtrueを設定するだけだった。イメージは以下。
#!/bin/bash
# hubot http port setting
export PORT=9999
# runhubot
export HUBOT_IRC_NICK="hubot"
export HUBOT_IRC_ROOMS="#test01,#test02"
export HUBOT_IRC_SERVER="localhost"
#export HUBOT_IRC_PASSWORD="hoge"
export HUBOT_IRC_SEND_NOTICE_MODE="true"
bin/hubot -a irc

fvwm風Autohotkeyスクリプト更新!

自宅時間が増え、自宅パソコンをいじる機会が多くなったので、これを機にパソコンの作業環境を整える。

まずは、ウィンドウ操作関連のショートカットの整備する。以前にAutohotkeyのスクリプトを作成してから、かなりの期間がたっており、状況も変わっていると思うので、スクリプトを作りなおしてみた。

スクリプトの内容は以下。

;===================================================================
; アクティブウィンドウのあるモニタを情報を返却する。
;===================================================================
GetCurrentMonitor(x,y,w,h){
  SysGet, MonitorCount, MonitorCount
  Loop, %MonitorCount%
  {
    SysGet, MonitorWorkArea, MonitorWorkArea, %A_Index%
    if(x+w/2 >= MonitorWorkAreaLeft AND x+w/2 <= MonitorWorkAreaRight){
      if(y >= MonitorWorkAreaTop AND y <= MonitorWorkAreaBottom){
        ;モニタの左上とセンタの座標を求める
	if(MonitorWorkAreaLeft<MonitorWorkAreaRight){
	  MonitorW:=MonitorWorkAreaRight-MonitorWorkAreaLeft
          CenterX:=MonitorWorkAreaLeft+(MonitorWorkAreaRight-MonitorWorkAreaLeft)/2
	}
        else{
	  MonitorW:=Abs(MonitorWorkAreaLeft)-Abs(MonitorWorkAreaRight)
          CenterX:=MonitorWorkAreaLeft+(Abs(MonitorWorkAreaLeft)-Abs(MonitorWorkAreaRight))/2
	}
	if(MonitorWorkAreaTop<MonitorWorkAreaBottom){
	  MonitorH:=MonitorWorkAreaBottom-MonitorWorkAreaTop
          CenterY:=MonitorWorkAreaTop+(MonitorWorkAreaBottom-MonitorWorkAreaTop)/2
	}
        else{
	  MonitorH:=Abs(MonitorWorkAreaTop)-Abs(MonitorWorkAreaBottom)
          CenterY:=MonitorWorkAreaTop+(Abs(MonitorWorkAreaTop)-Abs(MonitorWorkAreaBottom))/2
	}
        Return % MonitorWorkAreaLeft . " " . MonitorWorkAreaTop . " " . MonitorW . " " . MonitorH . " " . CenterX . " " . CenterY
      }
    }
    Exit
  }
}
;===================================================================
; fvwm風ウィンドウ制御(マルチモニタ対応)
;===================================================================
^+!i::
  WinGet,id,ID,A
  if(x%id%=""){
    WinGetPos,x%id%,y%id%,w%id%,h%id%,A
    String := GetCurrentMonitor(x%id%,y%id%,w%id%,h%id%)
    Array := StrSplit(String, A_Space)
    MonitorX := Array[1]
    MonitorY := Array[2]
    MonitorW := Array[3]
    MonitorH := Array[4]
    CenterX := Array[5]
    CenterY := Array[6]
    if(x%id%+w%id%/2 < CenterX){
      ;左
      WinMove,ahk_id %id%,,MonitorX,MonitorY+4
        ,MonitorW/2,MonitorH
    }
    Else{
      ;右
      WinMove,ahk_id %id%,,CenterX,MonitorY+4
        ,MonitorW/2,MonitorH
    }
  }
  Else{
    ;元のポジションに戻してウィンドウを忘れる
    WinMove,ahk_id %id%,,x%id%,y%id%,w%id%,h%id%
    x%id%=
    y%id%=
    w%id%=
    y%id%=
  }
  return
^+!k::
  WinGet,id,ID,A
  if(x%id%=""){
    WinGetPos,x%id%,y%id%,w%id%,h%id%,A
    String := GetCurrentMonitor(x%id%,y%id%,w%id%,h%id%)
    Array := StrSplit(String, A_Space)
    MonitorX := Array[1]
    MonitorY := Array[2]
    MonitorW := Array[3]
    MonitorH := Array[4]
    CenterX := Array[5]
    CenterY := Array[6]
    if(y%id%+h%id%/2 < CenterY){
      ;上
      WinMove,ahk_id %id%,,MonitorX,MonitorY+4
	,MonitorW,MonitorH/2
    }
    Else{
      ;下
      WinMove,ahk_id %id%,,MonitorX,CenterY+4
	,MonitorW,MonitorH/2
    }
  }
  Else{
    ;元のポジションに戻してウィンドウを忘れる
    WinMove,ahk_id %id%,,x%id%,y%id%,w%id%,h%id%
    x%id%=
    y%id%=
    w%id%=
    y%id%=
  }
  return
^+!j::
  WinGet,id,ID,A
  if(x%id%=""){
    WinGetPos,x%id%,y%id%,w%id%,h%id%,A
    String := GetCurrentMonitor(x%id%,y%id%,w%id%,h%id%)
    Array := StrSplit(String, A_Space)
    MonitorX := Array[1]
    MonitorY := Array[2]
    MonitorW := Array[3]
    MonitorH := Array[4]
    CenterX := Array[5]
    CenterY := Array[6]
    WinMove,ahk_id %id%,,MonitorX,MonitorY+4
	,MonitorW,MonitorH
  }
  Else{
    ;元のポジションに戻してウィンドウを忘れる
    WinMove,ahk_id %id%,,x%id%,y%id%,w%id%,h%id%
    x%id%=
    y%id%=
    w%id%=
    y%id%=
  }
  return
^+!Left::
  WinGet,id,ID,A
  if(x%id%=""){
    WinGetPos,x%id%,y%id%,w%id%,h%id%,A
  }
  String := GetCurrentMonitor(x%id%,y%id%,w%id%,h%id%)
  Array := StrSplit(String, A_Space)
  MonitorX := Array[1]
  MonitorY := Array[2]
  MonitorW := Array[3]
  MonitorH := Array[4]
  CenterX := Array[5]
  CenterY := Array[6]
  ;左
  WinMove,ahk_id %id%,,MonitorX,MonitorY+4
    ,MonitorW/2,MonitorH
  return
^+!Right::
  WinGet,id,ID,A
  if(x%id%=""){
    WinGetPos,x%id%,y%id%,w%id%,h%id%,A
  }
  String := GetCurrentMonitor(x%id%,y%id%,w%id%,h%id%)
  Array := StrSplit(String, A_Space)
  MonitorX := Array[1]
  MonitorY := Array[2]
  MonitorW := Array[3]
  MonitorH := Array[4]
  CenterX := Array[5]
  CenterY := Array[6]
  ;右
  WinMove,ahk_id %id%,,CenterX,MonitorY+4
    ,MonitorW/2,MonitorH
  return
^+!UP::
  WinGet,id,ID,A
  if(x%id%=""){
    WinGetPos,x%id%,y%id%,w%id%,h%id%,A
  }
  String := GetCurrentMonitor(x%id%,y%id%,w%id%,h%id%)
  Array := StrSplit(String, A_Space)
  MonitorX := Array[1]
  MonitorY := Array[2]
  MonitorW := Array[3]
  MonitorH := Array[4]
  CenterX := Array[5]
  CenterY := Array[6]
  ;上
  WinMove,ahk_id %id%,,MonitorX,MonitorY+4
    ,MonitorW,MonitorH/2
  return
^+!DOWN::
  WinGet,id,ID,A
  if(x%id%=""){
    WinGetPos,x%id%,y%id%,w%id%,h%id%,A
  }
  String := GetCurrentMonitor(x%id%,y%id%,w%id%,h%id%)
  Array := StrSplit(String, A_Space)
  MonitorX := Array[1]
  MonitorY := Array[2]
  MonitorW := Array[3]
  MonitorH := Array[4]
  CenterX := Array[5]
  CenterY := Array[6]
  WinMove,ahk_id %id%,,MonitorX,CenterY+4
    ,MonitorW,MonitorH/2
  return
;拡張デスクトップの切り替え
^Left::#^Left
^Right::#^Right
^Up::#^Left
^Down::#^Right

以前のスクリプトでは画面サイズをハードコーディングしていたが、自動取得するAPIを発見したため、それに変更してみた。
あと、ウィンドウの移動とサイズ変更方法をWindowsの「Winキー+矢印」っぽくしてみた。使用方法は以下。

Ctrl+Shift+Alt+iウィンドウ縦拡大
Ctrl+Shift+Alt+kウィンドウ横拡大
Ctrl+Shift+Alt+jウィンドウ全画面
Ctrl+Shift+Alt+←ウィンドウ左寄せ
Ctrl+Shift+Alt+→ウィンドウ右寄せ
Ctrl+Shift+Alt+↑ウィンドウ上寄せ
Ctrl+Shift+Alt+↓ウィンドウ下寄せ
Ctrl+←仮想デスクトップ切り替え左
Ctrl+→仮想デスクトップ切り替え右
Ctrl+↑仮想デスクトップ切り替え左
Ctrl+↓仮想デスクトップ切り替え右


拡大中のウィンドウ選択時に特定のショートカットを入力すると元のサイズと位置に戻る。
あと、拡張デスクトップの切り替えをCtrl+矢印でできようにホットキーを変更している。
マルチモニターには一応対応させてつもり。誤動作するかもしれません。ご注意を。スクリプトの文字コードはUTF-8にしてください。



2021年9月22日水曜日

既存UbuntuサーバをWindows10のWSL2に置換する!(其の四)

   其の四:wsl2にhubotをインストールする

いよいよ本格的な移行作業として、wsl2のUbuntu 20.04 LTSにhubotをインストールする。いろんなサイトを参考にインストールしてみたがうまくいかず、かなり試行錯誤を繰り返したが、ようやくIRCチャットルームにbotをログインさせるところまで来たので、その手順を紹介する。

まず、hubotに必要なパッケージをインストールする。

$ sudo apt-get install nodejs npm coffeescript libicu-dev
次に、hubotをgit clone後、任意でディレクトリ名を変更し、ディレクトリを移動する。
$ git clone https://github.com/jgable/hubot-irc-runnable
$ mv hubot-irc-runnable hubot
$ cd hubot
次に、npmインストール実行。
$ npm install
次に、hubot-scripts.jsonを一応バックアップして編集。hubot-scripts.jsonうち、使用しないスクリプトを削除する。
$ cp hubot-scripts.json hubot-scripts.json.org
$ vi hubot-scripts.json
今回は全部つかわないので、以下の内容に変更。
[]
次に起動スクリプト(runbot.sh)を編集。
$ vi runbot.sh
内容は以下。ニックネームとかJOINするチャンネルとかは適当に変えてください。尚、同一WSL2内のチャットサーバに接続する場合には、以下設定(localhost)で接続できた。redis brainは今回使わないので起動しているところをコメントアウト。
#!/usr/bin/env sh

# Set Environment Variables
export HUBOT_IRC_NICK=hubot
export HUBOT_IRC_SERVER=localhost
export HUBOT_IRC_ROOMS="#test01,#test02"

# Using SSL?
#export HUBOT_IRC_PORT=6697
#export HUBOT_IRC_USESSL=true
#export HUBOT_IRC_SERVER_FAKE_SSL=true

# Server password?
#export HUBOT_IRC_PASSWORD=password

# Don't let hubot flood the room.
export HUBOT_IRC_UNFLOOD=true

# Output environment variables
echo HUBOT_IRC_NICK=$HUBOT_IRC_NICK
echo HUBOT_IRC_SERVER=$HUBOT_IRC_SERVER
echo HUBOT_IRC_ROOMS=$HUBOT_IRC_ROOMS

#echo HUBOT_IRC_PORT=$HUBOT_IRC_PORT
#echo HUBOT_IRC_USESSL=$HUBOT_IRC_USESSL
#echo HUBOT_IRC_SERVER_FAKE_SSL=$HUBOT_IRC_SERVER_FAKE_SSL
#echo HUBOT_IRC_PASSWORD=$HUBOT_IRC_PASSWORD

# Start the redis brain
#echo ""
#echo "Starting Redis Server"
#/usr/local/bin/redis-server > /dev/null &

echo ""
echo "Starting bot"
./bin/hubot -a irc

実行権を付与。
  $ chmod 755 runbot.sh
スクリプトを起動。正常に接続できると以下の応答がコンソールに出力される。
hubot has joined #test01
hubot has joined #test02
以上でインストールと起動は完了。ただし、hubotの文字コードはUTF-8が前提となっているため、ISO-2022-JP(Shift-jis)が前提のサーバだと、文字化けする。次回はこの文字化けを解消する。

既存UbuntuサーバをWindows10のWSL2に置換する!(其の三)

  其の三:wsl2にIRCチャットサーバをインストールする

hubotのwsl2移行のために、テスト用のIRCチャットサーバをwsl2で立ち上げる。手順は非常に簡単。

まず、wsl2のUbuntuにngircdをインストールする。

$ sudo apt install ngircd

次に、portproxyの設定とUbuntuのngircdサービスを起動するため、以下のバッチを作成する。バッチでは、wsl側のUbuntuが使用しているIPアドレスをwslコマンドで取得し、変数に設定。それを使って、portproxyを設定後、root権限でngircdサービスを起動する。

@echo off
for /f "usebackq" %%t in (`wsl -d Ubuntu-20.04 exec hostname -I`) do set IP=%%t
netsh.exe interface portproxy delete v4tov4 listenport=6667
netsh.exe interface portproxy add v4tov4 listenport=6667 connectaddress=%IP%
wsl -d Ubuntu-20.04 -u root service ngircd start

上記バッチをC:\Users\ユーザ名\tools\wsl\wsl2_ngircd_start.batとかに保存し、管理者権限で実行する。

これで、limechatとかでサーバをlocalhost:6667でつなげば、チャットが使用できる。

外部からのアクセスを許可する場合には、Windows Defender ファイアウォールの「受信の規則」でTCPポート6667番を許可に設定する必要があります。ご注意を!

 

2021年9月21日火曜日

既存UbuntuサーバをWindows10のWSL2に置換する!(其の二)

 其の二:wsl2にCygwinからsshログインする

まず、wsl2のUbuntuにopensshをインストールする。

$ sudo apt install openssh-server

次に、以下コマンドで鍵を作成する。

$ sudo ssh-keygen -A

次に、/etc/ssh/sshd_configのPasswordAuthenticationをyesに変更する。これをやらないと『Permission denied(publickey)』で怒られる。

PasswordAuthentication yes

次に、portproxyの設定とUbuntuのsshdサービスを起動するため、以下のバッチを作成する。バッチでは、wsl側のUbuntuが使用しているIPアドレスをwslコマンドで取得し、変数に設定。それを使って、portproxyを設定後、root権限でsshサービスを起動する。

@echo off
for /f "usebackq" %%t in (`wsl -d Ubuntu-20.04 exec hostname -I`) do set IP=%%t
netsh.exe interface portproxy delete v4tov4 listenport=22
netsh.exe interface portproxy add v4tov4 listenport=22 connectaddress=%IP%
wsl -d Ubuntu-20.04 -u root service ssh start

上記バッチをC:\Users\ユーザ名\tools\wsl\wsl2_ssh_start.batとかに保存し、管理者権限で実行する。

以上で、Cygwinからwslにsshログインできるようなる。
Windows起動時にこのバッチを起動するようタスクスケジューラに設定すれば、自動起動できると思う。
外部からのアクセスを許可する場合には、Windows Defender ファイアウォールの「受信の規則」でTCPポート22番を許可に設定する必要があります。ご注意を!






Windows 10 に Cygwinインストール

久しぶりにWindows 10にセットアップしたCygwinをminttyで開いたら、不要なターミナル画面が2画面くらいでてきてビックリ!原因調べるのめんどくさいので、再インストールすることにした。

まず、Cygwinには64bit版と32bit版の二種類のインストーラーが用意されている。普通に考えたら64bit版を選択したくなるが、過去の投稿で書いた通り、一部機能(perlのOLE)が64bit版で動かないことがあったため、32bit版を選択する。今は治っているかもしれないが・・・

インストール手順は以下。

  1. 古いCygwinのインストールフォルダを念のためリネーム。(C:\Cygwin -> C:\Cygwin.bak
  2. 新しいCygwinフォルダ(C:\Cygwin)を作成し、このインストーラをダウンロードしてCygwinフォルダに配置。
  3. インストーラを実行。Install from Internetを選択し次へクリック。
  4. Root Drectoryがインストールフォルダであることを確認し、次へクリック。
  5. 次へクリック
  6. Use System Proxy Settingsを選択して次へ
  7. 近そうなDownload Sites(日本ならjp)を選択し次へ
  8. パッケージを追加したい場合は、「New」欄のプルタブをクリックして、Skipとなっているところをインストールするバージョンを選択し、次へをクリック。私は、gcc,make,perl,tcsh,minttyを追加した。
  9. 完了クリック
  10. Cygwin Terminalを起動すると、各種デフォルト設定ファイルがhome配下に自動でコピーされる。このあと古いCygwinにある各種設定ファイル(.bashrcや.tcshrc)を新しいCygwinのhome配下にコピー。これで移行完了。動作が問題ないようなら古いCygwinは消してしまおう。
以上で、インストール完了。ちなみに、Windowsのユーザー環境変数に以下を追加するとCygwin Terminal起動時のShellを変更できる。
 変数名=「SHELL」 値=「シェルの在りか 例)/bin/tcsh」


既存UbuntuサーバをWindows10のWSL2に置換する!(其の一)

其の一:WSL2のUbuntu 20.04 LTSをインストール

会社で使っている既存Ubuntuサーバの老朽化が心配・・・。Ubuntuサーバと言っても若手の転勤で不要となったデスクトップPCにUbuntuをインストールしただけの激安サーバである。ただし、もう7年くらい不具合なく常時稼働(ビル停電以外は常時ON)している。優秀。FUJITSU最高。

このサーバでは全文検索ツールとIRCチャットのbot(hubot)が動作しているため、故障に備えて、別なPCにあらかじめ環境を構築していつでも移行できるように作業を進めたい。

移行先は、別PCにUbuntuを入れるもしくは、仮想化マシンにする等あるが、今回はWindows10のWSL2に構築してみる。 

まず、WSL2のインストール。

  1. Windows->設定->プログラムと機能->Windowsの機能の有効化または無効化で『Linux用Windowsサブシステム』と『仮想マシンプラットフォーム』をチェックし再起動。
  2. Linux カーネル更新プログラム パッケージをダウンロードし、インストールする。https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
  3. Windows PowerShellを起動し、『wsl --set-default-version 2』を実行し、WSL 2 を既定のバージョンとして設定する。
  4. MicroSoft Store からUbuntu 20.04 LTSをインストール。https://www.microsoft.com/store/apps/9n6svws3rx71
  5. スタートメニューからUbuntu 20.04 LTSを起動し、ユーザとパスワード設定。
  6. Windows PowerShellで『wsl -l -v』WSLの状態確認。STATEがRunning、VERSIONが2となっていればOK。
以上、これでWSL2のインストールは完了。詳しい手順は、https://docs.microsoft.com/ja-jp/windows/wsl/install-win10に書いてある。