home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 May / PCpro_2006_05.ISO / files / free_security / languard / languardnss7.exe / osdata.ssh < prev    next >
Encoding:
Text File  |  2005-11-09  |  14.8 KB  |  592 lines

  1. #!/bin/bash
  2. arg1=$1 #general
  3. arg2=$2 #partitions
  4. arg3=$3    #TOD
  5. arg4=$4    #users
  6. arg5=$5    #groups
  7. arg6=$6    #logged users
  8. arg7=$7    #network
  9. arg8=$8    #USB
  10. arg9=$9    #processes
  11. shift 1
  12. arg10=$9 #services
  13.  
  14. renice -15 $$ 2>&1 1>/dev/null
  15.  
  16. echo "TRUE:"
  17.  
  18. #linux detect
  19. if [ -e /etc/SuSE-release ]; then
  20.     DISTRIBUTION=`cat /etc/SuSE-release | awk '{
  21.     if (NR==1)
  22.         print $0
  23.     }'`
  24. fi
  25.  
  26. if [ -e /etc/redhat-release ]; then
  27.     DISTRIBUTION=`cat /etc/redhat-release | awk '{
  28.     if (NR==1)
  29.             print $0
  30.     }'`
  31. fi
  32.  
  33. if [ -e /etc/debian_version ]; then
  34.     DISTRIBUTION=`cat /etc/debian_version | awk '{
  35.     if (NR==1)
  36.         print $0
  37.     }'`
  38. fi
  39.  
  40.  
  41. if [ -z "$DISTRIBUTION" ]; then
  42.  
  43.     LINUXDATA=`uname -o`
  44.  
  45.     DISTRIBUTION=`awk 'BEGIN{
  46.     if (index("'"$LINUXDATA"'","FreeBSD")>0) {
  47.         print "FreeBSD"
  48.     }
  49.     }'` 
  50.  
  51. fi
  52.  
  53. if [ -z "$DISTRIBUTION" ]; then
  54.     DISTRIBUTION=`uname -o`
  55. fi
  56.  
  57.  
  58. KERNEL=`uname -r`
  59. HOST=`uname -n`
  60.  
  61. if [ $arg1 -eq 1 ]; then
  62. echo "<general>"
  63.     echo "<os>"$DISTRIBUTION"</os>"
  64.     echo "<kernel>"$KERNEL"</kernel>"
  65.     echo "<host>"$HOST"</host>"
  66. echo "</general>"
  67.  
  68. echo "%&%&"
  69.  
  70. if [ -r /proc/cpuinfo ] ;then 
  71. #read data about CPU
  72. a=`cat /proc/cpuinfo | grep processor | wc -l`
  73. echo "<processors count=\""$a"\">"
  74. i=1
  75. while [ $i -le $a ] ; do
  76. #for each processor, get vendor, model, speed and flags
  77.     echo "<processor>"
  78.     x=`cat /proc/cpuinfo | grep -m $i "processor" | tail -n 1`
  79.     cat /proc/cpuinfo | grep -A18 "$x" | while read a b c d; do
  80.         if [ "$a" = "vendor_id" ] ; then    
  81.             echo "<vendor_id>"$c"</vendor_id>"
  82.         elif ( [ "$a" = "model" ] && [ "$b" = "name" ] ) ; then 
  83.             echo "<model_name>"$d"</model_name>"
  84.         elif ( [ "$a" = "cpu" ] && [ "$b" = "MHz" ] ) ; then
  85.             echo "<cpu_speed>"$d"</cpu_speed>"
  86.         elif [ "$a" = "flags" ] ; then
  87.             prima=0
  88.         #print cpu flags separated by commas    
  89.         echo "<cpu_flags>" `for z in $c $d ; do
  90.                 if [ $prima -eq 0 ] ; then
  91.                     prima=1
  92.                     echo $z
  93.                 else
  94.                     echo ", "$z
  95.                 fi
  96.             done` "</cpu_flags>"
  97.         fi
  98.     done
  99.     echo "</processor>"
  100.     i=$[i+1];
  101. done
  102.  
  103. echo "</processors>"
  104. echo "%&%&"
  105. fi
  106. fi
  107.  
  108. if [ $arg2 -eq 1 ]; then 
  109. #get info about MOUNTED partitions; it will not print partitions that are not mounted, or remote disks
  110. echo "<partitions>"
  111. #check if we can execute df, fdisk and mount
  112. if ( [ -x /sbin/fdisk ] && [ -x /bin/df ] && [ -x /bin/mount ]) ; then
  113.     if /sbin/fdisk -l 2>&1 | grep "Cannot" >/dev/null ; then 
  114.         #this means that we can't run df; sorry
  115.         echo "<partition>"
  116.         echo "</partition>"    
  117.     else
  118.         fdisk -l | grep ^/dev | while read a b; do
  119.                 if [ `mount | grep $a | cut -d\  -f1` ]; then
  120.                     echo "<partition>"
  121.                     mount | grep $a | ( read a b c d e f ; if [ $a ]; then echo "<fs_type>"$e"</fs_type>" "<mount_point>"$c"</mount_point>"; fi )
  122.                     df $a | tail -n1 | ( read a b c d e f ; echo "<size>"`expr $b \* 1024`"</size>" "<used_space>"`expr \( $b \- $c \) \* 1024`"</used_space>" )
  123.                     echo "</partition>"
  124.                 fi
  125.         done
  126.         #the code below checks partition mounted in redhat's LVM
  127.         if [ "`lvdisplay 2>/dev/null | grep /dev`" ] ; then
  128.             echo "<partition>"
  129.             mount | grep /dev/mapper | while read a b c d e f ; do
  130.                 echo "<fs_type>"$e"</fs_type>" "<mount_point>"$c"</mount_point>"
  131.                 df $a | tail -n1 | ( read a b c ; echo "<size>"`expr $a \* 1024`"</size>" "<used_space>"`expr \( $a \- $b \) \* 1024`"</used_space>" )
  132.             done
  133.             echo "</partition>"
  134.         fi    
  135.     fi
  136. fi
  137. echo "</partitions>"
  138. echo "%&%&"
  139. fi
  140.  
  141. #time of day
  142. if [ $arg3 -eq 1 ]; then
  143.  
  144. #    UPTIME=`uptime | awk '{
  145. #    split($0,el,",")
  146. #    upt=substr(el[1],index(el[1],"up")+2)
  147. #    print upt
  148. #    }'`
  149.  
  150.     UPTIME=`cat /proc/uptime | awk '{
  151.     split($1,el,".")
  152.     print el[1]
  153.     }'`
  154.  
  155.  
  156.     DATE=`date`
  157.  
  158.  
  159.     echo "<timeofday>"
  160.     echo "<ct>"$DATE"</ct>"
  161.     echo "<uptime>"$UPTIME"</uptime>"
  162.     echo "</timeofday>"
  163. echo "%&%&"
  164. fi
  165.  
  166.  
  167. #userlist
  168. if [ $arg4 -eq 1 ]; then
  169.   if ( [ -r '/etc/passwd' ] && [ -r '/etc/shadow' ] )  ; then
  170.         #we need read access to both passwd and shadow to read all info
  171.         echo "<users>"
  172.         #to optimize, we put both files in variables
  173.         declare -a paswd=( `cat /etc/passwd | ( while read a; do echo \"$a\"; done; )` )
  174.         declare -a shadw=( `cat /etc/shadow | ( while read a; do echo \"$a\"; done; )` )
  175.  
  176.         nusers=`cat /etc/passwd | wc -l`
  177.         i=0
  178.         while [ $i -lt $nusers ] ; do
  179.                 pline=${paswd[$i]}
  180.                 sline=${shadw[$i]}
  181.                 echo "<user>"
  182.                 echo $pline\:$sline | cut -d: -f 1,6,7,9,10 --output-delimiter=" " | ( read n b c d e ;
  183.                 echo "<name>`echo $pline | cut -d: -f 5`</name>"
  184.                 echo "<homedir>$b</homedir>"
  185.                 echo "<login>$c</login>"
  186.                 echo "<username>$n</username>"
  187.                 echo "<pass_age>$e</pass_age>"
  188.  
  189.                 if [ -z "$d" ] ; then
  190.                         echo "<account_stats>2</account_stats>"
  191.                 elif [ "$d" = '*' ] || [ "$d" = '!' ] || [ "$d" = '!!' ] || [ "$c" = "/sbin/nologin" ]; then
  192.                         echo "<account_stats>1</account_stats>"
  193.                 else
  194.                         echo "<account_stats>0</account_stats>"
  195.                 fi
  196.                 ll=`lastlog -u $n 2>/dev/null | tail -c 31`
  197.                 echo "<lastlogon>$ll</lastlogon>"
  198.                 if [ $n ] && [ -z "`echo $ll | grep -i never`" ] ; then
  199.                         num_logins=`last $n | wc -l`
  200.                         num_logins=`expr $num_logins \- 2`
  201.                         if [ $num_logins -lt 0 ] ; then
  202.                                 num_logins=0
  203.                         fi
  204.                 else
  205.                         num_logins=0
  206.                 fi
  207.                 echo "<num_logins>"$num_logins"</num_logins>"
  208.                 )
  209.  
  210.                 echo "</user>"
  211.                 i=`expr $i \+ 1`
  212.         done
  213.  
  214.         echo "</users>"
  215.  
  216.     else
  217.         echo "<users></users>"
  218.     fi
  219.  
  220. echo "%&%&"
  221.  
  222.  
  223. fi
  224.  
  225.  
  226.  
  227. #grouplist
  228. if [ $arg5 -eq 1 ]; then
  229.     if ([ -r '/etc/group' ])  ; then
  230. #    echo "TRUE:"
  231.  
  232.     awk ' BEGIN {
  233.       FS=":"
  234.       substfile = "/etc/group"
  235.       i=0
  236.       while ( getline < substfile ) {
  237.             grpid[i] = $3
  238.         var1[i] = $1
  239.         grpusr[$3] = $4
  240.         i=i+1
  241.       }
  242.       close (substfile)
  243.       nrrows=i
  244.  
  245.       substfile = "/etc/passwd"
  246.       i=0
  247.       while ( getline < substfile ) {
  248.         split(grpusr[$4],U,",")
  249.         found=0
  250.         for (j in U)
  251.             if (U[j]==$1)
  252.             found=1
  253.         if (found==0)
  254.             if (length(grpusr[$4])==0)                            
  255.                 grpusr[$4] = $1
  256.             else
  257.                 grpusr[$4] = grpusr[$4] "," $1
  258.  
  259.       }
  260.       close (substfile)      
  261.  
  262.  
  263.       print "<groups>"        
  264.       for (i=0;i<nrrows;i++) {
  265.         print "<group name=\"" var1[i] "\">"       
  266. #        print var1[i]
  267.         split(grpusr[grpid[i]],U,",");          
  268.         for (j in U)
  269.             print "<member>" U[j] "</member>"            
  270.         print "</group>"
  271.       }
  272.       print "</groups>"
  273.       }'
  274.     else
  275. #    echo "FALSE:"
  276.         echo "<groups></groups>"
  277.     fi
  278. echo "%&%&"
  279. fi
  280.  
  281. #logged users
  282. if [ $arg6 -eq 1 ]; then
  283.  
  284.     if ([ -r /etc/HOSTNAME ]) ; then
  285.     HOST=`cat /etc/HOSTNAME`
  286.     else
  287.     HOST=`hostname`
  288.     fi
  289.  
  290.  
  291.     if ([ -r '/usr/bin/w' ])  ; then
  292.  
  293.     #check if FROM column is enabled by default
  294.     enabled=`w | awk ' NR==2 {
  295.         if (index($0, "FROM") > 0) {
  296.             print 1            
  297.         }        
  298.         else {
  299.             print 0
  300.         }    
  301.     }'`
  302.     
  303.     if [ $enabled == "0" ] ; then
  304.         OUTPUT="w -f"
  305.     else
  306.         OUTPUT="w"
  307.     fi
  308. #    echo "TRUE:"
  309.     echo "<logged_users>"
  310.     exec $OUTPUT | awk -v hname=$HOST '{
  311.             if (NR > 2) {
  312.             print "<loggeduser>"
  313.  
  314.                 remote="true"
  315.                 if ( hname == "$3" ) {
  316.                     remote="false"
  317.                 }
  318.                 if (index($3,":") == 1) {
  319.                     remote="false"
  320.                 }
  321.                 if (index($3,"-") == 1) {
  322.                     remote="false"
  323.                 }
  324.                 if (remote == "true") {
  325.                     print "<name>"$3"\\"$1"</name>"
  326.                     print "<remote>1</remote>"
  327.                 }
  328.                 else {
  329.                         print "<name>"$1"</name>"                
  330.                 }                
  331.                 
  332.                 print "<tty>"$2"</tty>"
  333. #                print "<from>"$3"</from>"
  334.                 print "<login>"$4"</login>"
  335.                 cm=""
  336.                 for (i=8; i<=NF; i++) {            
  337.                     cm=cm $i
  338.                 }
  339.                 print "<app>"cm"</app>"
  340.             print "</loggeduser>"
  341.         }
  342.         }'
  343.     echo "</logged_users>"
  344.  
  345.     else
  346. #    echo "FALSE:"
  347.     echo "<logged_users></logged_users>"    
  348.     fi
  349. echo "%&%&"
  350. fi    
  351.     
  352.  
  353.  
  354.  
  355. #netcard
  356.  
  357. if [ $arg7 -eq 1 ]; then
  358. echo "<interfaces>"
  359. for iffile in "/proc/net/dev" "/proc/net/wireless"; do
  360.         nrif=`cat $iffile | wc -l`
  361.         nrif=`expr $nrif - 2`
  362.  
  363.         if [ $iffile = "/proc/net/dev" ] ; then
  364.                 ifcfg="ifconfig"
  365.         else
  366.                 ifcfg="iwconfig"
  367.         fi
  368.         cat $iffile | tail -n$nrif | cut -d: -f1 | while read ifn ; do
  369.         baddr=`$ifcfg $ifn | grep "Base address" | cut -dx -f2 | head -c4`
  370.         ip=`$ifcfg $ifn | grep "inet" | cut -d: -f2 | cut -d\  -f1`
  371.         if [ $ip ] && [ $ifn != "lo" ] ; then
  372.                 echo "<interface>"
  373.                 echo "<ip>"$ip"</ip>"
  374.                 echo "<name>"$ifn"</name>"
  375.                 if [ $iffile = "/proc/net/dev" ]; then
  376.                     echo "<type>"wired"</type>"
  377.                 else
  378.                     echo "<type>"wireless"</type>"
  379.                 fi
  380.                 if [ $baddr ] ; then
  381.                         hwaddr=`lspci -v | grep $baddr -B4 | grep "controller" | cut -d\  -f1 | tail -c8`
  382.                         echo "<vendor>"`lspci -vm | grep -A6 $hwaddr | grep "^Vendor" | cut -d: -f2`"</vendor>"
  383.                         echo "<product>"`lspci -vm | grep -A6 $hwaddr | grep "^Device" | cut -d: -f2`"</product>"
  384.                         echo "<hwaddr>"`$ifcfg $ifn | grep "HWaddr" | tail -c20`"</hwaddr>"
  385.                         echo "<bcast>"`$ifcfg $ifn | grep "Bcast" | cut -d: -f3 | cut -d\  -f1`"</bcast>"
  386.                         ip6=`$ifcfg $ifn | grep "inet6" | cut -d/ -f 1 | tail -c25`
  387.                         if [ $ip6 ] ; then
  388.                                 echo "<ip6>" $ip6 "</ip6>"
  389.                         fi
  390.                         echo "<nmask>"`$ifcfg $ifn | grep "Mask" | cut -d: -f4 | cut -d\  -f1`"</nmask>"
  391.                 fi
  392.                 echo "</interface>"
  393.         fi
  394.     done
  395. done
  396. echo "</interfaces>"
  397. echo "%&%&"
  398. fi
  399.     
  400.    
  401.  
  402.  
  403.  
  404. #usb devices
  405.  
  406. #check if usb.ids is present
  407. if [ $arg8 -eq 1 ]; then
  408.  
  409. if [ -r '/proc/bus/usb/devices' ] ; then
  410.         devf='/proc/bus/usb/devices'
  411.  
  412. if [ -r '/usr/share/hwdata/usb.ids' ] ; then
  413.         idsfile='/usr/share/hwdata/usb.ids'
  414. elif [ -r '/usr/share/usb.ids' ] ; then
  415.         idsfile='/usr/share/usb.ids'
  416. fi
  417.  
  418. idstring='*'
  419. i=1
  420.  
  421. maxdevcount=`cat $devf | grep "^T:" | wc -l`
  422.  
  423. echo "<usb>"
  424. while [ $i -le $maxdevcount ] ; do
  425.         idstring=`cat $devf | grep "Vendor" -A2 -m$i | tail -n3`
  426.         i=`expr $i + 1`
  427.         vndid=`echo $idstring | cut -d= -f2 | cut -d\  -f1`
  428.         if [ "$vndid" = "0000" ] ; then
  429.                 vendor='Generic USB Device Vendor'
  430.         else
  431.                 vendor=`cat $idsfile | grep $vndid | ( read a b ; echo $b )`
  432.         fi
  433.         devid=`echo $idstring | cut -d= -f3 | cut -d\  -f1`
  434.         if [ "$devid" = "0000" ] ; then
  435.             product='Generic USB Device'
  436.         else
  437.                 product=`cat $idsfile | grep $vndid -m1 -A100 | grep $devid -m1 | ( read a b ; echo $b )`
  438.                 if [ -z "$product" ] ; then
  439.                         product='Unknown Product'
  440.                 fi
  441.         fi
  442.  
  443.         if [ "`echo $idstring | grep -i product`" ] ; then
  444.             manufacturer=`echo $idstring | cut -d= -f5 | cut -d: -f1 | sed "s/\(.*\)S/\1/"`
  445.             description=`echo $idstring | cut -d= -f6`
  446.         else
  447.             manufacturer="Generic Manufacturer"
  448.             description="Generic Device"
  449.         fi
  450.  
  451.         if [ -z "`echo $description | grep -i controller`" ] ; then
  452.                 echo "<device>"
  453.                 echo "<product>$product</product>"
  454.                 echo "<vendor>$vendor</vendor>"
  455.                 echo "<manufacturer>$manufacturer</manufacturer>"
  456.                 echo "<description>$description</description>"
  457.                 echo "</device>"
  458.         fi
  459. done
  460. echo "</usb>"
  461.  
  462. echo "%&%&"
  463.  
  464. fi
  465. fi
  466.  
  467.  
  468. #process list
  469. if [ $arg9 -eq 1 ]; then
  470.  
  471. #displays the list of all running proceeses
  472. #the current process and all processes that derive from this one
  473. #will be avoided
  474.     CPID=$$
  475.  
  476. #echo $CPID
  477.  
  478. #echo "TRUE:"
  479. echo "<processes>"
  480. FIRSTLINE=0
  481. ps ax -o "fname pid euser ppid priority args" | grep -v ps | grep -v $$ | while read a b c d e f; do
  482.     if [ $FIRSTLINE -eq 0 ] ;then 
  483.         FIRSTLINE=1
  484.     else 
  485.         if [ -e /etc/xinetd.d/$a ] ; then
  486.             x=1
  487.         else
  488.             x=0
  489.         fi
  490.         echo "<proc x=\""$x"\">"
  491.         echo "<name><![CDATA["$a"]]></name>" 
  492.         echo "<pid>"$b"</pid>" 
  493.         echo "<user>"$c"</user>" 
  494.         echo "<ppid>"$d"</ppid>"
  495.         echo "<priority>"$e"</priority>" 
  496.         echo "<cmd_line>"$f"</cmd_line>"
  497.         echo "<home_dir>"`ls -lA /proc/$b 2>/dev/null| grep cwd | cut -d ">" -f 2`"</home_dir>"
  498.         x=`ls -lA /proc/$b/task 2>/dev/null | wc -l`
  499.         x=$[x-1]
  500.         echo "<thread_count>"$x"</thread_count>"
  501.         echo "</proc>"
  502.         echo
  503.     fi
  504. done
  505. echo "</processes>"
  506.  
  507. #echo $$
  508.  
  509. #echo "!!SCRIPT_FINISHED!!"
  510.  
  511. echo "%&%&"
  512.  
  513. fi
  514.  
  515.  
  516. #services
  517. if [ $arg10 -eq 1 ]; then
  518.  
  519.     #check if we have enough rights to determine the startup settings
  520. if ( [ -x '/sbin/runlevel' ] && [ -x '/sbin/chkconfig' ] ) ; then
  521.  
  522.     #check which services are enabled
  523.     CRUNLEVEL=`runlevel | (read a a ;echo $a)`
  524.  
  525.     NRUNLEVEL=`cat /etc/inittab | grep ":initdefault:" | cut -d ":" -f 2`
  526.  
  527.  
  528.     
  529.     if [ -e "/etc/init.d" ] 
  530.     then
  531.     DAEMONDIR="/etc/init.d/"
  532.     else
  533.     if [ -e "/etc/rc.d/init.d" ] 
  534.     then
  535.             DAEMONDIR="/etc/rc.d/init.d/"
  536.     else
  537.             DAEMONDIR="/etc/rc.d/"
  538.         fi
  539.     fi
  540.       
  541.  
  542.  
  543.   
  544. echo "<services runlevel=\"$CRUNLEVEL\" nextrunlevel=\"$NRUNLEVEL\">"
  545.  
  546.  
  547. chkconfig --list | grep ":on" | while read a b c d e f g h ; do
  548.     echo "<service>"
  549.     echo "<name><![CDATA["$a"]]></name>"
  550.     echo "<description><![CDATA["`cat $DAEMONDIR$a | grep -m1 -i "description" | (read a b c; echo $c)`"]]></description>"
  551.     if [ $a = network ] ;then 
  552.         echo "<status>4</status>"
  553.     else
  554.         stats=`$DAEMONDIR$a status 2>&1 | tail -n 1 | awk '{print $NF}'`
  555.         if [ `echo $stats | grep -i "stopped"` ] || [ `echo $stats | grep -i "unused"` ]; then
  556.             echo "<status>1</status>"
  557.         elif [ `echo $stats | grep -i "running"` ] || [ `echo $stats | grep -i "started"` ] ; then
  558.             echo "<status>4</status>"
  559.         else
  560.             echo "<status></status>"
  561.         fi
  562.     fi
  563.     zz=''
  564.     for x in "$b" "$c" "$d" "$e" "$f" "$g" "$h" ; do
  565.         if [ `echo $x | grep 'on'` ] ; then
  566.             if [ "$zz" ] ; then
  567.                 zz=$zz\,\ `echo $x | cut -d: -f1`
  568.             else
  569.                 zz=`echo $x | cut -d: -f1`
  570.             fi
  571.         fi
  572.     done
  573.     echo "<runlevels>$zz</runlevels>"
  574.     echo "</service>"
  575. done
  576.  
  577.  
  578. echo "</services>"
  579.  
  580. else
  581.     echo "<services></services>"
  582. fi
  583. echo "%&%&"
  584. fi
  585.  
  586. if [ `ls -lA | wc -l` -eq 1 ] ; then
  587.     rm -rf ~/.gfi-temp
  588. else
  589.     rm -f $0 2>&1 1>/dev/null
  590. fi
  591.  
  592. echo "!!SCRIPT_FINISHED!!"