Ssh on ping result?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ssh on ping result?
# 1  
Old 11-27-2018
Ssh on ping result?

I still haven't had chance to read the entire Debian manual, which I promise I will do as soon as I can, and I will start putting info back into this forum.

However, for the mean time, could someone please help with a small script? I understand what I've got to do and how to do it, but I'm struggling with getting the syntax right.

I have a loads of nodes I need to connect to from time to time, but I'm never sure if they're alive. So instead of ssh'ing and waiting ages to either get a connection, or not, I'd sooner start with a ping, if there is a good response, set off the ssh. At this point I might have 10 terminal sessions open for different nodes, so I want to automate as much as possible.

So the pseudo would be:

User types:
con user@192.168.xxx.xxx

Code:
#!/bin/bash
Strip ip from $1 to str-IP
Start loop from 1 to 10 (arbitrary):
Create a bln-Connection variable and set value = False
Start loop from 1 to 10 (Arbitrary)
--Ping the str-IP
--If response | grep includes ttl or time, or ms I guess:
----set bln-Connection = True
----exit the loop

If bln-Connection = True:
--ssh $1
else
--echo "The blooming thing is down again."

Something like that. I hope that's clear enough, and I know I'm using about 3 different languages, just none of it bash!

Many, many thanks.

Moderator's Comments:
Mod Comment edit by bakunin: Please use CODE-tags, even for Pseudo-code. Thank you.

Last edited by bakunin; 11-28-2018 at 07:39 AM..
# 2  
Old 11-27-2018
Why not use ping's return code as intended instead of grepping text?

Code:
#!/bin/bash

for X in "$@"
do
        X="${X/*@/}"
        ping -c 4 -w 1000 "$X" || echo "The blooming thing is down again"
done

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-27-2018
Or use the respective option to ssh:


Code:
ssh -oConnectTimeout=1 user@FreeBSD
ssh: connect to host freebsd port 22: Connection timed out

This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-27-2018
I've actually had some time to look into this today, and with 'some help' from google I think I have it sorted out. Or will when I get the wrong lines right!

While I'm at it, could someone please explain why:
Code:
-# command="user@192.168.xxx.xxx"
-#*echo $command | cut -d'@' -f1
-# user

but-

Code:
-# command="user@192.168.xxx.xxx"
-# user=$command | cut -d'@' -f1
-# echo $user

gives no output? Or at least an empty line. Do I need to present the variable in brackets or quotes? I've tried every combination that makes sense from examples in help topics.

Thanks.

------ Post updated at 05:22 PM ------

Thanks folks so far...
Corona688, that certainly helps me set the flag.
RudiC, -o ConnectTimeout doesn't appear to be part of this branch of Debian.

Last edited by rbatte1; 11-28-2018 at 01:52 PM.. Reason: Added CODE tags
# 5  
Old 11-27-2018
Quote:
Originally Posted by MuntyScrunt
I've actually had some time to look into this today, and with 'some help' from google I think I have it sorted out. Or will when I get the wrong lines right!

While I'm at it, could someone please explain why:
Code:
-# command="user@192.168.xxx.xxx"
-#*echo $command | cut -d'@' -f1
-# user

but-
Code:
-# command="user@192.168.xxx.xxx"
-# user=$command | cut -d'@' -f1
-# echo $user

gives no output?
A pipe | captures the output of the previous command.

What gets printed when you type echo $command ? It prints user@192.168.xxx.xxx

What gets printed when you type user=$command ? Nothing.

There is basically no reason to ever use echo "line" | cut, that's inefficient and the shell has better builtins to do it.

Try
Code:
$ user="username@192.168.xxx.xxx"
$ echo "${user/@*/}"

192.168.xxx.xxx

$

If your shell doesn't have ${VAR/SUB} type substitution, try "${user##*@}" instead.

Last edited by rbatte1; 11-28-2018 at 01:53 PM.. Reason: Added CODE tags in the quote to match the update to the original post
# 6  
Old 11-27-2018
Quote:
RudiC, -o ConnectTimeout doesn't appear to be part of this branch of Debian.
What exactly happens when you try? Show exactly what you did, word for word, letter for letter, keystroke for keystroke.
# 7  
Old 11-27-2018
You certainly want
Code:
-# command="user@192.168.xxx.xxx"
-# user=$(echo $command | cut -d'@' -f1)
-# echo $user

The shell builtin for this is
Code:
user=${command%%@*}

It's easier to compose long strings from short strings
Code:
-# user=user
-# command="$user@192.168.xxx.xxx"
-# echo $user

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Cant ssh, but ping works

I cant ping to some of my machines, but ping works. I attach screenshots. Port is open and it is 22. I can't figure out why i cant access. https://www.unix.com/attachments/unix-for-advanced-and-expert-users/7492d1541541072-cant-ssh-but-ping-works-sshlisten-jpg... (17 Replies)
Discussion started by: tomislav91
17 Replies

2. AIX

Not able to ssh or ping a server

Hi, Earlier I was able to ssh /ping the server but now it just hangs Please suggest how to troubleshoot. Best regards, Vishal (4 Replies)
Discussion started by: Vishal_dba
4 Replies

3. Shell Programming and Scripting

Ping result talking too long

Hello all, I am writing a script that pings various machines to check connectivity. If a machine is available, the prompt returns a result immediately: root@ops # ping 172.21.5.5 172.21.5.5 is alive BUT if a machine is Down , the reply takes a long time to come. The issue is I want to... (1 Reply)
Discussion started by: Junaid Subhani
1 Replies

4. Shell Programming and Scripting

Perl : code on ping showing difference result

Hi all, I am using the below code to ping a code and print whehter the connection is successful or not. use Net::Ping; $p = Net::Ping->new(); my $host = "x.x.x.x"; # print "$host is alive.\n" if $p->ping($host); if ($p->ping($host,3)) { print... (0 Replies)
Discussion started by: scriptscript
0 Replies

5. Solaris

Ping = no, SSH = eventually. Next step?

I have a Solaris 9 server that does not return a ping. When I try to log in via SSH I eventually get in. I am logged in now. I know this is a wide open question, but can you recommend some things I should check? . Thanks in advance, ~R (2 Replies)
Discussion started by: abstractrick
2 Replies

6. Shell Programming and Scripting

prevent ssh from executing result in shell

Hi, I am writing a script on Solaris 10 and want to execute a remote ssh command. Normally this command should just return the value 0000000000002356 but when using ssh it seems it is passing the result to the shell to execute. ssh root@10.5.112.145 `/usr/bin/nawk -F\, '$1=="USG" && $2=="01"... (3 Replies)
Discussion started by: borderblaster
3 Replies

7. Solaris

could not ssh and ping

Hi, I have a solaris 8 machine. I see ssh is running in the machine sbnismwp2# ps -aef | grep ssh root 947 945 0 04:34:45 ? 0:00 /export/opt/SSHtecagt/sbin/ssh-mgmt-sysmonitor root 945 1 0 04:34:45 ? 0:00 /export/opt/SSHtecagt/sbin/ssh-mgmt-agent... (11 Replies)
Discussion started by: jegaraman
11 Replies

8. Solaris

could not ssh and ping

hi , i cannot ssh to a server , i have restarted the ssh instances also. and i cannot ping to the server.. any ideas ..plz (5 Replies)
Discussion started by: jegaraman
5 Replies

9. Shell Programming and Scripting

Problem storing SSH result in a variable

i have this SSH command which runs perfectly on command prompt in sunOS ssh -o Port=${portno} ${uname}@${server} find ${dir_path} -name '***' output : /usr/local/home/*** My problem is when i run same command in my script #!/usr/bin/ksh res=`ssh -o Port=${portno} ${uname}@${server}... (1 Reply)
Discussion started by: prash184u
1 Replies

10. Linux

Can't SSH / ping Linux box !!!

I have a linux box build11 which can be pinged from build18 (Windows) box. And we can only login to the box (using SSH) from build18 box. Plz help to characterize the problem, network, DNS, DHCP, etc (or whatever which I am unsure) Any idea what may be the reason ? :confused: Thanks in... (4 Replies)
Discussion started by: csaha
4 Replies
Login or Register to Ask a Question