#!/usr/bin/env ruby ######################################################################## #-- # # FILE: tikcli -- Example of using the Ruby MikroTik API in Ruby # #++ # Author:: Aaron D. Gifford - https://aarongifford.com/ # Copyright:: Copyright (c) 2009-2020, InfoWest, Inc. # License:: BSD license #-- # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, the above list of authors and contributors, this list of # conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. Neither the name of the author(s) or copyright holder(s) nor the # names of any contributors may be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S), AUTHOR(S) AND # CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY # AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL # IN NO EVENT SHALL THE COPYRIGHT HOLDER(S), AUTHOR(S), OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # DCONSEQUENTIAL AMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF # THE POSSIBILITY OF SUCH DAMAGE. ######################################################################## # encoding: ASCII-8BIT $LOAD_PATH.unshift(File.dirname(__FILE__)+'/../lib') require 'rubygems' require 'mtik' def usage(msg='') STDERR.print( (msg.size > 0 ? msg + "\n\n" : '') + "Usage: #{$0} [-s|--ssl] [-u|--unencrypted_plaintext] \n" + " --ssl OR -s - Use SSL for the API connection.\n" + " --unencrypted_plaintext OR -u - Use the 6.43+ login API even if NOT\n" + " using SSL.\n" ) exit(-1) end use_ssl = unencrypted_plaintext = false while !ARGV[0].nil? && ARGV[0][0] == '-' arg = ARGV.shift case arg when '--ssl', '-s' usage("Please do not repeat the --ssl (or -s) parameter") if use_ssl use_ssl = true when '--unencrypted_plaintext', '-u' usage("Please do not repeat the --unencrypted_plaintext (or -u) parameter") if unencrypted_plaintext unencrypted_plaintext = true else usage("Unknown argument #{arg.inspect}") end end usage("Too many arguments.") if ARGV.size > 3 usage("Insufficient arguments.") if ARGV.size < 3 ## Permit setting use_ssl and unencrypted_plaintext via environment variables: use_ssl = true if ENV['MTIK_SSL'] unencrypted_plaintext = true if ENV['MTIK_UNENCRYPTED_PLAINTEXT'] ## NOTE: We IGNORE environment variable MTIK_VERBOSE mode as this script ## ALWAYS uses verbose mode. args = { :host => ARGV[0], :user => ARGV[1], :pass => ARGV[2], :ssl => use_ssl, :unencrypted_plaintext => unencrypted_plaintext } p args MTik::interactive_client(args)