On microblaze uclinux: put IP address to variable - bash

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]#*:}

Related

How to split a URL and Port stored in a variable and separate them into two variables URL and Port in bash

I was not able to find a simple example of how to split an URL from port number after extracting them from an API call. So I wanted to put it here for anyone else looking for an answer.
APP_ENDPOINT=htttps://www.someURL.com:3333
I need to separate the URL from the PORT to make another call with them separated such as to ssh into the server.
This is the answer I came up with, does anyone have a more succinct answer?
Also this was for URL and port number but should work for IP:port # as well.
I used grep with regex to put the port # in one variable and sed to remove the the : and port # from the endpoint variable.
APP_ENDPIONT=https://www.someURLorIP:port#
APP_PORT=$(grep -Eo "[0-9]+$" <<< $APP_ENDPOINT)
APP_ENDPOINT=$(echo $APP_ENDPOINT | sed "s/:[0-9]*//")
This will give you two variables with the URL/IP and the port in their respective variables.
I will do like this:
APP_PORT=${APP_ENDPIONT##*:}
APP_ENDPOINT=${APP_ENDPIONT%:*}
And you can lookup the meaning of ${APP_ENDPIONT##*:} and ${APP_ENDPIONT%:*} in what-is-the-meaning-of-the-0-syntax-with-variable-braces-and-hash-chara

Translate numbers (IP address) inside a variable (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%.*}.*

shell command line to modify a configuration file

I searched this but did not find answer for my problem.
I have a configuration file, which has a line like
server_ip=xxx.xxx.xxx.xxx
This file is in every node in a cluster. The server_ip is just an IP address which sometimes needs to change. I understand that it is easy to change the server_ip to a new ip address if the old ip address is given. But I don't want to keep tracking the old IP address. I just want to force the server_ip address to change to a new value. So the point here is that we don't want to use xxx.xxx.xxx.xxx part to recognize this line. Instead we can only use 'server_ip=' as the locator, and after this line being located, we just set the server_ip to a new value.
Thanks.
GNU sed has an in-place option -i. The command is a simple substitute which replaces each string that matches with the new string. We use double quotes so that we can access the environment variable containing the new address. Because of the double quotes we must escape the $ which denotes the EOL in the match pattern or else the shell would try to see a variable name after it.
I didn't test this, so it's probably wrong, but should get you close. (updated to include OP's fixes.)
newIp=1.2.3.4; sed -i bak "s/server_ip=.*\$/server_ip=$newIp/g" configfile
Use SED!
echo "server_ip=xxx.xxx.xxx.xxx" > file.txt
sed 's/server_ip=.*/server_ip=NEW_IP/g' < file.txt > tmpFile

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