Passing commands through an SSH shell in a bash script
I have a txt file with a bunch of commands. Here is the file"
Code:
'/ip firewall nat add chain=dstnat action=dst-nat to-addresses=10.0.0.25 to-ports=80 protocol=tcp src-address=192.168.10.44 dst-port=1-65535 comment="Baer NONPAYER"' Code:
cat /media/32GB/MikroTik/firewall_rules.txt | while read line; do Code:
expected command name (line 1 column 1) I have already got the key authentication set up and have tested it by passing Code:
ssh admin@192.254.1.1 '/ip firewall nat add chain=dstnat action=dst-nat to-addresses=10.0.0.25 to-ports=81 protocol=tcp src-address=192.168.10.52 dst-port=1-65535 comment="Johnson TORRENTER"' |
How about this?
Code:
cat commands.txt | while read x; do echo $( ssh regularuser@remotehostnameorip.domain "sudo $x" & ) ; done |
Quote:
Here what I ended up using, still getting the same error: Code:
cat /media/32GB/MikroTik/firewall_rules.txt | while read line; do The really confusing part is that I call in another script before this that does essentially the same thing, it works, but uses: Code:
`ssh admin@192.254.1.1 '/ip firewall nat remove 6'` |
I suspect that the first level of parsing by the shell strips off the leading/trailing single quotes from the file recs. Try escaping them thus
Code:
\'/ip firewall nat add chain=dstnat action=dst-nat to-addresses=10.0.0.25 to-ports=81 protocol=tcp src-address=192.168.10.52 dst-port=1-65535 comment="Johnson TORRENTER"\' Also, to see what's really happening, try set -vx at the top of the script. |
Try:
Code:
ssh admin@192.254.1.1 < /media/32GB/MikroTik/firewall_rules.txt |
I agree with the guru, if you can mod the input file and put in escapes/etc. might work ok for you.
Can also try an eval version: cat cmds.txt | while read i; do echo $( eval "ssh remoteuser@host.domain ""$i" & ); done |
if you are going to be explicitly typing all those commands, why don't you simply add the ssh part to them too?
ssh admin@192.254.1.1 '/ip firewall nat add chain=dstnat action=dst-nat to-addresses=10.0.0.25 to-ports=80 protocol=tcp src-address=192.168.10.44 dst-port=1-65535 comment="Baer NONPAYER"' ssh ad,om@192.254.1.1 '/ip firewall nat add chain=dstnat action=dst-nat to-addresses=10.0.0.25 to-ports=80 protocol=tcp src-address=192.168.10.45 dst-port=1-65535 comment="Watson NONPAYER"' That way you don't have to worry about 'cat' or re-direction or one too many parsing/evaluations by previous shell interpretation, etc. You would of course have to make the file executable and add the '#!/bin/bash' at the beginning of the file and that's it. Then, you simply run such file from the command line. |
Or you could scp the file to the remote server, and just run it with bash. This also negates the need to make multiple ssh connections within a short span of time. Just two: one to copy the file with the list of commands, and a second to ssh in to run the file.
|
The single quotes are messing it up, because it thinks the whole line is the name of the command. Remove the single quotes from each line and type the following command.
ssh admin@192.254.1.1 < /media/32GB/MikroTik/firewall_rules.txt |
I agree that the single quotes need to be removed from the txt file.
Then, as suggested, this should work: Code:
ssh admin@192.254.1.1 < /media/32GB/MikroTik/firewall_rules.txt Code:
cat /media/32GB/MikroTik/firewall_rules.txt | while read line; do Code:
cat /media/32GB/MikroTik/firewall_rules.txt | while read line; do |
Well, I spent a lot of time redoing this script. I've taken out all references to awk altogether and combined all of it in to one enormous script. A few bugs worked out thanks to the replies I've received here. Using ssh admin@192.254.1.1 < /media/32GB/MikroTik/firewall_scripts is awesome because it literally cut the run time of this script down to 1/100 of what it was. I've tested it, and this appears to be working just fine:
Code:
#!/bin/bash |
You could consider some further improvements below (Uses IFS= and read to remove cat, head and tail usage, removes usage of temp files, etc.)
Are Name.txt and ip.txt just leftover temp files? Code:
#!/bin/bash |
Just a comment for the benefit of those suggesting while..do..done loops in this situation.
The firewall_rules.txt file is a list of commands needed to be executed in the remote environment, separated by Linefeed characters. When issued by this command: Code:
ssh admin@192.254.1.1 < /media/32GB/MikroTik/firewall_rules.txt Think upon that, and marvel at the awesome power of the shell. |
All times are GMT -5. The time now is 10:37 PM. |