Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

script question

Posted on 2014-11-15
2
Medium Priority
?
278 Views
Last Modified: 2015-01-09
Can you please help me write a shell script for the following

when I execute the following i should be getting this output as follows

I need to take this argument and split the 0xffffff00 as follow

ff ff ff 00 --> 255.255.255.0   (how do i convert this to this way)



 $ ./network.sh 172.16.200.123/0xffffff00 -hex
            0xac10c8ff
0
Comment
Question by:linuxperson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
savone earned 1000 total points
ID: 40444925
This was fun to figure out and I learned a lot.  I am sure there might be a better way to do this, but seems to work.

Here is my test:
$ ./convert.sh 172.16.200.123/0xffffff00
Here is the conversion
255.255.255.0

Here is the code I used:
#!/bin/bash
ARG1=`echo $1 | cut -d/ -f2 | tr '[:lower:]' '[:upper:]'`
pair=($(echo $ARG1|sed  's/\(..\)/\1 /g'))

p1="$(echo -e "ibase=16; ${pair[1]}" | bc)" 
p2="$(echo -e "ibase=16; ${pair[2]}" | bc)"
p3="$(echo -e "ibase=16; ${pair[3]}" | bc)"
p4="$(echo -e "ibase=16; ${pair[4]}" | bc)"

echo "Here is the conversion"
echo $p1\.$p2\.$p3\.$p4

Open in new window

0
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 1000 total points
ID: 40446315
Hi linuxperson,

I'm not totally clear on the bottom line of your question, but if you want to take this as input:
    172.16.200.123/0xffffff00
and convert it to this:
    255.255.255.0
(as savone seems to think), then you could do it in Perl like this:
    echo "172.16.200.123/0xffffff00" | perl -ne '/0x(.+)/; map{push @dec, hex} unpack "(A2)*",$1; print join(".",@dec)'

But if you really want a shell solution, then savone's might be the way to go.
0

Featured Post

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question