I hacked away at this based on someone elses code. I found this script on the Internet with no credit to it! Thanks to whoever wrote the original script.
then run the script below like
ssh-cmd 127.0.0.1 txtfile.txt
and it should send all the commands in txtfile.txt to the router! I am successfully using this script to take all the static ip address entries in my mysql db (30-40 er so) and pull them into the mikrotik’s dhcp server. It works great!
#!/usr/bin/perl
# by Ian Redden (c) 2005
# Purpose: to get info off of a router for looking glass.
#
#-varibles
use Expect;
$ssh = "/usr/bin/ssh";
$router = "$ARGV[0]";
$txtfile = "$ARGV[1]";
#-get username/password
open(PS,"/usr/local/mikrotik/monitor.passwd");
while (<PS>) {
chomp;
($usernm,$pass) = split(/\|\|/, $_);
}
close(PS);
$command = "$ssh -l $usernm $router";
#-connect to router.
$ssh = Expect->spawn("$command");
$ssh->log_stdout(1);
#-check to see if dsa authenticity question appears
if ($ssh->expect(2, "yes/no")) {
print $ssh "yes\r";
}
#-send password.
if ($ssh->expect(undef, "password:")) {
print $ssh "$pass\r";
}
#-send command.
open( FILE, "< $txtfile" ) or die;
if ($ssh->expect(undef, ">")) {
while ($cmd = <FILE>) {
print $ssh "$cmd\r";
$ssh->expect(undef, ">");
}
}
close FILE;
print $ssh "/quit\r";
Great work!
This makes it possible to do anything on the board form a web based application.
Remember the script requires pearl module called Expect and to initially log in with ssh to the router for key exchange.
I did an update on the ssh-cmd. I noticed a bug when I was coding my dhcp push and bandwidth shaper push from my mysql database. My scripts would write all the correct commands to the text file, but the ssh-cmd would only half the time send half the cmds then quit mysteriously. Its fixed now and works 100% of the time.
So why can’t you just use ssh and a command to execute ? Is there no shell thats spawned or something? Seems like a hack to me. After using a command line like:
ssh -l admin -p pass router.domain.tld “export”
it should just pipe the response out, but it doesn’t. Just curious if this is something that would be fixed in 2.9 or there is no way around it.
I dislike using perl when not necessary so I wrote the same thing in sh.
It works with both ssh and telnet.
It’s bit more fancy since I also included dialog menus (I had some spare time).
The whole thing can be easly changed to make it more suitable for running within a cronjob.
Just read the connect_ssh and connect_telnet functions.
Hi YazzY, I’m trying to send commands via plink.exe in ssh but i’m getting back error “Server sent command exit status 127”. The connection is working, but when i send any type of command it returns that line. I suspect its the syntax but cant work out.
Ok, given the amount of questions here - is there any interest in a Windows based “MikroTik automation” tool? I.e. one that could read commands from a file, execute them, output the results to screen or save them to disk, … The whole thing perhaps with some kind of scheduler or something. Other suggestions?!
What would interested guys be willing to pay for it? I have some proof of concept almost finished, but this would need some more serious work and fine-tuning.
Everybody interested should write to info@cmit.de - I don’t want to abuse this forum for those discussions
Hi Cmit, i’m interested in a ssh or equivilant tool that i can incorporate in a .bat or .exe file to package commands to send to Mikrotik. The commands will be comming from my billing program, and would include adding/deleteing hotspot users, queues and any other relevant function.
I have Plink.exe command line tool but i cant seem to get Mikrotik to accept commnads, returns “…a ststus 127.” as i have said above it may be the syntax.
I myself and several others tried plink.exe to no avail. I suppose it has to do with the way RouterOS is spawing a shell (not not), that you can’t execute RouterOS commands from there. I don’t think that plink.exe can’t be made to work…
My tool would be made exactly for such purposes. I will have the ability to work via command-line only, and perhaps feature an integrated scheduler for repetitive tasks etc. Any suggestions from your side for required features?
Could you send me an e-mail to the address noted above?
Regarding the “down key”: There’s a parameter “without-paging” for all print commands in MikroTik. Then everything will be output without waiting for a key after every screen of data.
For example: “/interface wireless registration-table print without-paging”