Sponsored Content
Top Forums Shell Programming and Scripting Bash script to detect nonpingable hosts Post 302815893 by Don Cragun on Sunday 2nd of June 2013 01:48:47 PM
Old 06-02-2013
Quote:
Originally Posted by newbie2010
I have a script to detect if a host is pingable or not. The problem is that I would like it to put the nonpingable hosts in one file and the pingable hosts in another. I have come up with this so far:

Code:
for ip in `cat /tmp/testlist2`; do ping -c 3 $ip >/dev/null && echo "$ip is up" || echo "$ip is down"; done>/tmp/s4.file
cat /tmp/s4.file |grep -i down >/tmp/f1.file
cat /tmp/s4.file |grep -i up >/tmp/sup.file

This works but it is clumsy. If I try to just do a redirect after the ping it doesn't work, though. What I get is only the first host name that the ping is tried on. Would I need another loop to get the hosts in two different files? Any suggestions?
You could try something like:
Code:
rm -f /tmp/f1.file /tmp/sup.file
for ip in $(cat /tmp/testlist2)
do      ping -c 3 $ip >/dev/null && echo "$ip is up" >> /tmp/sup.file || echo "$ip is down" >> /tmp/f1.file
done


Last edited by Don Cragun; 06-02-2013 at 02:50 PM.. Reason: Reversed the redirections...
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

hosts.allow & hosts.deny

Hi! Im trying to use host.allow & host.deny to resrtic access to my sun machine, but it doesnt seem to work... I want to allow full access from certain IPīs (ssh,http,ftp,etc...) but deny all kind of conections from outsideworld, the way that im doing that is: hosts.allow ALL:127.0.0.1... (2 Replies)
Discussion started by: Sorrento
2 Replies

2. UNIX for Dummies Questions & Answers

how to detect my script is already running

I have a script which must not be run more than once at any given time. THis script will be scheduled to run every 20 mins as a cron job. In my script can i have logic to say if this script is already running from the previous cron, then exit. How do i go about doing that. If you describe the... (11 Replies)
Discussion started by: rmulchandani
11 Replies

3. Shell Programming and Scripting

BASH Script to Detect and List USB Flash Drives

Hello. This is my first post to this forum. I've read many of the posts over the last two or three years and I've learned a lot. I'm creating a live Linux distribution using the Linux Live Scripts -- just as a hobby project -- and I'm wanting to create an automated way for a user to copy the... (7 Replies)
Discussion started by: godzillarama
7 Replies

4. AIX

aix tcp wrappers hosts.allow hosts.deny?

hi all just installed the netsec.options.tcpwrapper from expansion pack, which used to be a rpm, for my aix 6.1 test box. it is so unpredictable. i set up the hosts.deny as suggested for all and allow the sshd for specific ip addresses/hostnames. the tcpdchk says the hosts allowed and... (0 Replies)
Discussion started by: wf201626
0 Replies

5. Shell Programming and Scripting

How to write bash script for creating user on multiple Linux hosts?

I wonder whether someone can help me with what I'm trying to achieve Basically, the objective is one script to create new user on more than 70 linux hosts if required. Everything works apart from the highlighted part. It gave me an output passwd: Unknown user name ''. when try to set... (35 Replies)
Discussion started by: fugeulu
35 Replies

6. Shell Programming and Scripting

Check connectivity with multiple hosts - BASH script available here

Hi everyone! Some time ago, I had to check connectivity with a big list of hosts, using different formats (protocol://server:port/path/, server:port, ....). I developed a script that checks the connectivity using different commands (ping, telnet, nc, curl). It worked for me so I'm sharing it... (9 Replies)
Discussion started by: Fr3dY
9 Replies

7. Shell Programming and Scripting

Detect DST from a date entered by user in bash

I am trying to write a bash script that takes in a users input of a specific date in a format such as MM/DD/YYYY and returns whether or not that specific date was during daylight savings time or not. Is there a specific function that checks this? Does the date command have a way to do this? I am... (1 Reply)
Discussion started by: conman1985
1 Replies

8. UNIX for Advanced & Expert Users

A script to detect system type

Hi forum, So I am trying to determine the OS type with the following script: #!/usr/bin/sh OStype1=`uname -s` Sunos1=SunOs if then echo "This system is Linux" exit 0 elif then echo "This system is SunOs" exit 0 elif (1 Reply)
Discussion started by: dampio
1 Replies

9. Shell Programming and Scripting

How to detect url in use in a script?

Hello, I have a small script and it runs from web application in below format: pipe:///path_to_myscript.sh url1 url2 url3 myscript.sh: #!/bin/bash count=0 while do count=$((count+1)) exec 3>&1 ((ffmpeg -i $1 ...... -f mpegts pipe:1 2>/dev/null 1>&3 ) 2>&1 | \ while read LINE; do echo... (9 Replies)
Discussion started by: baris35
9 Replies

10. Solaris

How to copy a tar file on a series of remote hosts and untar it on those hosts?

Am trying to copy a tar file onto a series of remote hosts and untar it at the destination. Need to do this without having to do multiple ssh. Actions to perform within a single ssh session via shell script - copy a file - untar at destination (remote host) OS : Linux RHEL6 (3 Replies)
Discussion started by: sankasu
3 Replies
libssh2_knownhost_get(3)					  libssh2 manual					  libssh2_knownhost_get(3)

NAME
libssh2_knownhost_get - get a known host off the collection of known hosts SYNOPSIS
#include <libssh2.h> int libssh2_knownhost_get(LIBSSH2_KNOWNHOSTS *hosts, struct libssh2_knownhost **store, struct libssh2_knownhost *prev): DESCRIPTION
libssh2_knownhost_get(3) allows an application to iterate over all known hosts in the collection. store should point to a pointer that gets filled in to point to the known host data. prev is a pointer to a previous 'struct libssh2_knownhost' as returned by a previous invoke of this function, or NULL to get the first entry in the internal collection. RETURN VALUE
Returns 0 if everything is fine and information about a host was stored in the store struct. Returns 1 if it reached the end of hosts. Returns negative values for error AVAILABILITY
Added in libssh2 1.2 SEE ALSO
libssh2_knownhost_readfile(3) libssh2_knownhost_writefile(3) libssh2_knownhost_add(3) libssh2 1.2 28 May 2009 libssh2_knownhost_get(3)
All times are GMT -4. The time now is 11:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy