0

I'm trying to catch/pipe ping response from remote cisco router over ssh (see script). Everything works well but only at ping the pipe brokes at first "!" ping response from router destination:

routername#ping xxx.xxx.xxx.xxx source loopback xx
Type escape sequence to abort.Sending 5, 100-byte ICMP Echos to xxx.xxx.xxx.xxx, timeout is 2 seconds:
Packet sent with a source address of xxx.xxx.xxx.xxx
!

And that's all... It seems to break after the first "!".

Where this works well and catch and give back whole ping result:

ssh "user@$1" 'ping xxx.xxx.xxx.xxx source loopback xx' > log
ssh user@$1 "ping xxx.xxx.xxx.xxx source loopback xx" | tee -a log

but only as single line command. What can I do to catch the whole output until end?

My script:

#!/bin/bash
echo "$1"
var1=$(ssh user@$1 << "ENDSSH"
sh version | incl System image file
sh ip interface INTx  | incl MTU
show controllers VDx | incl Speed
sh cellular 0 radio | include RSS
sh cellular 0 netw | include Selected
ping xxx.xxx.xxx.xxx source loopback xx
ENDSSH)
echo "$var1" > log
bojan
  • 1
  • 1

1 Answers1

0

This is how I'd do it if I wanted to use var1 for more things later on;

var1=$(ssh user@$1 "ping xxx.xxx.xxx.xxx source loopback xx; SSHEND")

echo "$var1" > log

However it's only good if the text doesn't have control chars in it.

This would have the effect of hiding output from std out and is equivalent to;

ssh user@$1 "ping xxx.xxx.xxx.xxx source loopback xx; morecommands" > log

You may want the output in stdout and there are ways to that too. I think it's;

ssh user@$1 "ping xxx.xxx.xxx.xxx source loopback xx" | tee -a log

When all else fails, put all your remote commands into a script and execute the script remotely.

a one liner;

tf=$(mktemp); echo "ping xxx.xxx.xxx.xxx source loopback xx">$tf; output=$(ssh user@$1 bash -s < $tf); echo $output; rm -f $tf
Calvin Taylor
  • 664
  • 4
  • 15
  • indeed... but i'm using some more commands inside ENDSSH, like show version and so on... while other are running well, the ping brokes after first ! ... i'm clueless why... – bojan Sep 12 '19 at 13:57
  • well you can separate commands in several ways, ';' will start a new command, '&&' will execute second if the first succeeds, and '||' if the first fails The way you have your question stated, it is valid bash syntax to use ENDSSH as a label like you have, so perhaps expand your question to include your caveats. – Calvin Taylor Sep 13 '19 at 12:42
  • hey calvin... thank you very much... only ssh user@$1 "ping xxx.xxx.xxx.xxx source loopback xx" | tee -a log give me full response to log how that should be like --- !!!!! Success rate is 100 percent (5/5), round-trip min/avg/max = 40/43/48 ms ---- – bojan Sep 16 '19 at 13:59
  • how can i execute further commands in the same ssh? like: var1=$(ssh user@$1 << "ENDSSH" sh version | incl System image file sh ip interface INT1 | incl MTU show controllers VDSL | incl Speed sh cellular 0 radio | include RSSI sh cellular 0 netwo | include Selected ENDSSH ) echo "$var1" > log ---- while those was previous allready ok and i get response from all of them except ping commands where the pipe to log broke after the first ping response sign from the router which is the exclamation mark "!" – bojan Sep 16 '19 at 13:59
  • https://stackoverflow.com/questions/305035/how-to-use-ssh-to-run-a-shell-script-on-a-remote-machine – Calvin Taylor Sep 16 '19 at 18:16
  • the '!' is a control char and would need to be escaped, and I think that can be avoided by using a script. – Calvin Taylor Sep 16 '19 at 18:17
  • hey Calvin... Thx for answering... How could I avoid that in my script? – bojan Sep 17 '19 at 17:05