ssh script can be telnet script?

hello!
i have some ssh script:
#!/bin/sh
ssh -i /bla/bla/dsa some_login@10.10.10.1 “/ip firewall address-list add address=$1 list=1M comment=$2”

but because of the problem with mikrotik ssh server, i want to do the same, but with the help of telnet. So i’ve created this:

!/bin/sh
user_add () {
(sleep 5; printf “password\n”; sleep 5; printf “/ip firewall address-list add address=$1 list=1M comment=$2\n”:wink: | telnet 10.10.10.2
}
user_add ${1} ${2}


so when i start the script:


[root@mega_servant /bla/bla/]# ./script.sh 10.10.3.3 test-user
Trying 10.55.31.1…
Connected to 10.55.31.1.
Escape character is ‘^]’.
Password:
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 3.17 (c) 1999-2008 http://www.mikrotik.com/

(628 messages not shown)

^[[?1;2c^[[65;3RConnection closed by foreign host.
[root@billing /data/netup/utm5/scripts]# 1;2c5;3R

i see strange symbols((( and i don’t know what to do.
Please, can anybody help?

it is problem with command you are trying to run - if you try to do just that on any usual linux openssh-server result will be the same.

you have to use expect scripting if you want to use interactive commands in non-interactive environment. then you will be able to log in, execute commands that other way would require user intervention, like /system telnet and control the target machine.

I’ve solved the problem not in ideal way:
i used the phptelnet script, so this phptelnet script:

<?php require_once "PHPTelnet.php"; $telnet = new PHPTelnet(); $telnet->show_connect_error=0; $result = $telnet->Connect('X.X.X.X','login','password'); switch ($result) { case 0: $telnet->DoCommand('/ping some_ip count=1', $result); echo $result; $telnet->DoCommand('/ip firewall address-list add address=Y.Y.Y.Y list=somelist comment=testings', $result); echo $result; $telnet->Disconnect(); break; case 1: echo '[PHP Telnet] Connect failed: Unable to open network connection'; break; case 2: echo '[PHP Telnet] Connect failed: Unknown host'; break; case 3: echo '[PHP Telnet] Connect failed: Login failed'; break; case 4: echo '[PHP Telnet] Connect failed: Your PHP version does not support PHP Telnet'; break; } ?>

logs on the Mikrotik and adds some address list.
I used ping as the first command, because, if we use address list add command - it doesn’t work=)
maybe this helps somebody…