Use SSH to execute commands (public/private key login)

From MikroTik Wiki
Revision as of 10:23, 4 April 2019 by Reinisjs (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Since RouterOS 2.9.13 support for SSH DSA keys and passing commands via ssh has been available. This allows you to run scripts from a remote machine against RouterOS without too much trouble anymore.

Example commands below are using FreeBSD - should be similar on other platforms.

1) Create a key using ssh-keygen

%ssh-keygen -t dsa

This creates a DSA key pair that is compatible with Mikrotik.

Generating public/private dsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_dsa.
Your public key has been saved in /home/user/.ssh/
The key fingerprint is:

Note: Make sure to leave the passphrase blank if you are going to be using this key in automated scripts. You do not want to be prompted for a password. Just make sure you keep your private key private (enough said?)!

2) Upload the generated key (public) to the target device:

Example: Upload key via FTP

Connected to
220 mikrotik FTP server (MikroTik 2.9.16) ready
Name ( admin
331 Password required for admin

ftp> put
226 ASCII transfer complete

ftp> exit

3) Now from either Winbox or terminal mode you need to import the key. An example using terminal mode is given.

[admin@mikrotik]> user ssh-keys import 
user: admin-ssh

The user field above determines which user account will be logged in when you pass the key. For security reasons you should never use the 'admin' account (you knew that right?). Create a separate user account.

Now that you've created a key pair and imported the public key into RouterOS you can start running commands from your remote machine.

Escaping shell special characters

For executing simple command with ssh you have to enclose command with quotes to tell shell which characters belong to single shell command parameter. If you use double quotes than you have to escape all inside double quotes with backslash \.

%ssh -l admin-ssh -i /home/user/.ssh/id_dsa ":put \"hello\""

Another special character which need escaping is single dollar sign $ because it's used for variable handling by shell like bash or sh.

%ssh -l admin-ssh -i /home/user/.ssh/id_dsa ":for i from=1 to=10 do={:put \$i}"

All other special characters evaluated by shell have to be escaped.


%ssh -l admin-ssh -i /home/user/.ssh/id_dsa "/system gps monitor"
  date-and-time: mar/18/2006 08:30:39
      longitude: "W 117 00' 00''"
       latitude: "N 33 0' 00''"
       altitude: "200.199997m"
          speed: "0.185200 km/h"
          valid: yes
%ssh -l admin-ssh -i /home/user/.ssh/id_dsa "/routing bgp peer print status"
Flags: X - disabled
 0 remote-as=65333 multihop=yes
     in-filter=cymru-in out-filter=cymru-out route-reflect=no hold-time=3m
     ttl=60 tcp-md5-key="" remote-hold-time=3h
     used-hold-time=3m used-keepalive-time=1m state=established
     uptime=1w1d10h54m55s prefix-count=68 refresh-capability=yes
%ssh -l admin-ssh -i /home/user/.ssh/id_dsa "/ip firewall connection print count-only"

DSA deprecated

Since OpenSSH 7.0 version DSA public key algorithm is considered weak and is deprecated. To be able to use DSA it needs to be enabled explicitly. Put PubkeyAcceptedKeyTypes +ssh-dss to ~/.ssh/config file.

Also to be able to connect to older versions of RouterOS you may need to use additional ssh parameters -o KexAlgorithms=diffie-hellman-group14-sha1 -o HostKeyAlgorithms=+ssh-dss

See also