How to pass output to Ping as stdin - shell

I have this command to strip the https:// portion and truncate the ending /.
echo https://example.com/ | awk -F '//' {'print $2'} | tr -d '/'
How can I pass the output to ping or host command as a stdin, making it an oneliner?
example.com
The expected output is the result of host/ping example.com
I have tried below commands
echo https://example.com/ | awk -F '//' {'print $2'} | tr -d '/' | ping $
echo https://example.com/ | awk -F '//' {'print $2'} | tr -d '/' | ping $1
I never got the expected results.

I found the answer, it can be achieved by piping using xargs
echo https://example.com/ | awk -F '//' {'print $2'} | tr -d '/' | xargs ping
echo https://example.com/ | awk -F '//' {'print $2'} | tr -d '/' | xargs host

You could try this:
echo https://example.com/ |(IFS=/ read http nothing host; ping $host)
IFS splits the reading at the slash character; the parenthesis are needed for read to work. If you just need the ping the above command is ok. Or, you can extract the host name:
host=$(echo https://example.com/ |(IFS=/ read http nothing host; echo $host))
ping $host

Related

Bash : Curl grep result as string variable

I have a bash script as below:
curl -s "$url" | grep "https://cdn" | tail -n 1 | awk -F[\",] '{print $2}'
which is working fine, when i run run it, i able to get the cdn url as:
https://cdn.some-domain.com/some-result/
when i put it as variable :
myvariable=$(curl -s "$url" | grep "https://cdn" | tail -n 1 | awk -F[\",] '{print $2}')
and i echo it like this:
echo "CDN URL: '$myvariable'"
i get blank result. CDN URL:
any idea what could be wrong? thanks
If your curl command produces a trailing DOS carriage return, that will botch the output, though not exactly like you describe. Still, maybe try this.
myvariable=$(curl -s "$url" | awk -F[\",] '/https:\/\/cdn/{ sub(/\r/, ""); url=$2} END { print url }')
Notice also how I refactored the grep and the tail (and now also tr -d '\r') into the Awk command. Tangentially, see useless use of grep.
The result could be blank if there's only one item after awk's split.
You might try grep -o to only return the matched string:
myvariable=$(curl -s "$url" | grep -oP 'https://cdn.*?[",].*' | tail -n 1 | awk -F[\",] '{print $2}')
echo "$myvariable"

Check ip is present within file & update iptables bash script

I want to check that the ip is present within the ccd folder and push the ip route to the FORWARDING chain in the iptables. Im new to bash scripting and need a little help finishing this script.
client file in /etc/openvpn/ccd :
ifconfig-push 10.8.0.45 255.255.255.0
push 'route 10.10.0.45'
I need to grep 10.8.0.45 & 10.10.0.45
and push those routes in the iptables.
e.g
iptables -A FORWARD -s 10.8.0.45 -d 10.10.0.45 -j ACCEPT
client-connect /etc/openvpn/on_connect.sh
script I need help with 'grep' or 'awk'
static_ip= cat $CCD_DIR/$common_name | grep -w "ifconfig-push" | awk -F ' ' {'print $2'}
ip_destination=cat $CCD_DIR/$common_name | grep -w "push 'route" | awk -F ' ' {'print $3'} | tr -d "'"
#!/usr/bin/env bash
#
# Add iptables rules based on CCD client config.
#
CCD_DIR="/etc/openvpn/ccd"
RULE_COMMENT="FORWARD"$common_name
static_ip=cat $CCD_DIR/$common_name | grep -w "ifconfig-push" | awk -F ' ' {'print $2'}.
ip_destination=cat $CCD_DIR/$common_name | grep -w "push 'route" | awk -F ' ' {'print $3'} | tr -d "'"
if [ -f $CCD_DIR/$common_name ]; then
sudo iptables -A FORWARD -s $static_ip -d ip_destination -j ACCEPT
fi
exit 0
Edit: I think my usage of cat is wrong .
Try like this.
static_ip=$( cat $CCD_DIR | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | grep -E '(^|\s)10.8.0.45($|\s)' )
ip_destination=$( cat $CCD_DIR | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | grep -E '(^|\s)10.10.0.45($|\s)' )
So first you grep all IP's address in file and then you search exactly what you need
Edited after your comment.
If I understand correctly,
"ifconfig-push" - Is only one peer ccd file ?
so you can use this :
static_ip=$( grep -w "ifconfig-push" | awk -F ' ' {'print $2'})
For the rest in "push route" you need to use loop to find all matching ip address and put them to the iptables.
if [ -f $CCD_DIR/$common_name ]
then
cat $CCD_DIR | awk -F 'route' {'print $2'} | awk -F ' ' {'print $1'} | sed '/^$/d' | grep -E "\b(10)\.(8)\.(0)\.|(10)\.(10)\.(0).\b" | while read ip_destination
do
sudo iptables -A FORWARD -s $static_ip -d ip_destination -j ACCEPT
done
fi

grep multiple pattern from read input

so i made a bash script the greps the name of the host on the redirected file. However, there are hosts that are named either with "-" or "_"
GTR_SRV123_EST
GTR-SRV123-EST
Right now, what i did was, grep just a portion of the FQDN, like SRV123
Is there a way i can grep the host even if i just put the FQDN GTR_SRV123_EST and it will still matched this GTR-SRV123-EST.
i have a prompt that ask for the hostname:
echo -n "Please enter the host: "
read $host
grep -i $host ${temp}/*
update:
so had it working with the help of Juan's command. However, the directory path is displayed on the output. How can i get rid of it.
/export/home/aa12s/GLB-TXU/temp/
Current output:
/export/home/aa12s/GLB-TXU/temp/GBL-ASA-A:100022FBC0D00038 gbl-asa-a-mode1 5005076801103673 active gbl-ac-wbg02
Desired output:
GBL-ASA-A:100022FBC0D00038 gbl-asa-a-mode1 5005076801103673 active gbl-ac-wbg02
Command:
grep -iE "$(echo $host| awk -F '/export/home/aa12s/GLB-TXU/temp/' '{$2=$1;a=gsub(/_/, "-",$2); print $1"|"$2}' 2>/dev/null)" ${temp}/*
Edit your pattern.
echo -n "Please enter the host: "
read host # Edit: not $host
host="${host//[_-]/\[_-\]}" # turn either into a *check* for either
grep -i "$host" ${temp}/*
Kind of hacky but give this a try:
grep -Ei "$(echo $host| awk '{$2=$1;gsub(/_/, "-",$2);print $1"|"$2}' 2>/dev/null)" ${temp}/*
To get rid of filepaths:
grep -iE "$(echo $host| awk '{$2=$1;gsub(/_/, "-",$2);print $1"|"$2}' 2>/dev/null)" ${temp}/* 2>/dev/null|awk -F \/ '{print $NF}'
NOTE: Slashes must NOT be present in the file content.
If there is no host name with both _ and - below will work.
Entered host contains only _
grep -iE $(echo $host | tr "_" "-")\|$host ${temp}/*
Entered host contains only -
grep -iE $(echo $host | tr "-" "_")\|$host ${temp}/*
Entered host contains both _ and -
grep -iE $(echo $host | tr "_" "-")\|$(echo $host | tr "-" "_")\|$host ${temp}/*
You can use backreference :
([_-]) : capture either _ ou - in group 1
\1 : reference group 1
try this command :
grep -iE "([_-])$host\1" ${temp}/*
https://regex101.com/r/uH5SHC/1/
Wiyh host=SRV123, you will capture :
GTR_SRV123_EST
GTR-SRV123-EST
and not
GTR_SRV123-EST

Bash script any reason why it wont write the file

I am helping debug some code that exec the following script is there any reason why its not writing a file to the server? - if that what it does. All the $ data and permissions are ok:
Script:
#!/bin/bash
RANGE=$1
ALLOCATION=`echo $RANGE | cut -f1,2,3 -d'.'`
/sbin/ip rule add from $1 lookup $2
echo $ALLOCATION
rm /path/too/file/location/$ALLOCATION
for i in `seq 3 254`
do
echo $ALLOCATION.$i >> /path/too/file/location/$ALLOCATION
done
ETH=`/sbin/ifconfig | grep eth0 | tail -n1 | cut -f2 -d':' | cut -f1 -d' '`

Bash new variable with other variable

I get the ip address like that :
Ip=`ifconfig | grep inet | grep -v -E 'inet6|127.0.0.1' | \
tr -d [:alpha:] | tr -s [:space:] | cut -d: -f2`
I have an ip like this for instance : 10.1.0.76
I want to make a new variable with the Ip variable to have another ip, for instance my new variable will return : 10.1.0.178
Just the last number change, so I want to get just a part of Ip variable (10.1.0.) and add another number to the end.
I tried with sed but I always have mistakes like "there's no file call'd ..."
Can you help me ?
You can use parameter expansion: It's simply: ${Ip%.*}.178
${Ip%.*} is the ip with the last dot and everything after it removed. The .178 is what you want to append after that.
Here it is in context:
# Your original expression
Ip=`ifconfig | grep inet | grep -v -E 'inet6|127.0.0.1' | \
tr -d [:alpha:] | tr -s [:space:] | cut -d: -f2`
# assign a new variable with the ip with different end octet
newIp=${Ip%.*}.178
# Show new ip
echo "$newIp"
Well, given that you have IP in a format x.y.z.w, you can use perl regex:
$ echo "120.20.31.78" | perl -pe 's/(.*)\..*/$1\.123/'
120.20.31.123
This will repace last number ("78") with "123".
So, in your case (assuming your "Ip" variable is set correctly), it would be:
Ip=ifconfig | grep inet | grep -v -E 'inet6|127.0.0.1' | tr -d [:alpha:] | tr -s [:space:] | cut -d: -f2 | perl -pe 's/(.*)\..*/$1\.123/'
see this, I hope it is what you want:
kent$ echo $ip
10.1.0.76
kent$ echo $part
178
kent$ sed -r "s/(.*\.).*/\1$part/" <<< $ip
10.1.0.178
to set $ip with new value:
kent$ ip=$(sed -r "s/(.*\.).*/\1$part/" <<< $ip)
kent$ echo $ip
10.1.0.178

Resources