Translate numbers (IP address) inside a variable (bash) - bash

My problem is that I'm having an IP address like 10.3.1.33
This IP address is inside a variable ip=10.3.1.33
Now I want to translate the 33 inside that IP address with a "*".
The "33" can change, so that this number has to be automatically put somewhere in a variable or so.....
I have no clue how to do that. Thanks for any advice :)

In your very specific case you could use:
$ ip="10.3.1.33"
$ printf "%s\n" "${ip/33/*}"
10.3.1.*
And to replace (remove) everything after the last period:
$ ip="10.3.1.33"
$ printf "%s\n" "${ip%.*}.*"
10.3.1.33
The later is POSIX compatible while the first is available in bash (among other shells)

I can think about this:
ip=1.2.3.4
ip1=${ip%.*}.*

Related

Printf adding extra space in shell script

I am writing a shell script to create file and write multiple lines to it.
#!/bin/bash
CURRENTHOST="$(hostname -I)"
touch genesis_start.sh
printf '%s\n\t''--http-server-address '"$CURRENTHOST"':8888 \' >> genesis_start.sh
There are more lines but I am just showing one.
The above code looks like in genesis_start.sh file:
--http-server-address 192.168.110.164 :8888 \
The extra space is added between 164 and 8888. How to display it without space?
There will always be space after the IP address, however, you can remove it like this :
CURRENTOST=${CURRENTHOST::-1}
Reference: https://unix.stackexchange.com/questions/144298/delete-the-last-character-of-a-string-using-string-manipulation-in-shell-script
Your code quality would really benefit from the help of the shellcheck.net linter.
There are many issues bugs with your script:
Misuse of printf (constant parts and variable formatter should go to the format string, there must be at least as many variable arguments as variable formatters)
Invalid escaping of single quote.
Wrong parsing of hostname -I which can return multiple space delimited entries.
Fixed your code:
#!/usr/bin/env bash
# Get the first IP address only
read -r CURRENTHOST _ < <(hostname -I)
printf '\t--http-server-address %s:8888\n' "$CURRENTHOST" > genesis_start.sh

On microblaze uclinux: put IP address to variable

Yes, this is related to Putting IP Address into bash variable. Is there a better way but nothing of the ideas there work for me on the microblaze uclinux.
I wish to have my ip address of eth0 stored to a shell variable that I can write a script using it. I need alternative ideas how to do this.
ifconfig is available if that helps.
I found that in the file /etc/config/dhcp0.conf the correct ip address is stored, here's the file's content:
1 192.168.10.102
How can I remove the 1 and space without using following commands
grep
sed
cut
this also does not work: echo ${variable:2}
You can use the shell's read built-in:
read num ip </etc/config/dhcp0.conf
$num will contain the number at the beginning of the line, $ip will contain the IP.
Assign ifconfig output of eth0 to array
ifout=($(ifconfig eth0))
Strip off everything before the semicolon of the 6th element of array and assign it to the variable $ethip
ethip=${ifout[6]#*:}

how to sum ip addresses

I'm creating a *.deb package that transform your wireless card into an hotspot.
I'm stuck at the configurations:
I have to write a postinst file in which I ask to the user what ip address he likes for his hotspot and then use it to generate the range & the subnet addresses for the isc-dhcp-server.
Something like that:
10.10.0.01 + 0.0.0.9 = 10.10.0.10
I know how to assign strings and numbers to variables and how to ask to user his choosen IP, but how to modify a variable and assign the result to another one? expr thinks it's a floating number and won't work.
Hoping that everything it's clear enough,
waiting for a help,
thank you in advance
Avoid leading zeros.
IFS="." read -a a <<< 10.10.0.1
IFS="." read -a b <<< 0.0.0.9
s="$[a[0]+b[0]].$[a[1]+b[1]].$[a[2]+b[2]].$[a[3]+b[3]]"
echo $s
Output:
10.10.0.10
Ok, I found a workaround method:
when I ask to the user its choosen ip I use these:
IFS="." read -r a b c d
choosenip="$a.$b.$c.$d"
subnetip="$a.$b.$c.0"
rangeipmin="$a.$b.$c.20"
rangeipmax="$a.$b.$c.30"
IFS change the default "space" or "tab" to whatever you want.
So when I have to put these in the dhcpd.conf with "echo", I just have to call the variables.
If you have more elegant ways to do that, you're welcome.
Thank you

Issue while printing variables using echo

I have an issue printing two variables using echo. Below is the code snippet from the script:-
tdaydatefile=$(date +'%m%d%Y')
ip=$(ip route get 8.8.8.8 | awk 'NR==1 {print $NF}')
echo "Dumping all network connections to $HOME/MyLog/netstat_$ip_$tdaydatefile.csv!"
When the script is run, it only prints below:-
Dumping all network connections to /root/MyLog/netstat_12022014.csv!
It doesn't print the ip address (ip variable) of the system. If I replace ip with tdaydatefile in echo, it prints ip and ignores tdaydatefile variable.
Any idea what could be wrong here?
Since _ is a valid variable-name character, you need to tell the shell where your variable name ends. (In your code, the shell interprets ip_ as the variable name.)
This can be done by enclosing the variable name in {...}, i.e., use ${ip} in this case:
echo "Dumping all network connections to $HOME/MyLog/netstat_${ip}_$tdaydatefile.csv!"
Note that it's not necessary with $HOME or $tdaydatefile , because / and . are not a valid variable-name chars.
When in doubt, however, use ${...} — it also helps readability.

to which subnet IP address belongs?

I have to write a script in bash , perl or python.
I got file with three columns (for manually manage connect-proxy)
SUBNET/IP socks_port socks_ip
1.2.3.* 1080 9.8.7.6
1.1.* 1080 6.8.7.6
I want to know to which subnet belongs IP address,
for example:
$ my_script 1.1.1.2
this IP belongs to 1.1.* subnet so I want back second line.
BASH: quick and dirty, use cut, then grep over the file.
PYTHON: use ip.rsplit() and then line.split()[].startswith() iterating through the file.
PERL: no idea.
Cheers!
If the file is in the format given (i.e. using *), it'll be fairly easy to use bash pattern matching to compare this to the ip address. However, as #Mark Drago pointed out, this format anything except octet-boundary subnets, so if you need to support arbitrary subnet boundaries, you need a better format.
Assuming you are using the "1.2.*" format, this should work:
#!/bin/bash
ip="$1"
found_match=false
while read subnet socks_port socks_ip; do
if [[ "$ip" == $subnet ]]; then # this'll do glob-style pattern matching against $subnet
echo "subnet=$subnet, socks_port=$socks_port, socks_ip=$socks_port"
found_match=true
break # assuming you don't want to check for multiple matches
fi
done </path/to/subnet/file
if ! $found_match; then
echo "No match found in subnet file"
fi

Resources