Mikrotik e le chiavi ssh
Una RouterBoard, come è noto, si gestisce con la sua interfaccia winbox, ma vista la versatilità del linguaggio che permette di programmarla tramite la command line, capita spesso di utilizzare la console ed il comando ssh per collegarsi.
Diventa molto utile importare le chiavi ssh (publick keys), questo permette di eseguire comandi remoti e collegarsi senza necessità di utilizzare una password. Ovviamente, l’host di cui si importa la chiave deve essere assolutamente fidato. Normalmente si utilizzano client o sistemi di reti interne non raggiungibili direttamente da internet. Vediamo, come fare:
Creazione delle chiavi sul client
# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
SHA256:ToMHDoc63hObug64to5VjQXdNmu46GhctIXn3rNyDT8 root@server3
The key's randomart image is:
+---[DSA 1024]----+
PS C:\Users\paolo\.ssh> dir Directory: C:\Users\paolo\.ssh Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 08/03/2021 09:38 1679 id_rsa -a---- 08/03/2021 09:38 404 id_rsa.pub -a---- 22/02/2021 17:50 1543 known_hosts
Importazione delle chiavi sulla RouterBoard
A questo punto accediamo a [System>User]>>[SSH Keys]>>[Import SSH Keys]
Adesso la chiave selezionata viene importata. Attenzione! La colonna User con la voce admin ci dice che qual è l’utente associato alla chiave importata.
[admin@MikroTik-CASA] > /user ssh-keys print
Flags: R - RSA, D - DSA
# USER BITS KEY-OWNER
0 D admin 1024 root@server3
Utilizzo della connessione senza password (con la chiave)
MMM MMM KKK TTTTTTTTTTT KKK
MMMM MMMM KKK TTTTTTTTTTT KKK
MMM MMMM MMM III KKK KKK RRRRRR OOOOOO TTT III KKK KKK
MMM MM MMM III KKKKK RRR RRR OOO OOO TTT III KKKKK
MMM MMM III KKK KKK RRRRRR OOO OOO TTT III KKK KKK
MMM MMM III KKK KKK RRR RRR OOOOOO TTT III KKK KKK
MikroTik RouterOS 6.46 (c) 1999-2019 http://www.mikrotik.com/
[?] Gives the list of available commands
command [?] Gives help on the command and list of arguments
[Tab] Completes the command/word. If the input is ambiguous,
a second [Tab] gives possible options
/ Move up to base level
.. Move up one level
/command Use command at the base level
[admin@MikroTik-CASA] >
# ssh admin@192.168.1.5 '/system resource print'
uptime: 25w6d5h36m28s
version: 6.46 (stable)
build-time: Dec/02/2019 11:16:46
factory-software: 6.23
free-memory: 92.0MiB
total-memory: 128.0MiB
cpu: MIPS 74Kc V4.12
cpu-count: 1
cpu-frequency: 600MHz
cpu-load: 61%
free-hdd-space: 105.9MiB
total-hdd-space: 128.0MiB
write-sect-since-reboot: 3559997
write-sect-total: 3693336
bad-blocks: 0%
architecture-name: mipsbe
board-name: RB951G-2HnD
platform: MikroTik