All posts by Kenji

Rails How to execute task in seed


Here’s how I executed task in seeds.rb, in Rails.

The Rails 4 Way

Environment

  • Ruby 2.2.2p95
  • Rails 4.1.8
  • Rake 10.4.2

First, create a task with rails g task sample shot. The following code will be generated.

Now, we can execute the task with bundle exec rake sample:shot. The goal is to execute the task when bundle exec rake db:seed is executed. Maybe, it is the same way to execute task in rails code.

Solution

Add the code into seeds.rb, like below.

Environment (test, development, production) is passed, which is valid in rake db:seed. On the above, set 'value' to SOMETHING. It is like FEATURES in rake db:fixture:load FIXTURES=xxxx.


Show japanese in xterm


Japanese letter is sometimes shown in incorrect way on xterm. In case that you use ubuntu with Japanese font. Here, I introduce how I made it available to show Japanese letter. (The way is also available for uxterm.)

Edit Menu

Launching xterm with some option solves the problem. So, change xterm command on menu. If you use LXDE or Gnome etc., you can change menu with property window opened by right-clicking the menu. On the dialog, change the command to xterm -fwb Sans, then wide bold letter displayed in Sans font,

In my case, I use openbox and wanted to change “Terminal emulator” menu item, which open LXTerminal, to xterm, then followed the steps below.

Then I was ready to change menu and changed the execution command of “Terminal emulator” in ~/.config/openbox/menu.xml to xterm launching command with options.


CakePHP3 separate config file for each environment


I’ll write how to separate config file in CakePHP3 version 3.0.9. As for FuelPHP, you can add setting file into app/config/development, etc.

In the case when creating setting file in CakePHP3 for each environment, I’ll recommend you to create the directory config/development and put setting files in it, and modify configure loading code.

Modified Code

Add one line with Configure::load into config/bootstrap.php. In the following code, it’s forth line. Suppoted that $environment is set. You should define it somewhere.

At the forth line, it will merge setting file config/{$development}/app.php into config/app.php.

You can add any other conditional statement like if or switch.

Explanation

What does Configure do?

According to bootstrap.php, Configure::config and Configure::load seems to concern loading setting file. Let’s look at these functions.

Class Configure is defined at vendor/cakephp/cakephp/src/Core/Configure.php.

Configure::config

According to the comment, this method adds an engine. First parameter is the key to identify added engines, seconed one is the instance of the engine. You know, if you set the engine with the same name twice or more times, the setting is overwritten by last one.

bootstrap.php adds engine new PhpConfig() as default one, with Configure::config('default', new PhpConfig());.

Configure::load

The method loads setting with the engine set with Configure::config.

First parameter specify the file to be load. When you use /, it means directory separator.

Second parameter specify the engine to handle settings, which is set with Configure::config. The identify key of the engine is expressed as $name in Configure::config, but it’s $config in here. On specifying the engine unset, return false.

Third parameter defines to merge or to overwrite. I’ll set it true, because common setting in all environments should be written in one file.

So, you can merge setting file with Configure::load(filepath, 'default', true). You know I didn’t explain the detail like Hash::merge.

If load not existing file?

You can know what will occur when you try it. But let’s look at what procedure works. According to bootstrap.php and Configure::load, the setting file will be load with read method in PhpConfig. PhpConfig is defined at vendor/cakephp/cakephp/src/Core/Configure.php.

It says error will be occurred. The variable $config seems a relic on the past


When you forget ssh key passphrase


Have you forgotten ssh key passphrase before? I forgot it twice. 設定したパスフレーズではなぜか使えなかったり、パスフレーズをどこに控えたかを忘れてしまったり。 そんなときに、とても時間がかかりますがブルートフォースでパスワードを当てる方法があります。

使うもの

john the ripper
総当たりをするための候補を作るプログラム。 linux だとパッケージに入っていることが多いです。
ssh-privkey-crack.c
キーの検証を行うプログラム。 コンパイルできる環境が必要です。

Procedure

  1. Download ssh-privkey.crack.c from lusas project. (執筆時にはダウンロードできていたのですが、時点で同じようにダウンロードできるかはわかりません。)

  2. Compile ssh-privkey-crack.c. コンパイルの方法はプログラム内のコメントに書かれています。 私の環境では gcc -Wall -o ssh-privkey-crack ssh-privkey-crack.c -lssl -lcrypto でコンパイルできました。 コンパイル後のファイルには実行権限をつけておきます。

  3. john the ripper をダウンロードして設定ファイルを編集します。 In my environment (ubuntu), /etc/john/john.conf was the default configuration. それに追記する形で設定を追加しました。

    総当たりをする場合の設定です。 File の指定は存在する chr ファイル を指定し、 Extra に chr ファイル 外 の文字で使用する文字を記述します。 chr ファイル の作り方がいまいち把握できていないのでこのようにしました。 MinLen 、 MaxLen はパスフレーズの長さの範囲を指定します。 CharCount で使用する文字の総数を記述します。 今回は 数字と大文字、小文字なので 62 です。 chr ファイルは私の環境だと /usr/share/john/ にありました。

  4. コマンド john -stdout -incremental:Pattern1 | ./ssh-privkey-crack ssh-private-keyfile を実行します。 最後に “-v” をつけると詳細なアウトプットが出せます。 (実際のところ MinLen = 3, MaxLen = 10, CharCount = 62 だとまず終わらないです。)