![]() |
|
|||||||
| Programming C, bash, Python, Perl, PHP, Java, you name it. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|||
|
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.. |
|
|||
|
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:
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 08:52 AM. |
|
|||
|
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
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 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 |
|
|||
|
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
Code:
Interface name: bge0 IP address: 10.10.35.12 Status: active Interface name: bge1 IP address: Status: no carrier Interface name: bge1 IP address:"No Ip Address" Status: no carrier i think "if" will solve the stuation (: Thank you both again.. |
|
|||
|
Thank you all for your replies..
|
|
||||
|
@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)
}
Code:
ifconfig -a | awk -f thascript.awk ![]() 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 |
|
|||
|
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
Code:
Interface name: re0 IP address: 10.10.35.29 Status: active Interface name: re1 IP address: No ip address Status: no carrier Best regards.. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|