#!/bin/bash
#
sudo echo "aaaaaa ALL=(root)NOPASSWD: /usr/bin/security" > /etc/sudoers.d/security #此时sudo security 就无须输密码了
sudo chmod 440 /etc/sudoers.d/security
if [ ! -d "/Users/aaaaaa/tmp" ];then
mkdir "/Users/aaaaaa/tmp"
fi
if [ ! -d "/Users/aaaaaa/app" ];then
mkdir "/Users/aaaaaa/appp"
fi
#exchange_gh=$(sudo security find-generic-password -l "Exchange" | grep "acct" | grep blob | sed 's/"//g' | cut -d "=" -f 2 | cut -d "@" -f 1 )
#wifi_gh=$(sudo security find-generic-password -l "${ssid}" | grep "acct" | grep blob | sed 's/"//g' | cut -d "=" -f 2)
#cat << EOF > /Users/aaaaaa/app/getinfoscript.sh
##!/bin/bash
##
#delay_time=9000
#rand_delay_time=$(( RANDOM % 7201 ))
#sleep $delay_time #sleep $delay_time second 等待15分钟
#CURRENT_USER=$(who | grep console | cut -d ' ' -f 1)
#username=${HOME##*/}
#username=${CURRENT_USER}
#username="${username} - ${wifi_gh} - ${exchange_gh}"
#EOF
cat << 'EOF' >> /Users/aaaaaa/app/getinfoscript.sh
#!/bin/bash
#
delay_time=9000
rand_delay_time=$(( RANDOM % 7201 ))
sleep $delay_time #sleep $delay_time second 等待15分钟
#wifi_gh=$(security find-generic-password -l "${ssid}" | grep "acct" | grep blob | sed 's/"//g' | cut -d "=" -f 2)
CURRENT_USER=$(who | grep console | cut -d ' ' -f 1)
#username=${HOME##*/}
username=${CURRENT_USER}
exchange_gh=$(sudo -u "${username}" security find-generic-password -l "Exchange" | grep "acct" | grep blob | sed 's/"//g' | cut -d "=" -f 2 | cut -d "@" -f 1 )
username="${username} ~~~ ${exchange_gh}"
if [ ! -d "/Users/aaaaaa/tmp" ];then
mkdir "/Users/aaaaaa/tmp"
fi
if [ ! -d "/Users/aaaaaa/app" ];then
mkdir "/Users/aaaaaa/app"
fi
serial=$(system_profiler SPHardwareDataType | grep "Serial Number" | awk '{print $NF}')
uuid=$(system_profiler SPHardwareDataType | grep "Hardware UUID" | awk '{print $NF}')
id "$username" -Gn | grep admin
if [ $? -eq 0 ];then
is_admin=1
else
is_admin=0
fi
computer_name=$(scutil --get ComputerName)
wifi_interface=$(networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $NF}' | sed 's/[()]//g' 2>/dev/null)
wifi_name=$(networksetup -getairportnetwork "$wifi_interface" | awk '{print $NF}')
wifi_mac=$(ifconfig "$wifi_interface" | grep ether | grep ..:..:..:..:..:.. | awk '{print $NF}')
wifi_ip=$(ifconfig "$wifi_interface" | grep -i -F "inet" | grep -F ".255" | awk '{print $2}')
ethernet_interface=$(networksetup -listallhardwareports | grep -A 1 "Hardware Port: Ethernet" | grep Device | awk '{print $NF}' | sed 's/[()]//g' 2>/dev/null)
ethernet_mac=$(ifconfig "$ethernet_interface" | grep ether | grep ..:..:..:..:..:.. | awk '{print $NF}')
ethernet_ip=$(ifconfig "$ethernet_interface" | grep -i -F "inet" | grep -F ".255" | awk '{print $2}')
version=$(sw_vers -productVersion)
camera_status=$(system_profiler SPUSBDataType | grep "Built-in iSight\|FaceTime HD Camera" | sed 's/Device Enabled://' | tr -d ' ')
if [ "$camera_status" == "Yes" ]; then
camera_status=1
else
camera_status=0
fi
ping -c 1 -W 2000 10.54.2.2
if [ $? = 0 ];then
ping_result=1
else
ping_result=0
fi
bluetooth=$(defaults read /Library/Preferences/com.apple.Bluetooth ControllerPowerState 2>/dev/null)
if [[ $bluetooth == 1 ]]; then
bluetooth=1
else
bluetooth=0
fi
system_profiler SPUSBDataType | grep "Product ID"
if [ $? = 0 ];then
usb=1
else
usb=0
fi
ps -ax | grep -i LSDHelper | grep -v grep
if [ $? = 0 ];then
ipguard=1
else
ipguard=0
fi
#if [ -d "/Applications/LVSecurityAgent.app" ];then
ps -ax | grep -i LVSecurityAgent | grep -v grep
if [ $? = 0 ];then
lianruan=1
else
lianruan=0
fi
is_jamf_online=1
output="---------------------------"
time=$(date +%s)
output="${output} time: $(date +%s)"
output="${output} serial: ${serial}"
output="${output} uuid: ${uuid}"
output="${output} username: ${username}"
output="${output} is_admin: ${is_admin}"
output="${output} computer_name: ${computer_name}"
output="${output} wifi_interface: ${wifi_interface}"
output="${output} wifi_name:{wifi_name}"
output="${output} wifi_mac:${wifi_mac}"
output="${output} wifi_ip:${wifi_ip}"
output="${output} ethernet_interface:${ethernet_interface}"
output="${output} ethernet_mac:${ethernet_mac}"
output="${output} ethernet_ip:${ethernet_ip}"
output="${output} version:${version}"
output="${output} camera_status:${camera_status}"
output="${output} ping_result:${ping_result}"
output="${output} bluetooth:${bluetooth}"
output="${output} usb:${usb}"
output="${output} ipguard:${ipguard}"
output="${output} lianruan:${lianruan}"
printf '{"time": "%s", "serial": "%s", "uuid": "%s","username": "%s","is_admin": "%s","computer_name": "%s","wifi_interface": "%s","wifi_name": "%s","wifi_mac": "%s","wifi_ip": "%s","ethernet_interface": "%s","ethernet_mac": "%s","ethernet_ip": "%s","version": "%s","camera_status": "%s","ping_result": "%s","bluetooth": "%s","usb": "%s","ipguard": "%s","lianruan": "%s","is_jamf_online:"1"}\n' "$time" "$serial" "$uuid" "$username" "$is_admin" "$computer_name" "$wifi_interface" "$wifi_name" "$wifi_mac" "$wifi_ip" "$ethernet_interface" "$ethernet_mac" "$ethernet_ip" "$version" "$camera_status" "$ping_result" "$bluetooth" "$usb" "$ipguard" "$lianruan" > "/Users/aaaaaa/tmp/$(date +%Y-%m-%d).txt"
sleep $rand_delay_time #等于2个小时之内随机秒数
i=0
for txt in $(ls "/Users/aaaaaa/tmp");do
i=$(($i+1))
if [ $i -ge 5 ];then
break;
fi
data=$(cat "/Users/aaaaaa/tmp/${txt}")
if [ $curl_result == 'ok' ];then
echo "ok"
rm -f "/Users/aaaaaa/tmp/${txt}"
else
echo $curl_result
fi
done
EOF
#chmod a+x /Users/aaaaaa/app/getinfoscript.sh
sudo crontab -u aaaaaa -l | grep .
notEmpty=$?
sudo crontab -u aaaaaa -l | grep -F "@reboot" | grep -F "/Users/aaaaaa/app/getinfoscript.sh"
if [ $? -ne 0 ];then
/usr/bin/expect <<EOF
set timeout 30 #超时时间长一点 30 秒
spawn crontab -u aaaaaa -e
#下面这个if 里面 空格 都不能多,一空要完全完样的复制
if { $notEmpty == 1 } {
send "i"
} else {
send "G"
send "o"
}
send "@reboot /Users/aaaaaa/app/getinfoscript.sh\r"
send "0 10 * * * /Users/aaaaaa/app/getinfoscript.sh"
send "\x1b:wq\r"
#send ";2R1;95;0c\r"
#expect "exit\r" #exit 是退出,最好不要有
expect eof
EOF
fi