LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2002, 07:10 AM   #1
antken
Member
 
Registered: Nov 2000
Posts: 368

Rep: Reputation: Disabled
getting network configuration


hi,

i have a script on my linux box that i have written
to run it you basically type ethinfo eth0

and it returns basic information about the adapters configuration ( ip, subnet, and adapter type ie ethernet or local )
this script is for the benifit of my boss who can't seem to get to grips with ifconfigs out put

how would i go about getting my script to check if the interface exists first then printing its output? ( code below)

thanks
antken



here is the script:
Code:
#!/bin/sh


e_ip=`/sbin/ifconfig $1 | grep inet | cut -f 2 -d : | cut -d ' ' -f 1`
e_snet=`/sbin/ifconfig $1 | grep Mas | cut -d : -f 4`
e_typ=`/sbin/ifconfig $1 | grep encap | cut -f 2 -d : | cut -d ' ' -f 1`

echo
echo IP Configuration for interface $1 is:
echo
echo This interface is: $e_typ
echo IP address: $e_ip
echo Subnet: $e_snet
echo

exit 0

Last edited by antken; 10-18-2002 at 07:11 AM.
 
Old 10-18-2002, 12:55 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
For instance looking for every interface in class eth:

#!/bin/sh
for interface in $(cat /proc/net/dev | grep "eth" | cut -d ":" -f 1 | tr -d " "); do
# replace your scripts "$1" with "$interface":
e_ip=`/sbin/ifconfig $interface | grep inet | cut -f 2 -d : | cut -d ' ' -f 1`; (etc etc...)
done
exit 0

Or you could do
case "$1" in
eth|tap)
for interface in $(cat /proc/net/dev | grep "$1" | cut -d ":" -f 1 | tr -d " "); do
# replace your scripts "$1" with "$interface":
e_ip=`/sbin/ifconfig $interface | grep inet | cut -f 2 -d : | cut -d ' ' -f 1`; (etc etc...)
done
;;
*)
echo "$(basename $0): first arg needs to be one of [eth|tap]"
exit 1
esac


...or you scrape the interface names from /etc/sysconfig/network-scripts/ifcfg* if you don't like /proc
 
Old 10-20-2002, 03:09 PM   #3
leed_25
Member
 
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102

Rep: Reputation: 17
Re: getting network configuration

Quote:
Originally posted by antken
hi,

. . .

e_ip=`/sbin/ifconfig $1 | grep inet | cut -f 2 -d : | cut -d ' ' -f 1`
e_snet=`/sbin/ifconfig $1 | grep Mas | cut -d : -f 4`
e_typ=`/sbin/ifconfig $1 | grep encap | cut -f 2 -d : | cut -d ' ' -f 1`

echo
echo IP Configuration for interface $1 is:
echo
echo This interface is: $e_typ
echo IP address: $e_ip
echo Subnet: $e_snet
echo

exit 0
[/code]




if i understand your question correctly, maybe something like this would do:

Code:
if [ ! -z $e_typ ]; then
    echo This interface is: $e_typ
    echo IP address: $e_ip
    echo Subnet: $e_snet
fi
you might want to do 'man test' for lots of info on the [ ... ] shell command.


Last edited by leed_25; 10-20-2002 at 03:16 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Slackware 10 network configuration. Problem with 8139 network card drivers ! Padmakiran Linux - Networking 8 03-27-2007 06:48 AM
Network Configuration Hanging in FC4 After Using Wireless Network Wizard Trip in VA Linux - Wireless Networking 2 07-18-2005 08:32 AM
network configuration ilectrcbob Fedora 2 03-11-2005 01:43 AM
9.2 + Network configuration kmashraf Mandriva 1 11-22-2004 03:40 AM
about configuration over the whole network Rex_chaos Linux - Networking 1 01-06-2004 06:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration