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
Related
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]#*:}
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%.*}.*
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
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.
I am working on a BASH shell script where I need to be able to check and see if the IP address returned from:
dig $HOSTNAME +short
is within a particular subnet or not. For example, if part of 192.168.10.x or 10.130.10.x then do z else do y.
I am not sure how to manipulate or check the IP address after I have it stored in a variable so that I can build out the logical test described above.
If your subnets are full class C then you can just do a substring check:
if [ ${IP#192.168.10.*} == ${IP} -a ${IP#10.130.10.*} == ${IP} ]
then
echo "not in either subnet"
else
echo "in one of the subnets"
fi
Edit: Note, this of course doesn't validate that the IPs are valid, but it alleviates the need for external tools.
I ended up using the following code to make it work:
if [[ "$IP" == *10.130.10.* || "$IP" == *192.168.10.* ]]; then
mount code goes here
fi
Thanks for everyone's help and explaining things to me to allow me to further learn about scripting. It is really appreciated!