2

I am trying to write a script that connects to remote server, pings other server (IP as argument) and outputs the result.

So here is what I wrote:

#!/bin/bash
# IP as argument
IP=$1
# Remote connection
ssh -p 22 [email protected]
# Pinging and saving only latency results to RESULT
RESULT=`ping -i 2 -c 4 $IP | grep icmp_seq | awk '{print $7}' | cut -b 6-12`
# Outputs the result    
echo $RESULT

But I am getting an error:

Name or service not known name tester.myserver.com

Of course tester.myserver.com is just example but if I manually type that ssh command with my real remote server address it does work. So I've really no idea why this won't work as a script.

5
  • You do know that the ssh command will block until you exit it, and then ping etc. will be executed on the local machine, right? Commented Apr 11, 2016 at 18:16
  • @Biffen I am total newbie when it comes to writing bash scripts and linux commands. So what would you suggest I do/change in my script? Commented Apr 11, 2016 at 18:17
  • The internet is full of examples of how to execute commands over SSH. So I suggest you ask your favourite search engine. Commented Apr 11, 2016 at 18:18
  • @Biffen as I wrote already in my Q. I know how to execute commands over SSH. I just write ssh... connect to remote server and then execute commands there. Q is how I integrate everything in one bash script and outputs the results. Commented Apr 11, 2016 at 18:21
  • The internet is full of examples of how to execute commands over SSH in a script as well. Commented Apr 11, 2016 at 18:22

2 Answers 2

3

Change your corresponding line to this:

RESULT=`ssh [email protected] "ping -i 2 -c 4 ${IP}" | grep icmp_seq | awk '{print $7}' | cut -b 6-12`

or without awk:

RESULT=`ssh [email protected] "ping -i 2 -c 4 ${IP} | grep icmp_seq | sed 's/^.*time=\([0-9.]\+\).*/\1/'"`

regards

Sign up to request clarification or add additional context in comments.

Comments

2

So the usual way to send a command or list of commands to be executed on a remote server is as follows:

ssh [email protected] "<your commands go here>"

or in your case:

ssh -p 22 [email protected] "ping -i 2 -c 4 $IP | grep icmp_seq | awk '{print \$7}' | cut -b 6-12"

Notice the "\" before $7 to escape the "$". This prevents the $7 to be evaluated to a local variable $7 (which might or not be set) when you run the ssh command, keeping the $7 in the right context with the other commands.

You would still have to set $IP for it to work though, and so all together looks like this:

IP = $1
ssh -p 22 [email protected] "ping -i 2 -c 4 $IP | grep icmp_seq | awk '{print \$7}' | cut -b 6-12"

Now $IP is resolved locally, while $7 is resolved remotely.

I had a similar problem to yours when I tried to connect to a remote server to run some commands and use a local variable - just like you are doing with $IP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.