(OLD) (ARCHIVED) Puppy Linux Discussion Forum Forum Index (OLD) (ARCHIVED) Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info

This forum can also be accessed as http://oldforum.puppylinux.com
It is now read-only and serves only as archives.

Please register over the NEW forum
https://forum.puppylinux.com
and continue your work there. Thank you.

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups    
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 26 Feb 2021, 23:55
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
shell script to get ip address/iface
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. View previous topic :: View next topic
Page 1 of 2 [16 Posts]   Goto page: 1, 2 Next
Author Message
01micko


Joined: 11 Oct 2008
Posts: 8787
Location: qld

PostPosted: Tue 12 Jan 2016, 02:56    Post subject:  shell script to get ip address/iface  

This is a simple script to return ip, iface or defaults to both (pass it 'ip' or 'iface' to get either one).

Code:
#!/bin/sh

func_ip_iface() {
   case "$1" in
      ip|iface|'')
      busybox ifconfig | \
      while read -r inet ip bcast x;do
         case $inet in
            [a-z]*[a-z][0-9]*)IFACE=$inet;; # eth0,eth0:0,ppp0,wlan0,usb0 etc
         esac
         case ${bcast%:*} in
            [BP][c\-][at][s\-][tP]) #strict match for 'Bcast' or 'P-t-P'
            case $1 in
               ip)echo ${ip#*:};;
               iface)echo $IFACE;;
               *)echo $IFACE ${ip#*:};;
            esac
            ;;
         esac
      done
      ;;
      *)echo "usage: ${0##*/} [ip|iface]"
      ;;
   esac
}

func_ip_iface $1



(just be careful copy/pasting as forum appends a space to each line.. line 4 is critical in that regard)

It seems to work ok for eth0, ppp0 and even if I add eth0:0 as an alias to eth0 (reports both if connected).

Anyone want to pick it to pieces for possible bugs? (regex maybe?) It works exactly the same with #!/bin/ash and perhaps even slightly slower with ash.

Note that it would need adjustment for FULL ifconfig, that's why I specifically call busybox.

_________________
Puppy Linux Blog - contact me for access

Last edited by 01micko on Tue 12 Jan 2016, 07:12; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website 
greengeek


Joined: 20 Jul 2010
Posts: 5834
Location: Republic of Novo Zelande

PostPosted: Tue 12 Jan 2016, 05:18    Post subject:  

Seems to work here (Slacko 5.6 derivative)
Code:
# ./getip
wlan0 192.168.1.5
#
# ifconfig
lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:288 (288.0 B)  TX bytes:288 (288.0 B)

wlan0     Link encap:Ethernet  HWaddr 00:1D:E0:4F:EB:3F 
          inet addr:192.168.1.5  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3377 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2341 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4168509 (3.9 MiB)  TX bytes:321572 (314.0 KiB)

#
Back to top
View user's profile Send private message 
L18L


Joined: 19 Jun 2010
Posts: 3493
Location: www.eussenheim.de/

PostPosted: Tue 12 Jan 2016, 05:53    Post subject:  

Code:
      case ${bcast%:*} in
       #  [BP][c\-][at][s\-][tP]) #strict match for 'Bcast' or 'P-t-P'
         Bcast|P-t-P)
works here too.
Back to top
View user's profile Send private message 
01micko


Joined: 11 Oct 2008
Posts: 8787
Location: qld

PostPosted: Tue 12 Jan 2016, 06:09    Post subject:  

L18L wrote:
Code:
      case ${bcast%:*} in
       #  [BP][c\-][at][s\-][tP]) #strict match for 'Bcast' or 'P-t-P'
         Bcast|P-t-P)
works here too.


That's a hangover from the original bashism version:

Code:
#!/bin/bash

printf "$(busybox ifconfig)" | \
while read -r inet ip bcast ;do
   #[[ "$inet" =~ ^([a-z]*[a-z][0-9]) ]] && IFACE="${BASH_REMATCH[1]}"
   [[ "$inet" =~ ^([a-z]*[a-z][0-9]) ]] && IFACE=$inet
   [[ "${bcast%:*}" =~ ([BP][c\-]*[a-z]) ]] && echo $IFACE ${ip#*:}
done


I know 'expr' can match regex in ash but seemed a bit overkill when case works and is probably faster. Besides.. is there any more than 'Bcast' and 'P-t-P'? I suppose I should search busybox sources. (And ifconfig too).

_________________
Puppy Linux Blog - contact me for access
Back to top
View user's profile Send private message Visit poster's website 
L18L


Joined: 19 Jun 2010
Posts: 3493
Location: www.eussenheim.de/

PostPosted: Tue 12 Jan 2016, 07:03    Post subject:  

Code:
#printf "$(busybox ifconfig)" | \
busybox ifconfig | \
works here too in ash and dash
Back to top
View user's profile Send private message 
01micko


Joined: 11 Oct 2008
Posts: 8787
Location: qld

PostPosted: Tue 12 Jan 2016, 07:11    Post subject:  

L18L wrote:
Code:
#printf "$(busybox ifconfig)" | \
busybox ifconfig | \
works here too in ash and dash


Yep Laughing

Updated 1st post and added usage case.

EDIT: and changed shebang to #!/bin/sh

_________________
Puppy Linux Blog - contact me for access
Back to top
View user's profile Send private message Visit poster's website 
step

Joined: 04 May 2012
Posts: 1352

PostPosted: Tue 12 Jan 2016, 08:24    Post subject:  

Maybe return 1 on illegal syntax
Code:

*)echo "usage: ${0##*/} [ip|iface]" ; return 1

_________________
Fatdog64-810|+Packages|Kodi|gtkmenuplus
Back to top
View user's profile Send private message 
Flash
Official Dog Handler


Joined: 04 May 2005
Posts: 13653
Location: Arizona USA

PostPosted: Tue 12 Jan 2016, 11:44    Post subject: Re: shell script to get ip address/iface  

01micko wrote:
(just be careful copy/pasting as forum appends a space to each line.)

Hm. Well, you can get around that by clicking the "quote" button in the post and copying from the compose window.
Back to top
View user's profile Send private message 
Karl Godt


Joined: 20 Jun 2010
Posts: 4208
Location: Kiel,Germany

PostPosted: Fri 15 Jan 2016, 15:46    Post subject:  

Geany editor has also options in the preferences under "Files" :
Strip trailing spaces and Tabs ..

I would advise to check that, to make commits for patches on github less cumbersome !

_________________
«Give me GUI or Death» -- I give you [[Xx]term[inal]] [[Cc]on[s][ole]] .
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal Razz
Back to top
View user's profile Send private message 
Karl Godt


Joined: 20 Jun 2010
Posts: 4208
Location: Kiel,Germany

PostPosted: Fri 15 Jan 2016, 15:49    Post subject:  

works without parameter:
Code:
sh-3.00# ./_get_ip.sh
eth103 192.168.8.100


Does not give correct error message with [wrong] parameter:
Code:
sh-3.00# ./_get_ip.sh eth1
usage: _get_ip.sh [ip|iface]

_________________
«Give me GUI or Death» -- I give you [[Xx]term[inal]] [[Cc]on[s][ole]] .
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal Razz
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5524
Location: Ontario

PostPosted: Sat 16 Jan 2016, 13:10    Post subject: Re: shell script to get ip address/iface  

Cool
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4878
Location: Blue Springs, MO

PostPosted: Mon 25 Jan 2016, 04:30    Post subject:  

TheProcManPage wrote:
/proc/net/arp
This holds an ASCII readable dump of the kernel ARP table used
for address resolutions. It will show both dynamically
learned and preprogrammed ARP entries. The format is:

Code:
        IP address     HW type   Flags     HW address          Mask   Device
        192.168.0.50   0x1       0x2       00:50:BF:25:68:F3   *      eth0
        192.168.0.250  0x1       0xc       00:00:00:00:00:00   *      eth0


it follows that you can get the IP address for an interface with no external dependencies using something like:

Code:
get_iface_ip(){
  DefaultIface=eth0 #you may prefer wlan0 as default here?
  while read IP HW Flags Addr Mask Device; do
    case "$Device" in
      ${1:-$DefaultIface})echo "$IP";return;;
    esac
  done < /proc/net/arp
  echo no ip address for ${1:-$DefaultIface} >/dev/stderr
}

_________________
Check out my github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
gcmartin

Joined: 14 Oct 2005
Posts: 6730
Location: Earth

PostPosted: Mon 25 Jan 2016, 17:27    Post subject:  

Testing results for both @01Micko and @Technosaurus LAN adapter reports: Note the console messages
Code:
# echo "Establish baseline report"
Establish baseline report
# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:a0:b8:d7:1e:31 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.7/25 brd 192.168.0.255 scope global eth0
       valid_lft forever preferred_lft forever
3: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc mq qlen 1000
    link/ether 00:b0:a3:f2:7d:9c brd ff:ff:ff:ff:ff:ff
#
# echo "Save @01Micko's eth0 reporter as whatsIP.sh and set as executable"
Save @01Micko's eth0 reporter as whatsIP.sh and set as executable
# whatsIP.sh
eth0 192.168.0.7
# echo "Works in urxvt"
Works in urxvt
#                                                                             
# echo "Messages for other specific interfaces"
Messages for other specific interfaces
# whatsIP.sh lo   
usage: whatsIP.sh [ip|iface]
# whatsIP.sh wlan0   
usage: whatsIP.sh [ip|iface]
# whatsIP.sh any
usage: whatsIP.sh [ip|iface]
#                                                                             
# echo "Save @technosaurus's LAN adapter reporter as whatsIP.sh and set as executable"
Save @technosaurus's LAN adapter reporter as whatsIP2.sh and set as executable
# whatsIP2.sh
# whatsIP2.sh eth0
# whatsIP2.sh wlan0
# whatsIP2.sh lo   
# whatsIP2.sh any
# echo "No reports received on console"
No report received on console
# echo "AND when I do a cat of my arp table ''/proc/net/arp'' it does NOT contain my local PC eth0 address"
AND when I do a cat of my arp table '/proc/net/arp' it does NOT contain my local PC eth0 address
# echo "This is NORMAL for expectations in the ARP table"
This is NORMAL for expectations in the ARP table
#
Questions
    What happens should user have more than 1 ethernet connection on MB?\ Should scripts allow a specific request?
Hope this is helpful info.

_________________
Get ACTIVE Create Circles; Do those good things which benefit people's needs!
We are all related ... Its time to show that we know this!
3 Different Puppy Search Engines or use DogPile
Back to top
View user's profile Send private message 
L18L


Joined: 19 Jun 2010
Posts: 3493
Location: www.eussenheim.de/

PostPosted: Tue 26 Jan 2016, 06:48    Post subject:  

gcmartin wrote:
What happens should user have more than 1 ethernet connection on MB?

"User" should learn to act as "administrator"
Code:
cat /proc/net/arp

or
Code:
ifconfig
Back to top
View user's profile Send private message 
jafadmin

Joined: 19 Mar 2009
Posts: 1258

PostPosted: Tue 22 Mar 2016, 23:48    Post subject:  

Here's kinda what I do:

Code:

#!/bin/sh
myAdapter=$(ifconfig | grep -A 1 'Ethernet')
myDevice=$(echo $myAdapter | cut -d' ' -f1)
myIP=$(echo $myAdapter | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f8 | awk '{ print $1}')
echo $myDevice" - "$myIP
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 2 [16 Posts]   Goto page: 1, 2 Next
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1010s ][ Queries: 11 (0.0154s) ][ GZIP on ]