-
Notifications
You must be signed in to change notification settings - Fork 4
/
gpg-import.sh
executable file
·55 lines (50 loc) · 1.82 KB
/
gpg-import.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
#!/bin/sh
########################################################################
# gpg-import.sh: GPG Key Import Script for APT
#
# Description:
# This script imports a GPG public key from a specified keyserver and
# adds it to the APT keyring. It's useful for adding external repository
# keys securely. Only works on Debian-based systems.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# v1.2 2024-01-18
# Standardized command existence checks using a common function.
# v1.1 2023-12-05
# Added environment check for Debian-based systems.
# Refactored for improved readability and added usage information.
# v1.0 2008-08-22
# Initial release. Imports GPG keys for APT from a keyserver.
#
# Usage:
# ./gpg-import.sh KEYSERVER PUBKEY
#
########################################################################
check_commands() {
for cmd in "$@"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: Command '$cmd' is not installed. Please install $cmd and try again."
exit 127
elif ! [ -x "$(command -v "$cmd")" ]; then
echo "Error: Command '$cmd' is not executable. Please check the permissions."
exit 126
fi
done
}
# Check if required commands are installed
check_commands gpg apt-key
# Check if both arguments are provided
if [ -n "$2" ]; then
# Import the GPG key from the specified keyserver
gpg --keyserver "$1" --recv-keys "$2"
# Export the GPG key and add it to the APT keyring
sudo gpg --armor --export "$2" | sudo apt-key add -
else
# Display usage information if arguments are missing
echo "Usage: $0 KEYSERVER PUBKEY"
fi