Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I am trying to add in my script an awk statement or a:

if; else; then if eth0:4 is a match then put in eth0:4 IP

How would you get the IP in your script as a variable if you have multiple IP's assgined to one NIC?

inet 133.16.8.9/16 brd 133.8.255.255 scope global eth0
inet 133.8.5.8/16 brd 133.8.255.255 scope global secondary eth0:1
inet 133.8.5.7/16 brd 133.8.255.255 scope global secondary eth0:2
inet 133.8.5.6/16 brd 133.8.255.255 scope global secondary eth0:3
inet 133.8.5/16 brd 133.8.255.255 scope global secondary eth0:4
inet 133.8.5.4/16 brd 133.8.255.255 scope global secondary eth0:5
inet 133.8.5.3/16 brd 133.8.255.255 scope global secondary eth0:6
inet 133.8.5.2/16 brd 133.8.255.255 scope global secondary eth0:7
share|improve this question

2 Answers 2

I tried that as well, the situation I am having is I have 7 applications running on this server and each app uses a different IP, so what I am doing is if the app is app4 then do

/sbin/ip addr ls eth0 | awk '/inet / {print $2, $8}'
133.8.5.9/16 eth0
133.8.5.8/16 eth0:1
133.8.5.7/16 eth0:2
133.8.5.6/16 eth0:3
133.8.5.5/16 eth0:4
133.8.5.4/16 eth0:5
133.8.5.3/16 eth0:6
133.8.5.2/16 eth0:7

if it is app4 then give me the IP to eth0:4, but this script should work for every app.

here is an example of my script:

JBOSS=$1
JBOSS_ENV=`echo -e ${JBOSS} | awk '{print substr($0,7)}'`
JBOSS_ETH=`echo -e ${JBOSS} | awk '{print substr($0,10)}'`
JBOSS_SVR=`uname -n`
JBOSS_IP=`/sbin/ifconfig eth0:${JBOSS_ETH} | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p'`
JBOSS_PID=`ps aux | grep -v grep | grep -v bash | grep $1 | awk '{print $2}'`
JBOSS_TMP='/home/tomcat/scripts/'${JBOSS_ENV}'.jboss_thread_dump.html'
SCRIPT_PATH=`dirname "$0"`; SCRIPT_PATH=`eval "cd \"$SCRIPT_PATH\" && pwd"`

if [[ -z ${JBOSS_PID} ]]; then

echo "Subject: ${JBOSS_ENV} JBOSS Thread Dump from ${JBOSS_SVR}" >> $JBOSS_TMP
echo -e "\nContent-Type: text/html; charset="us-ascii"" >> $JBOSS_TMP
echo -e "\nJBOSS Script Path: ${SCRIPT_PATH}\n\nJBOSS Environment: ${JBOSS_ENV}\n" >> $JBOSS_TMP

echo "<html><body>" >> $JBOSS_TMP

/usr/local/${JBOSS}/jboss-soa-p.5.0.0/jboss-as/bin/twiddle.sh --server=${JBOSS_IP} --user=***** --password=***** invoke jboss.system:type=ServerInfo listThreadDump >> $JBOSS_TMP

echo "</body></html>" >> $JBOSS_TMP

   cat $JBOSS_TMP | grep -i "FOUND DEADLOCK" > /dev/null; status=$?
if [ $status = 0 ]; then
   cat $JBOSS_TMP | mail -s "${JBOSS} ${JBOSS_SVR} DEADLOCK FOUND" MYEMAILADDRESS
fi

   cat $JBOSS_TMP | grep "ERROR" > /dev/null; status=$?
if [ $status = 0 ]; then
   cat $JBOSS_TMP | mail -s "${JBOSS} ${JBOSS_SVR} ERROR FOUND" MYEMAILADDRESS fi

/usr/sbin/sendmail MYEMAILADDRESS < $JBOSS_TMP

rm $JBOSS_TMP

exit

fi

The script works this way, but I am trying to use IP instead of ifconfig. Also I want to elimnate all the extra echo's and unnecessary code.

share|improve this answer
var=$(awk '/eth0:4/ {print $2}' file)"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.