Created
April 17, 2014 12:16
-
-
Save ryan5500/10978813 to your computer and use it in GitHub Desktop.
WAPM-APG300N / WAPM-AG300Nの設定コマンド入力を自動化するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Buffalo WAPM-APG300N / WAPM-AG300Nの設定を自動化するスクリプト | |
# sshでルーターに接続し、 コマンドを実行する。 | |
# | |
require 'net/ssh' | |
host = ARGV[0] # ホスト名 | |
password = ARGV[1] # アクセス用のパスワード | |
# 実行したいコマンドを以下に入力する | |
commands = [ | |
"show config ether", | |
"exit" | |
] | |
Net::SSH.start(host, 'root', password: password) do |ssh| | |
ssh.open_channel do |channel| | |
# shell requestでアクセスしない場合、 | |
# `*** load_command_config error`と表示され、ssh接続が終了するため | |
channel.send_channel_request "shell" do |chan, success| | |
abort "could not start user shell" unless success | |
commands.each do |command| | |
chan.send_data("#{command}\n") | |
end | |
end | |
# コマンドの実行結果を出力する | |
channel[:data] = "" | |
channel.on_data do |ch, data| | |
channel[:data] << data | |
end | |
channel.on_process do |ch| | |
if channel[:data] =~ /^.*?\n/ | |
puts $& | |
channel[:data] = $' | |
end | |
end | |
channel.on_close do |ch| | |
puts "session closed" | |
exit | |
end | |
end | |
channel.on_open_failed do |ch, code, desc| | |
abort "open failed" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment