#!/bin/bash

#USERNAME = whoami
# Def Functions

pattern10(){
	awk '{print "                              "$0}'
}

pattern20(){
	awk '{print "                    "$0}'
}

#--------------------------------------------------------------

echo -e "\n------系统通用信息------\n"
echo "user: $(whoami 2>/dev/null)"
echo "hostname: $(hostname 2>/dev/null)"
echo "arch: $(getconf LONG_BIT)"
echo "kernel: $(uname -r)"
yum -h >/dev/null 2>&1
if [ $? -eq 0 ];then echo "distribution: RHEL";fi
apt -h >/dev/null 2>&1
if [ $? -eq 0 ];then echo "distribution: Debian";fi
virtual_machine=$(dmidecode -s system-product-name 2>/dev/null)
if [ $? -eq 0 ];then
	echo "virtual machine: $virtual_machine"
else
	virtual_machine=$(ls -1 /dev/disk/by-id/ 2>/dev/null)
	if [ $? -eq 0 ];then
		echo "virtual machine: $virtual_machine"
	fi

fi

# NetWork
echo -e "\n-----Network Information-----\n"
echo "---IP:"
ip_ifconfig=$(ifconfig 2>/dev/null)
if [ $? -eq 0 ];then
	echo $(ifconfig |grep -v "127.0.0.1" |grep inet|awk '{print $2,$4}' 2>/dev/null)|pattern10
else
	ip_ipaddr=$(ip addr |grep -v "127.0.0.1" |grep inet|awk '{print $2}' 2>/dev/null)
	if test $? -eq 0;then
		echo $ip_ipaddr | pattern10
	fi
fi

echo "---DNS:"
cat /etc/resolv.conf 2>/dev/null | grep "nameserver"|pattern10
echo "---Router:"
route 2>/dev/null | grep default|pattern10
echo "---netstat -antp:"
netstat -antp 2>/dev/null|pattern10
echo "---netstat -anup:"
netstat -anup 2>/dev/null|pattern10

# User
echo -e "\n------用户信息------\n"
echo -e "---当前在线用户:"
w -sfhi|cut -d ' ' -f 1 2>/dev/null|pattern10

echo -e "---lastlog:"
lastlog=$(lastlog |grep -v "Never" >/dev/null 2>&1)
if [ $(lastlog |grep -v "Never"|wc -l) -gt 1 ];then
	printf "%-10s %s\n" " " "$lastlog";
else
	printf "%-10s %s\n" " " "N/A";
fi

user_count=$(ls -1 /home|wc -l 2>/dev/null)
echo "---/home:"
printf "%-10s %s\n" " " "/home下有$user_count个目录"
for user_name in $(ls -1 /home);do printf "%-10s %s\n" " " "└── $user_name";done 2>/dev/null
echo -e "---/ect/passwd:"
printf '%-10s %s %s %s %s\n' " " "权限:" $(ls -l /etc/passwd| cut -d ' ' -f 1,3,4 2>/dev/null)
echo -e "---/etc/shadow:"
printf '%-10s %s %s %s %s\n' " " "权限:" $(ls -l /etc/shadow| cut -d ' ' -f 1,3,4 2>/dev/null)

passwd_hash=$(grep -v '^[^:]*:[x]' /etc/passwd >/dev/null 2>&1)
if [ $? -eq 0 ];then
	printf "%-10s %s %s\n" " " "passwd文件中是否存在hash信息:" $passwd_hash;
else
	printf "%-10s %s %s\n" " " "passwd中存在hash:" "N/A";
fi

read_shadow=$(cat /etc/shadow >/dev/null 2>&1)
if [ $? -eq 0 ];then
	printf "%-10s %s\n" " " "shadow 文件可读性: 可读";
else
	printf "%-10s %s\n" " " "shadow 文件可读性: 不可读";
fi
sudo_nopass=$(echo '' | sudo -S -l -n >/dev/null 2>&1)
if [ $? -eq 0 ];then
	printf "%-10s %s\n" " " "sudo: NO PASS";
else
	printf "%-10s %s\n" " " "sudo: Need PASS ";
fi

echo "---/ect/shells:"
for file in $(cat /etc/shells|grep -v "#"|sort 2>/dev/null);do printf "%-10s %s\n" " " $file;done

# 服务信息收集
echo -e "\n------服务信息------\n"
echo "---ssh:"
echo "是否允许root登录:$(grep "PermitRootLogin " /etc/ssh/sshd_config 2>/dev/null|grep -v '#')"|pattern10
echo "ssh公钥:"|pattern10
#for file in $(find / -name "id_dsa*" -o -name "id_rsa*" -o -name "known_hosts" -o -name "authorized_hosts" -o -name "authorized_keys" 2>/dev/null |xargs -r ls -1);do printf "%-18s %s\n" " " $file;done

# hash_encode
echo -e "---hash:"
cat /etc/login.defs 2>/dev/null | grep "PASS_MAX_DAYS\|PASS_MIN_DAYS\|PASS_WARN_AGE\|ENCRYPT_METHOD" 2>/dev/null | grep -v "#" 2>/dev/null | pattern10

# crontab
echo "---crontab:"
echo "/etc/crontab:"|pattern10
grep -v "#" /etc/crontab  2>/dev/null | pattern20
echo "cron for every user:"|pattern10
cat /etc/passwd | cut -d ":" -f 1 | xargs -n1 crontab -l -u 2>/dev/null|pattern20
