我试图在我的脚本中添加一条awk
语句或:
if; else; then if eth0:4 is a match then put in eth0:4 IP
如果您将多个 IP 分配给一个 NIC,您将如何在脚本中将 IP 作为变量获取?
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
答案1
var=$(awk '/eth0:4/ {print $2}' file)"
答案2
我也尝试过,我遇到的情况是我有7个应用程序在此服务器上运行,每个应用程序使用不同的IP,所以我要做的是如果应用程序是app4然后做
/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
如果是 app4,则给我 eth0:4 的 IP,但此脚本应该适用于每个应用程序。
这是我的脚本的示例:
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
该脚本以这种方式工作,但我尝试使用 IP 而不是 ifconfig。另外我想消除所有额外的回声和不必要的代码。