DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 2nd February 2011
isthistheend isthistheend is offline
New User
 
Join Date: Feb 2011
Posts: 4
Unhappy Combining outputs?

Hi,
i'm new at shell scripting.. i need to combine different syntax outputs.
I try to get interface name with first syntax and ip add with second one..

ifconfig |grep -E 'flags' |grep -v lo |grep -v enc |awk -F":" '{print $1}'

ifconfig |grep -E 'inet' |grep -v inet6 | grep -v 127.0.0.1 |awk '{print $2}'

i need to combine their outputs like;

"İnterface name" "ip address"

i bet solution is too easy but i stuck D:
Please Help.. Thanks a lot..
Reply With Quote
  #2   (View Single Post)  
Old 2nd February 2011
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 3,318
Default

Quote:
Originally Posted by isthistheend View Post
i bet solution is too easy
This sounds like a homework problem.

I hope others let you respond before jumping in providing answers. You will get more out of the exercise if you work it out yourself as opposed to simply letting someone else supply you an answer.

In fact, there are several ways this script can be written.
Quote:
i need to combine their outputs like;

"İnterface name" "ip address"
So, let me paraphrase the information presented. There are two values you have determined.
  • How would you assign the result of each to a variable?
  • How would you print two different variables on the same line?
For bonus points, a very basic introduction to shell scripting can be found at the following:

http://steve-parker.org/sh/sh.shtml

The answers to your questions can be found there too, but you may need to think about the fundamental problems first.

Last edited by ocicat; 2nd February 2011 at 09:52 AM.
Reply With Quote
  #3   (View Single Post)  
Old 2nd February 2011
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,128
Default

You are close. You only have to glue them together.

Code:
#!/bin/sh

name=$(ifconfig |grep -E 'flags' |grep -v lo |grep -v enc |awk -F":" '{print $1}')
echo "The interfaces are: $name"

#ip=$(ifconfig |grep -E 'inet' |grep -v inet6 | grep -v 127.0.0.1 |awk '{print $2}')

for X in $name ; do
    ip=$(ifconfig $X  | grep 'inet ' | awk '{print $2}')
cat <<END
Interface name: $X  IP address: $ip
END
done
Set the variable 'name' to the names of the NICs. (Using your grep/awk wizardy)
Just echo this variable to check
Now loop over these names, do an 'ifconfig' for each of them and filter the 'inet ' line to extract the IP address.

The output in my case
Code:
The interfaces are: bge0
re0
Interface name: bge0  IP address: 
Interface name: re0  IP address: 192.168.222.20
My ifconfig output:
Code:
$ ifconfiglo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33160
        priority: 0
        groups: lo
        inet 127.0.0.1 netmask 0xff000000
        inet6 ::1 prefixlen 128
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x4
bge0: flags=8802<BROADCAST,SIMPLEX,MULTICAST> mtu 1500
        lladdr 00:10:18:00:9f:fd
        priority: 0
        media: Ethernet autoselect (none)
        status: no carrier
re0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        lladdr 00:19:db:47:b0:4c
        priority: 0
        groups: egress
        media: Ethernet autoselect (100baseTX full-duplex,rxpause,txpause)
        status: active
        inet 192.168.222.20 netmask 0xffffff00 broadcast 192.168.222.255
        inet6 fe80::219:dbff:fe47:b04c%re0 prefixlen 64 scopeid 0x2
enc0: flags=0<> mtu 1536
        priority: 0
pflog0: flags=141<UP,RUNNING,PROMISC> mtu 33160
        priority: 0
        groups: pflog
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #4   (View Single Post)  
Old 2nd February 2011
isthistheend isthistheend is offline
New User
 
Join Date: Feb 2011
Posts: 4
Lightbulb

Thank you ocicat and J65nko..
i was thinking about using "for" but i wasnt sure how to use it. I get all of code now. i add the code
Code:
#!/bin/sh

name=$(ifconfig |grep -E 'flags' |grep -v lo |grep -v enc |awk -F":" '{print $1}')
ip=$(ifconfig |grep -E 'inet' |grep -v inet6 | grep -v 127.0.0.1 |awk '{print $2}')
status=$(ifconfig | grep status | awk '{print $2}')

for X in $name ; do
    ip=$(ifconfig $X  | grep 'inet ' | awk '{print $2}')
    status=$(ifconfig $X | grep status | awk -F ":" '{print $2}')
cat <<END
Interface name: $X  IP address: $ip Status: $status
END
done
my output:
Code:
Interface name: bge0  IP address: 10.10.35.12 Status: active
Interface name: bge1  IP address:  Status: no carrier
Now i'm looking for adding "No IP" or something like that when interface have no ip or carrier, like:
Interface name: bge1 IP address:"No Ip Address" Status: no carrier

i think "if" will solve the stuation (:
Thank you both again..
Reply With Quote
  #5   (View Single Post)  
Old 2nd February 2011
s0xxx's Avatar
s0xxx s0xxx is offline
Package Pilot
 
Join Date: May 2008
Posts: 192
Default

Fully respecting what @ocicat has suggested, now that J65nko has modified OPs attempt, I'll try with awk only:
Code:
$ ifconfig -a | awk ' \
/lo/||/enc/      {next}
/flags/          {iface=$1}
/inet /&&iface   {buf[iface]=$2}
END{for(i in buf)printf("%s\t%s\n", i, buf[i])}'
__________________
The best way to learn UNIX is to play with it, and the harder you play, the more you learn.
If you play hard enough, you'll break something for sure, and having to fix a badly broken system is arguably the fastest way of all to learn. -Michael Lucas, AbsoluteBSD
Reply With Quote
  #6   (View Single Post)  
Old 2nd February 2011
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Default

^ good one
Reply With Quote
  #7   (View Single Post)  
Old 3rd February 2011
isthistheend isthistheend is offline
New User
 
Join Date: Feb 2011
Posts: 4
Default

Thank you all for your replies..
Reply With Quote
  #8   (View Single Post)  
Old 3rd February 2011
s0xxx's Avatar
s0xxx s0xxx is offline
Package Pilot
 
Join Date: May 2008
Posts: 192
Default

@isthistheend

I was pretty sure my message came before yours yesterday...

Anyway, I just redesigned awk script a bit to add "no IP" output and a few comments:

Code:
# First we skip interfaces we don't want shown
# Comment out if want to display them too

/lo0/||/enc/{next}

# We remember interfaces in iface variable, and increase gotif
# If we get more that 2 interfaces it means last one doesn't have an address
# so we put it in noip array and decrease the gotif counter

/flags/{
	if(++gotif>1){noip[iface]++; gotif--}

	iface=$1
}

# We found an address. We put interface and address in buf array
# and decrease counter by 1

/inet /&&gotif{	
	buf[iface]=$2
	gotif--
}
# END block
END{	

	# If we still have counter set it means there is last interface
	# that doesn't have an address so we include that one too

	if(gotif>=1) noip[iface]++

	# We print out both arrays

	for(i in buf) printf("%s\t%s\n", i, buf[i])
	for(j in noip) printf("%s\tNo address\n", j)
}
Try to call it as:
Code:
ifconfig -a | awk -f thascript.awk
I leave to you to get "status" message displayed in output, just make sure you post it here in case someone else needs it as well.

All best
__________________
The best way to learn UNIX is to play with it, and the harder you play, the more you learn.
If you play hard enough, you'll break something for sure, and having to fix a badly broken system is arguably the fastest way of all to learn. -Michael Lucas, AbsoluteBSD
Reply With Quote
  #9   (View Single Post)  
Old 4th February 2011
isthistheend isthistheend is offline
New User
 
Join Date: Feb 2011
Posts: 4
Default

Thank you for your awk code s0xxx, i'll study it for sure (:
This may not be equal to your code but i want to show what i have done to get "No ip" thing (:

I add if statement into for loop like;
Code:
for X in $name 
do
    ip=$(ifconfig $X  | grep 'inet ' | awk '{print $2}')
    status=$(ifconfig $X | grep status | awk -F ":" '{print $2}')

if [ ! -e $ip ]; then
    echo "Interface Name: $X" "Ip Address: $ip" "Status: $status"
else
    echo "Interface Name: $X" "Ip Address: No ip address" "Status: $status"
 fi

done
and i got output;

Code:
Interface name: re0 IP address: 10.10.35.29 Status: active
Interface name: re1 IP address: No ip address Status: no carrier
i hope i done correct and hope to it can show a way to newbies like me (:

Best regards..
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:01 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick