Get inventory in the lan

This script needs whoe network pcs having linux os, I have used sshpass for understanding you can use ssh keys also. This is written for 3  seperate networks in lan ( 192.168.10.0/24 192.168.100.0/24 and 192.168.20.0/24)

The script will provide the username ip address mac address memory cpu info (* note my user having . in username so i wrote accordingly. you may have different and so you have to edit the script according to you.




#!/bin/bash
 downhosts=/var/log/hostsdetails/down
 uphosts=/var/log/hostsdetails/up
 inventory=/var/log/hostsdetails/inventory
 old=/var/log/hostsdetails/old/
 pass=PASSWORD
 net=(10 100 20)
 hour=0
 start_time=$(date +%s)

mv "$downhosts" "$old"
 mv "$uphosts" "$old"
 mv "$inventory" "$old"
 echo "USERNAME <-> IP-ADDRESS <-> MAC-ADDRESS <-> MEMORY <->  CPU-INFORMATION" >> $inventory

for i in ${net[@]}; do
 echo  "for '$i'th network"
 for j in $(seq 1 254); do ip=192.168.$i.$j && ping -c 1 $ip>/dev/null; [ $? -eq 0 ] && echo "$ip is UP" && mac=`sshpass -p "$pass" ssh -o StrictHostKeyChecking=no root@"$ip" ifconfig | grep eth0 | awk '{ print $5}'` && mem=`sshpass -p "$pass" ssh -o StrictHostKeyChecking=no root@"$ip"  free -m | grep Mem | awk '{print $2}'`&& user=`sshpass -p "$pass" ssh -o StrictHostKeyChecking=no root@"$ip" ls -l /proc /| awk '{print $3}'| grep "\." | uniq` && if [ "$user" == "" ]; then  echo Unable to get the logged in user information for $ip; fi && cpu=`sshpass -p "$pass" ssh -o StrictHostKeyChecking=no root@"$ip" cat /proc/cpuinfo |grep 'model name' | head -n 1 | cut -d ":" -f 2` && echo "$user <-> $ip <-> $mac <-> $mem <-> $cpu" >> $inventory && echo "$ip" >> $uphosts || echo "$ip" >> $downhosts ; done
 done

finish_time=`date +%s`
 time_taken=`expr $finish_time - $start_time`
 if [ $time_taken -ge 60 ];
 then
 min=`expr $time_taken / 60`
 sec=`expr $time_taken % 60`
 if [ $min -ge 60 ];
 then
 hour=`expr $min / 60`
 min=`expr $min % 60`
 fi
 fi
 hour=`printf "%0*d\n" 2 $hour`
 min=`printf "%0*d\n" 2 $min`
 sec=`printf "%0*d\n" 2 $sec`
 echo "Script execution time: $hour:$min:$sec"

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

5,177 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>