Skip to content

Instantly share code, notes, and snippets.

@ryan5500
Created April 17, 2014 12:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryan5500/10978813 to your computer and use it in GitHub Desktop.
Save ryan5500/10978813 to your computer and use it in GitHub Desktop.
WAPM-APG300N / WAPM-AG300Nの設定コマンド入力を自動化するスクリプト
#!/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