How to concatenate comment + bash alias command call? - macos

I'm trying to print out my ip address along with a comment in front of it. I have no idea how to do it all in one single line.
6 alias showip="ifconfig | grep 'inet' | sed -n '5p' | tr -s ' ' | cut -d ' ' -f2"
11 ip=showip
12 ip="ip: $ip"
13 echo ip
The output I'm looking for is something along the lines of:
ip: 192.168.1.2
Thanks a bunch guys.
printf 'ip: %s\n' $(showip)
UPDATED: PROBLEM SOLVED

I am not sure I understand your question but you may be looking for:
printf 'ip: %s\n' $(ifconfig | grep 'inet' | sed -n '5p' | tr -s ' ' | cut -d ' ' -f2)

Related

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

Raspberry Pi Bash Script for network adapter, ip addresss and Mac address

I want to have a short cut to display the adapter, IP address and Mac address. I have the following:
#! /bin/bash
for iface in $(ifconfig | grep -v "lo" | cut -d ' ' -f1| tr '\n' ' ')
do
ipadd=$(ip -o -4 addr list $iface | awk '{print $4}' | cut -d/ -f1)
madd=$(ip -o link list $iface | awk '{print $17}')
printf "$iface\t$ipadd\t$madd\n"
done
The ethernet adapter doesn't show IP address and show as no such device. But if I run the command manually in bash it work and show up. The same script work correctly on my Ubuntu but not on Raspberry Pi (only manual command work). wlan0 works with no problem on Pi
The MAC address doesn't work at all, but if I run the command manually ip -o link list <adapter> | awk '{print $17}') it show the Mac address correctly.
Please advise where could have gone wrong.
Update:
+++ ifconfig
+++ grep -v lo
+++ cut -d ' ' -f1
+++ tr '\n' ' '
++ for iface in $(ifconfig | grep -v "lo" | cut -d ' ' -f1| tr '\n' ' ')
+++ ip -o -4 addr list enxb827ebe7229c:
+++ awk '{print $4}'
+++ cut -d/ -f1
Device "enxb827ebe7229c:" does not exist.
++ ipadd=
+++ ip -o -4 link list enxb827ebe7229c:
+++ awk '{print $17}'
Device "enxb827ebe7229c:" does not exist.
++ madd=
++ printf 'enxb827ebe7229c:\t\t\n'
enxb827ebe7229c:
++ for iface in $(ifconfig | grep -v "lo" | cut -d ' ' -f1| tr '\n' ' ')
+++ ip -o -4 addr list wlan0:
+++ awk '{print $4}'
+++ cut -d/ -f1
++ ipadd=192.168.1.4
+++ ip -o -4 link list wlan0:
+++ awk '{print $17}'
RTNETLINK answers: No such device
Cannot send link get request: No such device
++ madd=
++ printf 'wlan0:\t192.168.1.4\t\n'
wlan0: 192.168.1.4
If I run the command manually:
ip -o -4 link list enxb827ebe7229c | awk '{print $17}'
I get the Mac address
If I run this
ip -o addr list enxb827ebe7229c | awk '{print $4}' | cut -d/ -f1
I too will get the ipaddress correctly
A small fix was enough:
for iface in $(ifconfig | grep -v "lo:" | cut -d ' ' -f1 | cut -d: -f1); do
ipadd=$(ip -o -4 addr list $iface | awk '{print $4}' | cut -d/ -f1);
madd=$(ip -o link list $iface | awk '{print $17}');
printf "$iface\t$ipadd\t$madd\n";
done
The part ifconfig | grep -v "lo:" | cut -d ' ' -f1| tr '\n' ' ' leaves the : character in the output, so you are iterating over eth0: eth1: not over eth0 eth1. You need to remove the :, either with a simple cut -d: -f1 or tr -d: or any other mean.
Also note, as you've just discovered, the ifconfig output differs between platforms and implementations. It's better to just stick to the new ip command. Ex. ip a | sed -n '/^[^ ]*: \([^ ]*\):.*/{s//\1/;p;}'
There is no need for tr '\n' ' '. Shell interprets any whitespace character - that is tab, space or newline - as a word separator.

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 output to multiple variales

I'm creating a script in Bash to change all MAC addresses of my PC. I can list all network interfaces with this:
ip link | grep "<" | cut -d " " -f 2 | cut -d ":" -f 1 | grep -v lo
And the output of the script is:
eth0
wlan0
Now I need to create a variable for each network interface (to use it in the future), but I don't know how, and Google didn't help me...
Answer:
readarray -t interfaces < <(ip link | grep "<" | cut -d " " -f 2 | cut -d ":" -f 1 | grep -v lo)
echo "${interfaces[0]}" # prints eth0
echo "${interfaces[1]}" # prints wlan0
And to loop over them use for:
for curInterface in "${interfaces[#]}"; do
echo "$curInterface"
done
But there are better ways to parse data:
First of all, instead of grepping < character you can use -o flag. This will output all of the data on single lines. Then you simply need the second word without : character. This is very simple in pure bash:
interfaces=()
while read -r _ curInterface _; do
interfaces+=("${curInterface%:}")
done < <(ip -o link)
Store the output in an array:
interfaces=( $(ip link | awk '/</ { print $2 }' | awk -F: '!/lo/ {print $1}') )
You can create an array from this output, and loop through it after.
my_array=( $(ip link | grep "<" | cut -d " " -f 2 | cut -d ":" -f 1 | grep -v lo) )
You can also this exmaple giving different alternatives redirect output to array
And I could have it simpler like this with one awk command:
readarray -t youravar < <(exec ip link | awk -F': ' '/^[0-9]+:/&&!/ lo: /{print $2}')

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