Our vBulletin migration is complete.
Welcome vBulletin users! All content and user information from the Micro Focus Forums (vBulletin) site has been migrated to this site. READ MORE.
Sarahjohn_388 Contributor.
Contributor.
282 views

getting network id from ip address & subnetmask

I am trying to learn some shell scripting and I though it would be a nice way to combine it with my ccna study. So I am writing a simple script that scans some network devices based upon a ip address and a subnetmask provided by the user. To determine the network a host belongs to I use the following lines of code, but it keeps getting me errors.

read -p "Geef een IP-adres op " i
read -p "Geef een subnetmask op " s

IFS=. read -r i1 i2 i3 i4 <<< $i
IFS=. read -r m1 m2 m3 m4 <<< $s

ip=`printf "%d.%d.%d.%d\n" $i1 $i2 $i3 $i4`
mask=`printf "%d.%d.%d.%d\n" $m1 $m2 $m3 $m4`

#bepaal netwerk id
id=`printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"`
The error is produce is as follows:

192 168 178 15 & 0xff 0xff 0xff 0x00: syntax error in expression (error token is "168 178 15 & 0xff 0xff 0xff 0x00")
Anyone here that can tell me why?

0 Likes
1 Reply
Highlighted
Vincent_M_NNM Acclaimed Contributor.
Acclaimed Contributor.

Re: getting network id from ip address & subnetmask

Hello Sarahjohn

Thanks for posting,

I am not a script expert, but I think that instead of using

IFS=. read -r i1 i2 i3 i4 <<< $i
IFS=. read -r m1 m2 m3 m4 <<< $s

You can use

IFS=. read -r i1 i2 i3 i4 <<< "$i"
IFS=. read -r m1 m2 m3 m4 <<< "$s"

and see if that works?

Regards,

Vincent Montenegro Mena
Customer Support Engineer

If you find that this or any other post resolves your issue, please be sure to mark it as an accepted solution.
If you are satisfied with anyone’s response please remember to give them a KUDOS by clicking on the STAR at the bottom left of the post and show your appreciation.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.