This is my first posting here, I haven't found exactly what I'm looking for anywhere else so I'm hoping you all can help. I have a portion of script here that needs to ping two servers, then return whether they are up or down (somehow), and then echo that information. Here is what I have so far. It does echo up/down, but the information doesn't change regardless of the ping success.
Moderator's Comments:
Please use CODE tags as required by forum rules!
Last edited by RudiC; 10-07-2018 at 05:01 AM..
Reason: Added CODE tags.
This is my first posting here, I haven't found exactly what I'm looking for anywhere else so I'm hoping you all can help. I have a portion of script here that needs to ping two servers, then return whether they are up or down (somehow), and then echo that information. Here is what I have so far. It does echo up/down, but the information doesn't change regardless of the ping success.
Not quite true. It does change: 2 up, 0 down, if the last ping fails, 1 up, 1 down, if it succeeds.
There seems to be one basic misunderstanding: the $? variable represents the exit code of the last command executed; the before last's is overwritten. So your first ping's result is lost. So, you need to evaluate it after each ping, e.g. with another if construct. Doable, but tedious and ineffective.
How about a different approach? Collect the respective exit codes, and do some math afterwards? Like
The -q is redundant if you redirect output to /dev/null. I introduced the -W timeout option to reduce execution / waiting time.
This is perfect! Another way of looking at it certainly does the trick sometimes. I created a new servers.sh script and placed those three lines in it for testing purposes, it consistently returns correct results. It's amazing how you can go from a bunch of if statements to just three lines of code. Thank you so much for such a great response.
This is in an exec script:
That logfile is run through sed -n -f with the following control file for sed:
In our shop other computers we ping all start with "br" that we don't need in later processing--delete that or change as necessary.
help with bash script!
im am working on this script to make sure my server will stay online, so i made this script..
HOSTS="192.168.138.155"
COUNT=4
pingtest(){
for myhost in "$@"
do
ping -c "$COUNT" "$myhost" &&return 1
done
return 0
}
if pingtest $HOSTS
#100% failed... (4 Replies)
This is the script I already have but I have problems with two arguments
the first argument -t , I want to count 200 by the last digit of the IP address for example when I run the script ./ping.sh -t 17, the output would be192.168.0.217 is upThe second arguments --up won't work. Could anybody... (1 Reply)
Hi
I am using perl to ping a list of nodes - with script below :
$p = Net::Ping->new("icmp");
if ($p->ping($host,1)){
print "$host is alive.\n";
}
else {
print "$host is unreacheable.\n";
}
$p->close();... (4 Replies)
Hi,
I develop simple animation ping script on Solaris Platform. It is like Cisco ping.
Examples and source code are below.
bash-3.00$ gokcell 152.155.180.8 30
Sending 30 Ping Packets to 152.155.180.8
!!!!!!!!!!!!!.!!!!!!!!!!!!!!!.
% 93.33 success... % 6.66 packet loss...... (1 Reply)
Gents,
I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Hi all,
I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first.
Can you please help me determine which one to... (14 Replies)
Hi
How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
hi ,
i have problem that i can't ping machine , machine is in running state but i can't access or ping it .. but when i restarts that machine it works fine.
can anybody have an idea abou this problem ..
thanks (1 Reply)
can someone pls help me with the script for a files coming from one system to a particular directory and i want to write a script to move those files to another directory on different system by renaming the files...
pls someone help me on this...
thanking in anticipation.... (1 Reply)