-
Notifications
You must be signed in to change notification settings - Fork 0
/
getkeys.sh
executable file
·83 lines (59 loc) · 2.44 KB
/
getkeys.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
#!/bin/bash
# Store old Internal File Separator to return IFS to its former value in the end. Setting IFS to $'\n' enables the very useful for...in... loop to read entire lines
OLDIFS=$IFS
IFS=$'\n'
# Check if script is run as root.
if [ $(id -u) -ne 0 ]
then echo "Please run this script with sudo or as root. Exiting..."
# If root, run the script.
# Store missing keys in ~/keys and count them.
else echo -n "Running aptitude update to get IDs of missing keys... "
sudo aptitude update >/dev/null 2>~/errors
cat ~/errors | grep "signatures" | cut -d " " -f 21 >~/keys
numberkeys=$(wc -l ~/keys | cut -d " " -f 1)
echo "done."
echo "Need to retrieve $numberkeys keys..."
# Execute this block only if there are keys to retrieve.
# Retrieve keys one by one, incrementing count to display progress to user.
if [ $numberkeys -gt 0 ]
then count=1
for key in $(cat ~/keys)
do echo -n " Retrieving key $count/$numberkeys... "
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $key >/dev/null 2>/dev/null
echo "done."
(( count=$count+1 ))
done
rm ~/keys
# Before exit, verify that all keys are now accounted for.
# Therefore store possible missing keys in ~/keys.
echo -n "Verifying success... "
sudo aptitude update >/dev/null 2>~/errors
cat ~/errors | grep "signatures" | cut -d " " -f 21 >~/keys
# If file ~/keys is empty, then no keys need to be retrieved any more.
if [ $(wc -l ~/keys | cut -d " " -f 1) -eq 0 ]
then echo "Job succeeded"\!
else echo "Arg"\!" Something failed..."
echo "More keys still seem to be missing, run this script again to try to fix the issue. If the problem persists , run sudo aptitude update to find out what is going wrong."
rm ~/errors
fi
fi
# End of the block executed only if keys were missing.
# If errors aptitude update output errors, tell user and ask him if he wants them displayed. Act accordingly.
if [ $(wc -l ~/errors | cut -d " " -f 1) -eq 0 ]
then rm ~/errors
else echo "While running aptitude update, the script has encountered problems, even if no keys are missing. Do you want to display these errors?(yes,no): "
read choice
while [ "$choice" != "yes" ] && [ "$choice" != "no" ]
do echo "Please answer yes or no :"
read choice
done
if [ "$choice" == "yes" ]
then echo "These errors are stored in $HOMEDIR/log :"
cat ~/errors
fi
fi
# Clean up before exit.
echo "Job finished, cleaning up."
rm ~/keys
fi
IFS=$OLDIFS