http://www.aarongifford.com/computers/m ... latest.txt
Download it and rename it as mtik.rb or something before using, and check the ruby command path on the first line of the script.
This script relies on some Ruby 1.9 behavior--while it wouldn't take much to port to 1.8, I suppose, I doubt I will bother. I haven't yet implemented timeouts like I hope to someday.
Improvements/bugs/comments are welcome.
In the mtik.rb file, if one were to uncomment the interactive client line:
Code: Select all
### Interactive client example:
MTik::interactive_client(ARGV)
Code: Select all
myhost# ./mtik.rb 10.0.0.1 admin weakpassword
<<< '/login' (6)
<<< END-OF-SENTENCE
>>> '!done' (5)
>>> '=ret=5eaf1a82ade0e68fbdcd412fd8aa582f' (37)
>>> END-OF SENTENCE
<<< '/login' (6)
<<< '=name=admin' (11)
<<< '=response=00a5a1f2e65b1aa9131be149671c4fe015' (44)
<<< END-OF-SENTENCE
>>> '!done' (5)
>>> END-OF SENTENCE
Command (/quit to end): /interface/print
=== COMMAND: /interface/print
<<< '/interface/print' (16)
<<< END-OF-SENTENCE
>>> '!re' (3)
>>> '=.id=*1' (7)
>>> '=comment=' (9)
>>> '=name=ether1' (12)
>>> '=type=ether' (11)
>>> '=mtu=1500' (9)
>>> END-OF SENTENCE
>>> '!re' (3)
>>> '=.id=*2' (7)
>>> '=comment=' (9)
>>> '=name=wlan1' (11)
>>> '=type=wlan' (10)
>>> '=mtu=1500' (9)
>>> END-OF SENTENCE
>>> '!re' (3)
>>> '=.id=*3' (7)
>>> '=comment=' (9)
>>> '=name=bridge' (12)
>>> '=type=bridge' (12)
>>> '=mtu=1500' (9)
>>> END-OF SENTENCE
[{"!re"=>nil, "=.id"=>"*1", "=comment"=>"", "=name"=>"ether1", "=type"=>"ether", "=mtu"=>"1500"}, {"!re"=>nil, "=.id"=>"*2", "=comment"=>"", "=name"=>"wlan1", "=type"=>"wlan", "=mtu"=>"1500"}, {"!re"=>nil, "=.id"=>"*3", "=comment"=>"", "=name"=>"bridge1", "=type"=>"bridge", "=mtu"=>"1500"}]
Command (/quit to end): /quit
<<< '/quit' (5)
<<< END-OF-SENTENCE
>>> '!fatal' (6)
>>> 'session terminated on request' (29)
>>> END-OF SENTENCE
=== Disconnected ===
myhost%
The other bottom-of-file example uses the script's MTik::command() function -- an all-in-one that accepts named parameters for host/user/port/pass/command, connects, logs in, executes the command, gets the result, closes the connection, then returns the result. Useful if you don't want to bother keeping a connection open.
If you instead choose to instantiate an MTik object, you'll most likely use the MTik#request_reply() call. Or you could use MTik#send_request() and MTik#get_reply() individually (the request_reply() method just encapsulates both). For MTik#send_request(), pass the command and args, command first, and args formatted in the '=argmane=argvalue' API style.
The response/rely is an array with each sentence of the response as a separate array item. Each array item is a hash (associative array) with key/value pairs already split up. So if the API word '=responsekey=responsevalue' appeared as a word in the 2nd response sentence, one would access the value 'responsevalue' thus:
Code: Select all
response[1]['=responsekey']
Here's an example of how I'm using this utility in another Ruby 1.9 script:
Code: Select all
def get_info(ip, user, pass)
tk = MTik.new(:host => ip, :user => user, :pass => pass)
rbinfo = tk.request_reply('/system/routerboard/print')
ethinfo = tk.request_reply('/interface/ethernet/print')
winfo = tk.request_reply('/interface/wireless/print')
... additional work with the routerboard, ethernet info, and wireless info done here...
tk.request_reply('/quit')
tk.close
end
Have fun!
I will.
Aaron out.