Skip to content

Instantly share code, notes, and snippets.

@thanhph111
Last active June 17, 2024 01:57
Show Gist options
  • Save thanhph111/27f69a5c65cc2d0fe2f37e2449f79c07 to your computer and use it in GitHub Desktop.
Save thanhph111/27f69a5c65cc2d0fe2f37e2449f79c07 to your computer and use it in GitHub Desktop.
Batch add recovery codes to 1Password #guide

Batch add recovery codes to 1Password

You want to store your recovery codes to 1Password after setting up one-time password but too lazy to add them manually? Here is the script to automatically do that using 1Password CLI.

  1. First, you need to install 1Password CLI.
  2. Then store your recovery codes line by line in a file named the item ID you want to add, you can find the ID of the item with this command:
op item list
  1. Finally, run the script with the recovery code file or a folder contains multiple recovery code files.

Examples:

$ cat ~/Downloads/op/rahzlludgpggjqs6zszgpe34la
6501 5184
3856 9113
2963 2385
0927 3516
5486 2267

$ cat ~/Downloads/op/3afisa8bszydcorcjy3i5wbyaru.txt
szqpf68cf9
7jaaka4zc0
1rd1tjkga7
azzwfw0xnx
89stsj5j1e
3xyv30qr45
pdq9qpvj2j
6236rt7g6t

$ ./op-add-recovery-code.sh ~/Downloads/op
==> Processing '/Users/thanhph111/Downloads/op/rahzlludgpggjqs6zszgpe34la'
Found item 'rahzlludgpggjqs6zszgpe34la' (Instagram), adding recovery code
    Added code 1: 6501 5184
    Added code 2: 3856 9113
    Added code 3: 2963 2385
    Added code 4: 0927 3516
    Added code 5: 5486 2267

==> Processing '/Users/thanhph111/Downloads/op/3afisa8bszydcorcjy3i5wbyaru.txt'
Found item '3afisa8bszydcorcjy3i5wbyaru' (Firefox), adding recovery code
    Added code 1: szqpf68cf9
    Added code 2: 7jaaka4zc0
    Added code 3: 1rd1tjkga7
    Added code 4: azzwfw0xnx
    Added code 5: 89stsj5j1e
    Added code 6: 3xyv30qr45
    Added code 7: pdq9qpvj2j
    Added code 8: 6236rt7g6t

Result:

Instagram Firefox
Instagram recovery codes Firefox recovery codes
#!/usr/bin/env bash
# op-add-recovery-code.sh: Add recovery code to the 1Password items.
# Usage: op-add-recovery-code.sh file_or_folder_path
# Step:
# 1. Set up 1Password CLI:
# https://developer.1password.com/docs/cli/get-started
# 2. Store list of recovery codes line by line in a file whose name is the
# item ID you want to add (get them by running `op item list`).
# 3. Run this script with the file or folder include multiple files as an
# argument.
set -e
add_recovery_code_from_file() {
filepath="$1"
echo "==> Processing '$filepath'"
# Check if the file exists
[ ! -f "$filepath" ] && {
echo "'$filepath' does not exist, skipping"
return
}
# Check if the file mime is text-based
file_mime=$(file --mime-type -b "$filepath")
[ "$file_mime" != "text/plain" ] && {
echo "'$filepath' is not a text file, skipping"
return
}
# Get the length of the file
grep -q '[^[:space:]]' <"$filepath" || {
echo "'$filepath' is empty, skipping"
return
}
line_count=$(grep -c "" "$filepath")
# Get the width of the `line_count` number
max_index_width=${#line_count}
# Check if the item exists in 1Password
filename="$(basename $filepath)"
item_id="${filename%.*}"
if output=$(op item get $item_id --cache); then
item_name=$(sed -nr 's/^Title:[[:space:]]+//p' <<<"$output")
echo "Found item '$item_id' ($item_name), adding recovery code"
else
echo "Failed to get item '$item_id', skipping"
return
fi
# Loop through the file and add the recovery code
index=0
# while read line; do
while IFS="" read -r line || [ -n "$line" ]; do
# if line is empty, skip
[ -z "$line" ] && {
echo "warning: '$line'"
continue
}
index=$((index + 1))
formatted_index=$(printf "%0${max_index_width}d" $index)
op item edit "$item_id" \
"Recovery Codes.code $formatted_index[password]=$line" \
&>/dev/null || {
echo " Failed to add code $formatted_index: $line"
return
}
echo " Added code $formatted_index: $line"
done <"$filepath"
}
# Check if `op` is installed
command -v op &>/dev/null || {
echo "'op' is not installed, skipping"
exit 0
}
first_argument="$1"
# Check if argument is folder or file
if [ -d "$first_argument" ]; then
# If argument is a folder, loop through all files in the folder
for filepath in "$first_argument"/*; do
add_recovery_code_from_file "$filepath"
echo
done
elif [ -f "$first_argument" ]; then
# If argument is a file, add the recovery code from the file
add_recovery_code_from_file "$first_argument"
else
echo "Invalid argument: '$first_argument'"
echo "Usage: $0 <folder_or_file>"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment