#!/bin/sh

export LANG=C

MYVAR="MAC 2 at 2015.07.15"

echo "IISCC network report version ${MYVAR}"
echo "generating report, please wait few minutes..."


MYDATE=$(/bin/date +%Y-%m%d-%H%M-%S)
MYWHO=$(/usr/bin/whoami)
MYDIR="/Users/${MYWHO}/Desktop"
MYFILE="${MYDIR}/iis-net-report_${MYDATE}.txt"

ECHO="/bin/echo"
SW="/usr/bin/sw_vers"
SYSP="/usr/sbin/system_profiler"
IPCFG="/usr/sbin/ipconfig"
IFCFG="/sbin/ifconfig"
AIRP="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
GREP="/usr/bin/grep"
AWK="/usr/bin/awk"
HOSTNAME="/bin/hostname"
NETSTAT="/usr/sbin/netstat"
TRACERT="/usr/sbin/traceroute"
TRACERT6="/usr/sbin/traceroute6"
SEQ="/usr/bin/seq"
EXPR="/bin/expr"
CAT="/bin/cat"

TRACETGT4="8.8.8.8"
TRACETGT6="2001:4860:4860::8888"
RESOLV="/etc/resolv.conf"

TESTNUM=7
TESTNOW=0


${ECHO} "---- IISCC net report ver ${MYVAR} ----" >> ${MYFILE}

${ECHO} ""
${ECHO} "---- IISCC wlan report start ----" >> ${MYFILE}
${SYSP} SPAirPortDataType >> ${MYFILE}
${AIRP} -I >> ${MYFILE}
${AIRP} -s >> ${MYFILE}
${ECHO} "---- IISCC wlan report end ----" >> ${MYFILE}
TESTNOW=$(${EXPR} ${TESTNOW} + 1)
${ECHO} "(${TESTNOW}/${TESTNUM}) wlan done"



${ECHO} "---- IISCC route report start ----" >> ${MYFILE}
${NETSTAT} -rn >> ${MYFILE}
${ECHO} "---- IISCC route report end ----" >> ${MYFILE}
TESTNOW=$(${EXPR} ${TESTNOW} + 1)
${ECHO} "(${TESTNOW}/${TESTNUM}) route done"

${ECHO} "---- IISCC address report start ----" >> ${MYFILE}
${IFCFG} >> ${MYFILE}
for i in $( ${IFCFG} | ${GREP} ^en | ${AWK} -F\: '{print $1}');
do
 echo "---- ipconfig  $i ----" >> ${MYFILE}
 ${IPCFG} getpacket ${i} >> ${MYFILE} 
 echo "----------------------" >> ${MYFILE}
done
${ECHO} "---- IISCC address report end ----" >> ${MYFILE}
TESTNOW=$(${EXPR} ${TESTNOW} + 1)
${ECHO} "(${TESTNOW}/${TESTNUM}) address done"

${ECHO} "---- IISCC dns report start ----" >> ${MYFILE}
${CAT} ${RESOLV} >> ${MYFILE}
${ECHO} "---- IISCC dns report end ----" >> ${MYFILE}
TESTNOW=$(${EXPR} ${TESTNOW} + 1)
${ECHO} "(${TESTNOW}/${TESTNUM}) dns done"

${ECHO} "---- IISCC gateway report start ----" >> ${MYFILE}
for i in $(${SEQ} 1 10);
do 
${TRACERT} -m 1 -n ${TRACETGT4} >> ${MYFILE} 2>&1
done
for i in $(${SEQ} 1 10);
do
${TRACERT6} -m 1 -n ${TRACETGT6} >> ${MYFILE} 2>&1
done
${ECHO} "---- IISCC gateway report end ----" >> ${MYFILE}
TESTNOW=$(${EXPR} ${TESTNOW} + 1)
${ECHO} "(${TESTNOW}/${TESTNUM}) gateway done"

${ECHO} "---- IISCC tracert report start ----" >> ${MYFILE}
${TRACERT} -m 15 -n ${TRACETGT4} >> ${MYFILE} 2>&1
${TRACERT6} -m 15 -n ${TRACETGT6} >> ${MYFILE} 2>&1
${ECHO} "---- IISCC tracert report end ----" >> ${MYFILE}
TESTNOW=$(${EXPR} ${TESTNOW} + 1)
${ECHO} "(${TESTNOW}/${TESTNUM}) tracert done"

${ECHO} "---- IISCC system report start ----" >> ${MYFILE}
${SYSP} SPHardwareDataType >> ${MYFILE}
${SYSP} SPNetworkDataType >> ${MYFILE}
${SYSP} SPEthernetDataType >> ${MYFILE}
${SYSP} SPNetworkLocationDataType >> ${MYFILE}
${HOSTNAME} >> ${MYFILE}
${SW} >> ${MYFILE}
${ECHO} "---- IISCC system report end ----" >> ${MYFILE}
TESTNOW=$(${EXPR} ${TESTNOW} + 1)
${ECHO} "(${TESTNOW}/${TESTNUM}) system done"


${ECHO} ""
${ECHO} "finished"






