Solved

script question

Posted on 2014-11-15
2
266 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
2 Comments
 
LVL 23

Accepted Solution

by:
savone earned 250 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 11

Assisted Solution

by:tel2
tel2 earned 250 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now