scripit for telnet and execute commands

Hi all,

im looking for a script to telnet a server (192.168.5.100) and execetute a command…

You can’t do that from RouterOS.

you have to use linux with ssh for that - set up non-interactive login using certificate.

after that you can execute commands using ssh or create tcl scripts.

Somewhere at the forum i saw few commands and i tried my script:

/system telnet 192.168.0.1
:delay 10
:put "password" 
:delay 10
:put "restart"

but northing :frowning:

As I said previously it is not possible from RuterOS and as janisk said

you have to use linux with ssh

this is simple script, that assuming, that 192.168.0.1 is RouterOS connects to it, then from there using mac-telnet conencts to box with known mac address and sets up ip address, identity, default gw and enables api on router, then disconnects and quits.

i run this on ubuntu box with expect installed on it.

because you cannot run that dirrectly from routeros but expect is “emulated user”, that sees output, and reacts on it.

#!/usr/bin/expect
set mac [lindex $argv 0]
set ip [lindex $argv 1]
set ses 1
set $::env(TERM) xterm
spawn "telnet" "192.168.0.1" "-l" "admin+ctw80h25"
set ses $spawn_id
set timeout 200
expect -i $ses "Password:"
exp_send -i $ses "\r"
expect -i $ses "*admin*@*MAC*]*>"
exp_send -i $ses "/tool mac-telnet $mac\r"
expect -i $ses "Login:"
exp_send -i $ses "admin+ctw80h25\r"
expect -i $ses "Password:"
exp_send -i $ses "\r"
expect -i $ses "*admin*@*]*>"
exp_send -i $ses "/ip address add address=$ip/24 interface=ether1\r"
expect -i $ses "*admin*@*]*>"
exp_send -i $ses "/ip route add gateway=192.168.0.1\r"
expect -i $ses "*admin*@*]*>"
exp_send -i $ses "/ip service enable api\r"
expect -i $ses "*admin*@*]*>"
exp_send -i $ses "/system identity set name=$ip\r"
expect -i $ses "*admin*@*]*>"
exp_send -i $ses "/tool bandwidth-server set authenticate=no\r"
expect -i $ses "*admin*@*]*>"
close