-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtellIP
More file actions
154 lines (139 loc) · 4.91 KB
/
tellIP
File metadata and controls
154 lines (139 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/sh -u
# Script_name : tellip
# Author : Bert Lindeman
# Description : Report active IP-addresses by speach
# This script is meant to be run in the boot sequence
# Version : v1.2
# Date : 2014-08-11
# Usage : tellIP
# Tell (speak) what IP address(es) this brick has.
# So you can hear the IP address
# in stead of pinging to find it.
# Notes : Tested upon ev3dev running jessie
# Because this script is meant to run as init script,
# using /bin/sh and not bash builtin functions
#
# Copyright : 2014 Bert Lindeman
# License : GPL-3.0+
#
#============================================================================
#
# Changelog
# Date By Description
# 20140828 BL o It takes more time to complete wifi setup,
# So do a few attempts and wait before giving up.
# 20140916 BL o added some character at the start of the string
# To start speaking a bit later and get past the clicks
# at the start of the text.
#============================================================================
#============================================================================
# function: report ip address for THIS interface
# parameter(s): $1 - interface-id like usb0 or wlan0
#
#============================================================================
#
reportIF () {
#
IP="" # start empty
loopcount=0 # counter at zero
#
# Get IP-address
# do while $IP is empty, but max 15 times..
#
while [ ".$IP" = "." ]
do
loopcount=$((loopcount+1))
extractIP $1
if [ $loopcount -ge 15 ] ; then
break;
elif [ "$IP" = "" ]; then
waittext="\"waiting 30 seconds\""
echo "Got no IP-address yet for $ifname, attempt# $loopcount, $waittext"
espeak "$waittext" --stdout | aplay -q
sleep 30
fi
done
#
echo "$0: Report for interface: \"$1\": $IP"
#
# the interface name is pronounced more clear if UPCASED and split by periods
# The same goes for the IP-address
#
IFNAME=`echo $ifname | tr '[:lower:]' '[:upper:]' | sed 's/./&./g'`
# echo "about to report $IP for $IFNAME"
splitText " $IFNAME $IP"
# echo "about to speaktext: \" \"interface.\" \"$splitText\""
speakText "( . . \"Interface.\" \"$splitText\""
}
#============================================================================
# function: Determine IP address for THIS interface
# parameter(s): $1 - interface-id like usb0 or wlan0
#
#============================================================================
#
extractIP () {
#
ifcmd="/sbin/ifconfig"
cutcmd="cut -d ":" -f2"
IP=`echo \`$ifcmd $1 | grep 'inet addr:'|$cutcmd|awk '{ print$1}'\``
#
}
#============================================================================
# function: split IP-address on periods to gain groups of max 3 digits
# parameter(s): $1 - IP-address in nn.nn.nn.nn format
# result: variable splitText in format "n n n . n n . n n n . n "
#============================================================================
#
splitText () {
splitword=`echo $* | sed 's/[^ ]*/&. /g'`
splitText=`echo $splitword | sed 's/./& /g'`
}
#============================================================================
# function: Let espeak report the interface and IP-address
# parameters used:
# ifname - name of the interface
# ipaddress - formatted as loose digits and periods
#============================================================================
speakText () {
#
#
# Text is: "interface" interface-name [followed by the IP-address]
speaktxt="$*"
#
# get current language
#
lang=`expr substr "$LANG" 1 2`
if [ ${#lang} -ne 2 ] ; then
lang="en" # use default if not known ;
fi
# TODO potential improvement: check that *this* language
# indeed *is* supported by espeak??
# use a slow pace to improve clarity:
# -s limits 80 to 450,
# default is 175
# 175 is too fast; using 100
#
# -a Amplitude, 0 to 200, default is 100
# (similar to volume of the sound)
eOPT="-a 40 -s 100 --stdout -punct=\".\""
basenm=`basename "$0"`
# echo "about to: \"espeak -v $lang $eOPT \"$speaktxt\" | aplay -q\""
espeak -v $lang $eOPT "$speaktxt" | aplay -q
}
#============================================================================
#
## main program:
#
# function: Loop through interfaces in /proc/net/dev
# parameters used: none
# Interface "lo" is ignored
#============================================================================
#
for i in `cat /proc/net/dev | grep ':' | cut -d ':' -f 1`
do
ifname=`echo $i | tr -d ' ' `
if [ "$ifname" != "lo" ] ; then
reportIF "$ifname"
fi
done
#