Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This is a simple thing but I just can't get it to work.
I got a file with a list of subnets like this:
10.50
10.51
10.62
10.121
...then I cnf=`ifconfig` thus giving me the config of the NICs
...after that I want to compare the $cnf to see if the value of it is
listed in the file and if it is do things.
There might also be something better to use then the 'ifconfig' but it worked so I just stuck to it
First I just had one subnet but now it's starting to grow and I wanna make a list instead of having them all listed in the if-statement.
I know this gotta be possible since it's Linux but whatever I do won't work
well I can't post it since I haven't really save it
I just "test" it at the command prompt before I do.
I can post this at least:
Code:
if [ `ifconfig | grep -ci 10.200` != 0 ]; then
/bin/eject /dev/cdrom ; reboot -f
elif [ `ifconfig | grep -ci 10.70` != 0 ]; then
/bin/eject /dev/cdrom ; reboot -x
else echo Unknown network. Please tell me what to do...
and now each of the if statement will now have more then just one, so far we're up to 12 of subnets and it's still growing therefore I wanna put them in a file.
If you have a list of subnets in a file you can reverse the logic and see if a specific subnet (from the ifconfig command) is listed into the file itself. Example with a while read loop taking input from the output of ifconfig parsed with awk:
Code:
while read subnet
do
if grep -qw $subnet file
then
echo $subnet
fi
done < <(/sbin/ifconfig | awk '/inet addr:/ && ! /127.0.0.1/{sub(/inet addr:[0-9]+\.[0-9]+\./,""); print $1}')
where file in the grep command is your saved list. The awk line excludes the lo address from the search and extract the subnet (two last octets of the IP address). Hope this helps.
Actually not so complicated: you have two expressions to match your input line concatenated by the logical AND operator &&. It matches the regular expression /inet addr:/ and doesn't match (that is excludes, see the negation operator !) the loopback address /127.0.0.1/.
The rules is quite simple: the sub fuction substitutes the text matched by /inet addr:[0-9]+\.[0-9]+\./ with the null string. In other words it removes the string
Code:
inet addr:XXX.YYY.
from the input, leaving only the last two octets of the IP address into the first field. Hope it's a bit more clear.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.