[CONTRIB] ssh perl script - automate batch commands *UPDATED

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.

EDIT: http://www.dysan.net/Perl/lg.php – Original script.

You put a file named monitor.passwd in your etc directory…

/etc/monitor.passwd:
admin||blah01

then create a file called txtfile.txt and put in it stuff …

/somewhere/txtfile.txt:
/ip dhcp-server lease print

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.

Thanks a lot iredden

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. :slight_smile:

perl -MCPAN -e shell
install Expect
install IO:Tty
install IO:Stty
quit

Another update.

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.

Sam

And still doesn´t working via CRON…

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.

The script is avaliable at:
http://yazzy.yazzy.org/coding/shell/routeros/routeros.txt

I tested it with sh on FreeBSD and Gentoo Linux (where /bin/sh → bash).

Enjoy.

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.

If anyone can help would appricaite very much.

any version for win based systems ? :slight_smile: (with/without SSH)

Use PERL for windows. I’m about to test it in a week or so - so I cannot guarantee you success. :wink:

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 :wink:

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.

Have you used plink.exe?

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?

Was a windows tool ever created?

Not yet - interest was, well, low…

bump, for all the people emailing me asking for this.

Let me give you a one-liner, just in case somebody cares about it:

expect -c “spawn ssh user@1.1.1.1 "/ip route print"; expect "password: "; send "userpass\r"; interact”

Note that the host you are connecting to should be previously added to SSH known hosts. Also note that it is for 2.9 only

is telnet/ssh the only way to do this?

i currently have a php script that telnets in and it takes 60 seconds to issue a command.

the other thing is how do you get data back out from mikrotik, the first couple line are easy, but what about when you need to press the down key?

is perl going to be alot faster than the php telnet i use now?

thanks

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”

I wrote a bit of php code that can log in and run a command then output the data. This code does require libssh2 (http://sourceforge.net/projects/libssh2/) and the ssh2 module for php5 (http://pecl.php.net/package/ssh2).

To enter a command you have to use a \r instead of \n.

<?php

$server = "x.x.x.x";
$username = "user";
$password = "pass";

$methods = array(
  'kex' => 'diffie-hellman-group1-sha1'
);

$connection = ssh2_connect($server, 22, $methods);
ssh2_auth_password($connection, $username, $password);
$shell = ssh2_shell($connection)){

$cmd = "/system";

$output = user_exec($shell,$cmd);
$output = user_exec($shell,$cmd);

print_r($output);

function user_exec($shell,$cmd) {
    fwrite($shell, $cmd ."\r");
    sleep(1);
    $output = "";
      while($line = fgets($shell)) {
               flush();
               $output[] = $line;
    }
    return $output;
    fclose($shell);
}

?>

Enjoy