Community discussions

MikroTik App
 
crs
just joined
Topic Author
Posts: 18
Joined: Wed Mar 10, 2010 10:19 am

problem with command via ssh - RouterOS bug ?

Wed May 16, 2012 2:29 pm

Hello. I'm writing script that gets user list from router. I'm executing commands on router using ssh (auth by key) and grab output. But.... when I execute the same command which works when I'm logged via ssh, but using it as a parameter to ssh command, I'm getting error "Interrupted". To be more clear - here are different tests I've performed:

1. OK - full list of users
> /usr/bin/ssh user2@192.168.1.1 ":put [ /user print ]"
Flags: X - disabled 
 #   NAME         GROUP       ADDRESS                                          
 0   user1        full       
 1   user2        backup                                      
 2   user3        full       
 3   user4        full       
 4 X user5        backup                                       
2. OK - list of users by getting names in loop. Instead of variable $x there's hardcoded id 1 so it gets always the same name
> /usr/bin/ssh user2@192.168.1.1 ":foreach x in=[ /user find ] do={ :put [ /user get 1 name ] }"
user2
user2
user2
user2
user2
3. ERROR - the same command as above but using loop variable in /user get
> /usr/bin/ssh user2@192.168.1.1 ":foreach x in=[ /user find ] do={ :put [ /user get $x name ] }"
interrupted
4. OK - the same command as in [3] but executed from shell after login into router console
[user1@192.168.1.1] > :foreach x in=[ /user find ] do={ :put [ /user get $x name ] }          
user1
user2
user3
user4
user5
Tests were performed on 5.14 and 5.16 systems.

Any idea what's wrong?
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: problem with command via ssh - RouterOS bug ?

Wed May 16, 2012 2:40 pm

The $ sign is used by the shell to access environment variables (at the client, before the SSH command even started).

I believe in UNIX, to escape a variable interpolation, you can either
- Use single quotes, i.e.
/usr/bin/ssh user2@192.168.1.1 ':foreach x in=[ /user find ] do={ :put [ /user get $x name ] }'
- Add a slash in front of the dollar, i.e.
/usr/bin/ssh user2@192.168.1.1 ":foreach x in=[ /user find ] do={ :put [ /user get \$x name ] }"
 
crs
just joined
Topic Author
Posts: 18
Joined: Wed Mar 10, 2010 10:19 am

Re: problem with command via ssh - RouterOS bug ?

Wed May 16, 2012 3:11 pm

Thanks man. It was very lame of me :)