After the success view of the host ( IPs ) I need to ping them in order to check if they are up. SIDS file contains 2 columns with hostnames. Are there any suggestions on how to Iimprove the code below?
#!/bin/bash
LINES=`cat /home/marko/SIDS | sed "s!/!-!g" | wc -l`
for (( i=1; i<=${LINES}; i++))
do
FIRSTIP=CPE-`sed -n "${i}{p;q}" /home/marko/SIDS | awk '{print $1}'| sed "s!/!-!g"`
SECONDIP=CPE-`sed -n "${i}{p;q}" /home/marko/SIDS | awk '{print $2}'| sed "s!/!-!g"`
COUNT=$( host ${FIRSTIP} | grep address | wc -l )
if [ $COUNT -gt 0 ]
then
echo success
else
echo ${SECONDIP}
fi
done
You can just use dig, to avoid searching the output of host:
IP=$(dig +short $SERVERNAME)
Then to check, if the host is alive:
if ping -q -c $IP >/dev/null 2>&1
then
echo "OK"
fi
Related
I'm grepping a live log and need to compare the response time. If the response time is greater than 5000, "Response time greater then 5000" needs to be printed on screen preferable with the response time.
Current script :
tail -f request-response-logger.log| grep getSubscriberTypeResponse | awk -F 'RESPONSE_TIME:' '{print $2}'| awk -F ',BODY:<soapenv:Body' '{print $1}'
Output:
255.0
289.0
352.0
236.0
365.0
520.0
472.0
528.0
560.0
522.0
557.0
586.0
493.0
I tried below code snippet. But it didn't work
if [$(grep getSubscriberTypeResponse | awk -F 'RESPONSE_TIME:' '{print $2}'| awk -F ',BODY:<soapenv:Body' '{print $1}') -gt 100]; then echo "greater than 100"; fi
I tried below command
tail -f request-response-logger.log | awk -F'[,:]' '{ if($13 > 5000.0 ) { print } }'| grep 'TRANSACTION:response' | grep getSubscriberTypeResponse
I have put together the code below to find all resources with a network tag that contains -allowaccess however it doesn't seem to work...
for i in $(gcloud projects list | awk NR>1); do gcloud compute instances list --filter="tags.items:-allowaccess --project=$i; done
Any ideas?
A colleague of mine figured it out...here's the command - hope it's useful to others!
for i in $(gcloud projects list | awk '{print $1}' | awk 'NR>1'); do echo PROJECT: $i && echo "--" && gcloud compute instances list --project=$i --filter="(tags.items:allowaccess)" && echo ""; done
For each project, this outputs each VM with a network tag that contains the text 'allow access'
Try something alike --filter="label:(*allowaccess)" or --filter="labels.*allowaccess:*", because these are generally instance labels. See gcloud topic filters.
I think the code self explain :)
# indice of .csv
echo "project;machine;region;family;value1;value2;value3;value4;value5" >> export.csv
# loop projects
for p in $(gcloud projects list | awk '{print $1}' | awk 'NR>1')
do
# loop values of instance
for i in $(gcloud compute instances list --project=${p} | grep -v "TERMINATED" | grep -v "NAME")
do
if [ "${i}" == "RUNNING" ]
then
echo ${instance}
X=0
elif [[ $X -eq 0 ]]
then
echo -n ${i}
echo -n ";"
echo -n ${i}
echo -n ";"
X=$((X+1))
else
echo -n ${i}
echo -n ";"
X=$((X+1))
fi
done
done >> export.csv
# remove wrong ;
sed -i 's/,;/ /g' export.csv
sed -i 's/;vCPU/ vCPU/g' export.csv
sed -i 's/;GiB/ GiB/g' export.csv
I have a bash script that SSH'es to a list of servers (given a .txt file), runs another script inside each server, and shows the results. But I need to parse the verbose data from output, and eventually save some meaningful results as a .CSV file.
Here is my main script:
set +e
while read line
do
ssh myUser#"$line" -t 'sudo su /path/to/script.sh' < /dev/null
done < "/home/listOfServers.txt"
where the listOfServers.txt is like
server1
server2
server3
The output of running my script looks like this, showing the results for each servers one after another.
SNAME:WORKFLOW_APS_001 |10891 | Alive:2018-06-18:06:54 |TCP
SNAME:WORKFLOW_APSWEB_001 |11343 | Alive:2018-06-18:06:54 |TCP
Processes in Instance: WORKFLOW_OHS_002
WORKFLOW_OHS_002 | OHS | 8925 | Alive | 852960621 | 1367120 | 510:11:51 | http:9881
Processes in Instance: WORKFLOW_OHS_003
WORKFLOW_OHS_003 | OHS | 9187 | Alive | 2041606684 | 1367120 | 510:11:51 | http:9883
SNAME:WORKFLOW_RPSF_001 |10431 | Alive:2018-06-18:06:55 |TCP
SNAME:WORKFLOW_SCPTL_001 |9788 | Alive:2018-06-18:06:55 |TCP
...
From this output, I only need the OHS names and their status, and save along with the original server's name as a CSV. The pattern to me looks like this: I need to look at each line, and if the line doesn't contain "Processes in Instance" or "SNAME", then split based on space, and grab the 1st (OHS name) and 4th field (status). So my CSV will look like:
server1, WORKFLOW_OHS_002, Alive
server1, WORKFLOW_OHS_003, Alive
server2, .....
...
How can I modify my bash to do this?
You can use awk:
while read -r line; do
ssh myUser#"$line" -t 'sudo su /path/to/script.sh' < /dev/null |
awk -v s="$line" -F '|' -v OFS=', ' '!/^[[:blank:]]*SNAME:/ && NF>2 {
gsub(/^[[:blank:]]+|[[:blank:]]+$/, "");
gsub(/[[:blank:]]*\|[[:blank:]]*/, "|");
print s, $1, $4
}'
done < "/home/listOfServers.txt"
EDIT: As per your comment below, you can do this to handle error conditions:
while read -r line; do
out=$(ssh myUser#"$line" -t 'sudo su /path/to/script.sh' < /dev/null 2>&1)
if [[ -z $out ]]; then
echo "$line, NULL, NULL"
elif [[ $out == *"timed out"* ]]; then
echo "$line, FAIL, FAIL"
else
awk -v s="$line" -F '|' -v OFS=', ' '!/^[[:blank:]]*SNAME:/ && NF>2 {
gsub(/^[[:blank:]]+|[[:blank:]]+$/, "");
gsub(/[[:blank:]]*\|[[:blank:]]*/, "|");
print s, $1, $4
}' <<< "$out"
fi
done < "/home/listOfServers.txt"
Something to try - and good luck.
while read line
do ssh myUser#"$line" -t 'sudo su /path/to/script.sh' < /dev/null |
sed -E "/Processes in Instance/d; /SNAME/d;
s/^ *([^| ]*) *[|][^|]*[|][^|]*[|] *([^| ]*).*/$line,\1,\2/;"
done < "/home/listOfServers.txt"
You ought to be able to improve on that. :)
I have to grep from a file name temp which has something like this
Process State
BE_RP:1 [PL_2_3] Running
BE_RP:2 [PL_2_4] Running
BE_RP:3 [PL_2_5] Running
BE_RP:4 [PL_2_6] Running
FE_SCTP:0 [PL_2_3] Running
FE_SCTP:1 [PL_2_4] Running
BE_NMP:0 Not Running
OAM:0 Running
I need to write a egrep statement which will return the number of process which are in running or not running state.
awk '/^OAM/ { next } /Not Running[ \t]*$/{s++} END {print s, NR-s-1}' foo.txt
Prints <running> <not running>
Running
$ grep -v 'OAM' input | grep -cP '(?<!Not) Running\s*$'
6
Not Running
$ grep -v 'OAM' input | grep -cP 'Not Running\s*$'
1
sed '{
1 d
s/^[^:]*:[0-9]*[ ]*//
s/^[^]]*]//
s/^[ ]*//
}' input_file | sort | uniq -c
grep -P '^(?!OAM:0).*Running' temp | cut -f2 | wc -l
I am trying to get the following formatted output out of ifconfig:
en0: 10.52.30.105
en1: 10.52.164.63
I've been able to at least figure out how to get just the IP addresses (weeding out localhost) with the following command, but it's not sufficient for my requirements:
ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'
Thanks!
This works on FreeBSD, which is at the heart of an apple :-)
#!/bin/sh
for i in $(ifconfig -l); do
case $i in
(lo0)
;;
(*)
set -- $(ifconfig $i | grep "inet [1-9]")
if test $# -gt 1; then
echo $i: $2
fi
esac
done
On Debian/RHEL systems you can do the following ---
#!/bin/sh
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "Interface: IP : MASK : BROADCAST : HWADDR"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
for i in $(ifconfig -a| grep -v ^$| grep ^[a-z*] | awk '{print $1}')
do
case $i in
(lo)
;;
(*)
ip=`(/sbin/ifconfig $i | awk /'inet addr/ {print $2}' | cut -f2 -d":" )`
bcast=`(/sbin/ifconfig $i | awk /'Bcast/ {print $3}' | cut -f2 -d":" )`
mask=`(/sbin/ifconfig $i | awk /'inet addr/ {print $4}' | cut -f2 -d":" )`
hwaddr=`(/sbin/ifconfig $i | awk /'HWaddr/ {print $4,$5}' | cut -f2 -d" " )`
if [ -z $ip ]; then
ip="NA"
fi
if [ -z $bcast ]; then
bcast="NA"
fi
if [ -z $mask ]; then
mask="NA"
fi
if [ -z $hwaddr ]; then
hwaddr="NA"
fi
echo $i: $ip : $mask : $bcast : $hwaddr
;;
esac
done