Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script suggestion 2

Status
Not open for further replies.

t3chhelp

Technical User
Apr 19, 2002
203
CA
Hello, spent a couple days trying to find a solution to what seems like a simple task. Basically I'm would like to print my current ip address but minus 1. For example, if my ip address was 192.168.0.5, then print 192.168.0.4. I've tried a couple combinations but always getting some errors. Any guru's can suggest a solution to this? Thanks

ADDRESS="'ifconfig |grep 'addr:' |cut -f2 -d ":"'"
let "NEWADDRESS=$ADDRESS-1"
printf "$ADDRESS"

too many errors and don't know what I've done wrong.
 
I'd use awk.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Code:
ADDRESS="'ifconfig |grep 'addr:' |cut -f2 -d ":"'"
a) What is the " supposed to do here?
b) Instead of ' (apostroph), I guess you might wanted to use ` (backtick), which is not a good idea, because it might easily get confused with apostroph. Instead use $(...)
Code:
ADDRESS=$(ifconfig |grep 'addr:' |cut -f2 -d ":")
c) In your locale, 'addr:' might work. In mine not - I would need to use 'Adresse:'
Code:
ifconfig |grep 'Adresse:'
leads to 4 addresses:
inet Adresse:192.168.32.32 Bcast:192.168.32.255 Maske:255.255.255.0
inet6-Adresse: fe80::20c:f1ff:fe59:da1/64 Gültigkeitsbereich:Verbindung
inet Adresse:127.0.0.1 Maske:255.0.0.0
inet6-Adresse: ::1/128 Gültigkeitsbereich:Maschine
d) Specifying the interface reduces this
Code:
ifconfig eth0 | grep 'addr:'
e) just the IPv4-adress:
Code:
ifconfig eth0 | grep 'inet addr:'
Code:
ADDRESS=$(ifconfig eth0 | grep 'inet addr:' | cut -f2 -d ":")
echo $ADDRESS 
192.168.32.32 Bcast
let's use sed instead:
Code:
 ADDRESS=$(ifconfig eth1 | grep 'inet addr:' | sed 's/[^:]*://;s/ .*//g' )
echo $ADDRESS 
192.168.32.32
Of course that's not a number we can perform arithmetik on.

You have to extract the last part of the number, substract one from that, and append that to rest of the adress.

echo ${ADDRESS//\./ }
192 168 32 32
Code:
#!/bin/bash
#
function ipMinusOne { 
	v=$4
	echo $1.$2.$3.$((v-1)) 
}
ADDRESS=$(ifconfig eth1 | grep 'inet addr:' | sed 's/[^:]*://;s/ .*//g' )
# echo $ADDRESS
ipMinusOne ${ADDRESS//\./ }
192.168.32.31
voila!


don't visit my homepage:
 
print my current ip address but minus 1
Code:
traceroute -n $(hostname) 2>/dev/null | awk '{ip=$2}END{split(ip,a,/\./);print a[1]"."a[2]"."a[3]"."(--a[4])}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Both worked great. You guys not only provided answers but also explained the process. Thank-you.
 
Both worked great. You guys not only provided answers but also explained the process. Thank-you.

Then surely both are worthy of a little purple star in appreciation - see below left in the posts in question.

Some days are diamonds, some days are rocks - make sure most are the former.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top