Problem sending multi-line command file via SSH

I’d like to be able to make changes to my configuration in an automated way via an SSH connection. I have a file with the following content:

/ip firewall mangle
add chain=whatever blah blah
add chain=whatever blah blah

If I type something like this at the command line it works fine. If I connect via SSH and do the same thing it works fine. But if I try and automate this process by piping it to an SSH session:

$ cat foobar.txt | ssh -i admin 192.168.88.1

Then I get the error message

bad command name add (line 1 column 1)

If I change my file to be

/ip firewall mangle add chain=whatever blah blah
/ip firewall mangle add chain=whatever blah blah

then it works, but this means modifying my script to have a lot of repeated information in it. Am I doing something obviously wrong here?

I use semi-colons between commands, at least for short things. I don’t run but a few lines on my stuff. Try using { } around your file and see if that helps.

Thanks for the suggestion, but braces don’t seem to help. I get a ‘missing closing brace’ error in addition to the ‘bad command name’ errors if I put braces round it.

Currently this is how ssh input is processed. Each line is treated as completely separate scripts that is why you get error when trying to send

/ip firewall mangle
add chain=whatever blah blah

Probably in the future it will be changed.

Thanks for clarifying. Is there a good way of applying a batch of commands like this, other than FlashFig? I’ll probably use FlashFig for bulk configuration of routers in a live setup, but for the moment I just want a quick way of applying a batch of commands from the command-line while I’m developing.

The best way I’ve found is to copy the file across with SCP and execute it on the router, which seems to work fine.

Yes, best way is to upload file and execute it on a router.

That still being a problem?? Thanks!

No. It works OK now.

Have some chance to support multiline script via ssh?
ssh admin@my.mikrotik.org < ros_multiline.script

scp && ssh run - unwanted and thread unsave

Hi!
Maybe someone still needs it…
I faced exactly the same problem.
On my Ubuntu 20.04.1 LTS the solution is:

$ ssh 192.168.88.1 $(cat script.rsc)

Good luck!