木曜日, 6月 27, 2013

[misc] 10年ぶりに自分便利ツールをバージョンアップ

とある作業で必要になったので、10年ぶり位に自分便利ツールをバージョンアップ。

httpサーバのステータスコードを確認するツールだ。
Proxy経由や HTTPS対応だと telnet じゃできないので ruby で書いて。
ブラウザ立ち上げるのが面倒だったり、User-Agentを書き換えるのが面倒だったり、GUIが使えない環境だったり踏み台サーバ経由だった利したときにHTTPステータス確認などに使っております。
まぁ、誰でもかけるレベルなのですが、自分備忘録として。

使うときは、ちゃんと改行位置合わせて使ってくだされ。

---------
#! /usr/bin/ruby
#
# HTTP Port Checkr URI version
# Rev2.0
# Auther: MYutaka
# Date:2004/11/8
# Modify:2013/6/27
#

require 'uri'
require 'socket'
require 'net/http'
require 'net/https'
require 'openssl/ssl.rb'
require 'openssl/x509.rb'


#
# 引数の処理
#

def usage
  STDERR.print "usage: #{$0} -vv -h IPADDRESS -path [PATH] -user-agent [User-Agent] [-https] [-s SOCKET] [-proxy IPADDRESS:SOCKET] | [-help]  \n"
end

while ARGV[0]
  case ARGV.shift
  when '-h'
    ipaddress = ARGV.shift
  when '-https'
    sslflug = 1
    socknum = 443
  when '-s'
    socknum = ARGV.shift
  when '-path'
    urlpath = ARGV.shift
  when '-user-agent'
    useragent = ARGV.shift
  when '-proxy'
    proxyflug = 1
    Aproxy = ARGV.shift.split(/\:/)
    proxyip = Aproxy[0]
    proxyport = Aproxy[1]
  when '-help'
    usage
    exit
  when '-vv'
    descriptionFlug = 1
  else
    usage
    exit 1
  end
end

if ipaddress.nil?
    usage
    exit 1
end

if socknum.nil?
 socknum = 80
end

if urlpath.nil?
 urlpath = "/"
end


#
# Main
#

if proxyflug != 1
    begin
        sockstat = TCPSocket.open(ipaddress,socknum)
    rescue
    end

    if sockstat.nil?
        print "Socket Not Open. Fail\n\n"
        exit
    end
end

http = Net::HTTP::Proxy(proxyip, proxyport).new(ipaddress,socknum)
if sslflug == 1
    print "HTTPS Negosiation Now. " ,"\n"
    print "Please Wait fiew minits......... " ,"\n"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

if useragent.nil?
   http.start { http.request_get(urlpath) { |http| }}
    if descriptionFlug == 1
#        If you don't need display HTML responce code, please comment-out of the following line.
        print "--------------------- Response HTML Code --------------------------\n"
        print http.body
        print "--------------------- Response HTML Code End ----------------------\n"
    end
else
   http.start { http.request_get(urlpath,'User-Agent' => useragent) { |http| }}
    if descriptionFlug == 1
#        If you don't need display HTML responce code, please comment-out of the following line.
        print "--------------------- Response HTML Code --------------------------\n"
        print http.body
        print "--------------------- Response HTML Code End ----------------------\n"
    end
end

case http
    when Net::HTTPUnknownResponse
        then puts "HTTP Status UnknownResponse Error \n"
             puts "HTTP Status Code is " + http.code + "\n\n"
             exit
    when Net::ProtoRetriableError
             puts "Unkown Error.   Net::ProtoRetriableError " "\n\n"
             exit
    when Net::ProtoUnknownError
             print "unknown error\n\n"
             exit
end

case http.code
    when "100"
       then puts "HTTP Status Code is 100 Continue \n\n"
    when "101"
       then puts "HTTP Status Code is 101 Switching Protocols \n\n"
    when "200"
       then puts "HTTP Status Code is 200 OK\n\n"
    when "201"
       then puts "HTTP Status Code is 201 Created\n\n"
    when "202"
       then puts "HTTP Status Code is 202 Accepted\n\n"
    when "203"
       then puts "HTTP Status Code is 203 Non-Authoritative Information\n\n"
    when "204"
       then puts "HTTP Status Code is 204 No Content\n\n"
    when "205"
       then puts "HTTP Status Code is 205 Reset Content\n\n"
    when "206"
       then puts "HTTP Status Code is 206 Partial Content\n\n"
    when "300"
       then puts "HTTP Status Code is 300 Multiple Choices\n\n"
    when "301"
       then puts "HTTP Status Code is 301 Moved Permanently\n\n"
    when "302"
       then puts "HTTP Status Code is 302 Found\n\n"
    when "303"
       then puts "HTTP Status Code is 303 See Other\n\n"
    when "304"
       then puts "HTTP Status Code is 304 Not Modified\n\n"
    when "305"
       then puts "HTTP Status Code is 305 Use Proxy\n\n"
    when "306"
       then puts "HTTP Status Code is 306 Unused\n\n"
    when "307"
       then puts "HTTP Status Code is 307 Temporary Redirect\n\n"
    when "400"
       then puts "HTTP Status Code is 400 Bad Request\n\n"
    when "401"
       then puts "HTTP Status Code is 401 Unauthorized \n\n"
    when "402"
       then puts "HTTP Status Code is 402 Payment Required  \n\n"
    when "403"
       then puts "HTTP Status Code is 403 Forbidden  \n\n"
    when "404"
       then puts "HTTP Status Code is 404 Not Found  \n\n"
    when "405"
       then puts "HTTP Status Code is 405 Method Not Allowed  \n\n"
    when "406"
       then puts "HTTP Status Code is 406 Not Acceptable  \n\n"
    when "407"
       then puts "HTTP Status Code is 407 Proxy Authentication Required  \n\n"
    when "408"
       then puts "HTTP Status Code is 408 Request Timeout  \n\n"
    when "409"
       then puts "HTTP Status Code is 409 Conflict  \n\n"
    when "410"
       then puts "HTTP Status Code is 410 Gone  \n\n"
    when "411"
       then puts "HTTP Status Code is 411 Length Required  \n\n"
    when "412"
       then puts "HTTP Status Code is 412 Precondition Failed  \n\n"
    when "413"
       then puts "HTTP Status Code is 413 Request Entity Too Large  \n\n"
    when "414"
       then puts "HTTP Status Code is 414 Request-URI Too Long  \n\n"
    when "415"
       then puts "HTTP Status Code is 415 Unsupported Media Type  \n\n"
    when "416"
       then puts "HTTP Status Code is 416 Requested Range Not Satisfiable  \n\n"
    when "417"
       then puts "HTTP Status Code is 417 Expectation Failed  \n\n"
    when "500"
       then puts "HTTP Status Code is 500 Internal Server Error  \n\n"
    when "501"
       then puts "HTTP Status Code is 501 Not Implemented  \n\n"
    when "502"
       then puts "HTTP Status Code is 502 Bad Gateway  \n\n"
    when "503"
       then puts "HTTP Status Code is 503 Service Unavailable  \n\n"
    when "504"
       then puts "HTTP Status Code is 504 Gateway Timeout  \n\n"
    when "505"
       then puts "HTTP Status Code is 505 HTTP Version Not Supported  \n\n"
end

exit
-------------

0 件のコメント: