forked from dmbaturin/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installdate.sh
executable file
·140 lines (119 loc) · 4.44 KB
/
installdate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/sh
# Try to find out system installation date
# WARNING: it isn't a forensic tool or something,
# as there aren't really immutable entities in
# any system, so any attempt to determine
# installation date is no more than a guess.
#
# It was written for fun and research purposes
# and the only valid use for it is to check
# how old a system running on a dusty server
# in a closet is before shutting it down forever.
# Copyright (C) 2012 Daniil Baturin <daniil at baturin dot org>
#
# Permission is hereby granted, free of charge,
# to any person obtaining a copy of this software
# and associated documentation files (the "Software"),
# to deal in the Software without restriction,
# including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons
# to whom the Software is furnished to do so, subject
# to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
if [ -n "$1" ]; then
DATE_FORMAT="$1"
else
DATE_FORMAT="%F"
fi
if [ `id -u` != 0 ]; then
echo "Some checks require root privileges!"
exit 1
fi
# Defaults
INSTALL_DATE=Unknown
SOURCE=None
OS=`uname`
result() {
echo "Installation date: $INSTALL_DATE"
echo "Information source: $SOURCE"
exit
}
case "$OS" in
Linux)
## First try "good", distribution
## specific methods (installer logs mostly)
# Debian-based
if [ -d "/var/log/installer" ]; then
RAW_DATE=`stat --format "%y" /var/log/installer/`
INSTALL_DATE=`date -d "$RAW_DATE" +"$DATE_FORMAT"`
SOURCE="Installer log"
result
fi
# RedHat-based
if [ -f "/root/install.log" ]; then
RAW_DATE=`stat --format "%y" /root/install.log`
INSTALL_DATE=`date -d "$RAW_DATE" +"$DATE_FORMAT"`
SOURCE="Installer log"
result
fi
## No luck with good methods.
## Try to find out root filesystem creation date,
## most likely it's the same to installation date.
ROOT_DEV=`mount | grep " / " | awk -F' ' '{print $1}'`
ROOT_OPT=`mount | grep $ROOT_DEV`
ROOT_FS=`echo $ROOT_OPT | awk -F' ' '{print $5}'`
if [ ! -z `echo $ROOT_FS | grep "ext"` ]; then
# ext2/ext3/ext4
RAW_DATE=`tune2fs -l $ROOT_DEV | grep "Filesystem created" | sed -e 's/Filesystem created:\s\+//'`
INSTALL_DATE=`date -d "$RAW_DATE" +"$DATE_FORMAT"`
SOURCE="Filesystem creation date"
result
elif [ ! -z `echo $ROOT_FS | grep "jfs"` ]; then
# JFS
RAW_DATE=`jfs_tune -l $ROOT_DEV | grep "Filesystem creation" | sed -e 's/Filesystem creation:\s\+//'`
INSTALL_DATE=`date -d "$RAW_DATE" +"$DATE_FORMAT"`
SOURCE="Filesystem creation date"
result
fi
;;
FreeBSD)
# Apparently /home -> /usr/home symlink is
# never changed under normal circumstances,
# so should be as old as the system itself
RAW_DATE=`stat -f%m /home`
INSTALL_DATE=`date -j -f "%s" $RAW_DATE +"%F"`
SOURCE="/home symlink"
result
;;
Darwin)
# Mac OS X, actually
if [ -r "/private/var/db/.AppleSetupDone" ]; then
RAW_DATE=`stat -f%m /private/var/db/.AppleSetupDone`
INSTALL_DATE=`date -j -f "%s" "$RAW_DATE" +"$DATE_FORMAT"`
SOURCE=".AppleSetupDone"
result
fi
;;
SunOS)
# SUNWsolnm contains /etc/release and apparently
# is never updated, so its installation date
# should represent system installation date too.
INSTALL_DATE=`pkginfo -l SUNWsolnm | grep INSTDATE | sed -e 's/\s*INSTDATE:\s*//'`
SOURCE="SUNWsolnm package"
result
;;
*)
echo "Sorry, operating system $OS is not supported"
exit 1
esac