Main Menu

Search

Showing posts with label Linker Article Linux Shell Scripts. Show all posts
Showing posts with label Linker Article Linux Shell Scripts. Show all posts

LINUX: While Loop Command To Run Command(s) Continuously (How To Doc) ("while" loop Command)

Below is the command to use.


# while true; do date;hostname ; sleep 1; done

In above command example we are running and date, hostname commands followed by 1 second sleep. All these 3 commands keep running in a loop until Ctrl C is done to exit out. You can replace commands in above command with the commands you want to run continuosly in while loop.

Below is example output of above command.


Thu Sep 19 14:31:39 EDT 2019
linux-test-machine
Thu Sep 19 14:31:40 EDT 2019
linux-test-machine
Thu Sep 19 14:31:41 EDT 2019
linux-test-machine
Thu Sep 19 14:31:42 EDT 2019
linux-test-machine
Thu Sep 19 14:31:43 EDT 2019Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
0b:12:45:53:31:78:64:7d:68:b9:1c:54:36:2d:51:3e root@source-vm
The key's randomart image is:
+--[ RSA 2048]----+
|     .++*+.+=+.  |
|     ..o..*.oo.  |
|    .  . o + .E  |
|     .    o    . |
|    . . S        |
|     . . .       |
|        .        |
|                 |
|                 |
+-----------------+

Products to which Article Applies

All Linux Operating Systems. The steps in this article should also work on other UNIX bases Operating systems. 

Additional References

https://www.tecmint.com/run-repeat-linux-command-every-x-seconds/

LINUX: Shell Script For Pinging Multiple IP Addresses / Hosts And Report Whether They Are UP or DOWN (How To Doc)

This article provides script which will ping multiple IP addresses / Hostnames and report if they are UP or down.

Below is the script content.

#!/bin/bash
# Program name: ping_multiple_ip_hosts.sh
echo "`date`" >>/tmp/result.out
cat /tmp/hosts |  while read output
do
    ping -c 1 "$output" > /dev/null
    if [ $? -eq 0 ]; then
    echo "node $output is up" >>/tmp/result.out
    else
    echo "node $output is down" >>/tmp/result.out
    fi
done


This script reads the IP addresses / hostnames from /tmp/hosts file. The IP addresses have to be light this in /tmp/hosts file with 1 IP or hostname per line.

10.10.10.10
10.10.10.20 

When the script is executed it redirects the output to /tmp/result.out file as follows. As you can see it prints date and whether the hosts are up or down.

Fri Aug 23 02:02:01 UTC 2019
node 10.10.10.10 is up
node 10.10.10.20 is up
Fri Aug 23 02:03:01 UTC 2019
node 10.10.10.10 is up
node 10.10.10.20 is up
Fri Aug 23 02:04:01 UTC 2019
node 10.10.10.10 is up
node 10.10.10.20 is up


Products to which Article Applies


All UNIX environments

Additional References