#!/usr/bin/ruby # # author: nein # date: 2005 # desc: script for use on web servers that allow put method require 'socket' require 'openssl' require 'getoptlong' usage = %{ Usage: #{$0} -h -l -r --host, -h hostname --local-file, -l localfile --remote-file, -r remotefile --target-port, -p port --ssl, -s enable ssl } opts = GetoptLong.new( [ "--host", "-h", GetoptLong::REQUIRED_ARGUMENT ], [ "--local-file", "-l", GetoptLong::REQUIRED_ARGUMENT ], [ "--remote-file", "-r", GetoptLong::REQUIRED_ARGUMENT ], [ "--target-port", "-p", GetoptLong::OPTIONAL_ARGUMENT ], [ "--ssl", "-s", GetoptLong::NO_ARGUMENT ]) targetHost,localFile,remoteFile,targetPort,hasSSL = nil begin opts.each { |opt, arg| case opt when '--host' targetHost = arg when '--local-file' localFile = arg when '--remote-file' remoteFile = arg when '--target-port' targetPort = arg when '--ssl' hasSSL = 1 end } rescue $stderr.close end if !targetHost || !localFile || !remoteFile puts usage exit end if !targetPort targetPort = 80 end data = File.read(localFile) rescue if !data puts usage exit end request = "PUT #{remoteFile} HTTP/1.1\r\n", "Host: #{targetHost}\r\n", "Content-Length: #{data.length}\r\n", "\r\n#{data}" if hasSSL sock = TCPSocket.new(targetHost,targetPort) if !sock puts "error: couldn't initiate connection" exit end ssl_context = OpenSSL::SSL::SSLContext.new() sslsock = OpenSSL::SSL::SSLSocket.new(sock, ssl_context) sslsock.sync_close = true sslsock.connect rescue sslsock.puts(request) puts sslsock.gets exit end if !hasSSL sock = TCPSocket.new(targetHost,targetPort) if !sock puts "error: couldn't initiate connection" exit end sock.puts(request) puts sock.gets end