Get two-factor authentication code from terminal?

I came up with a pretty simple solution to this problem using AppleScript that should print the 2FA code:

#! /usr/bin/env osascript tell application "System Events" if name of every process contains "FollowUpUI" then tell window 1 of process "FollowUpUI" click button "Allow" delay 2 set code to value of static text 1 of group 1 log (code) click button "Done" end tell else log ("Couldn't find 2FA window") end if end tell return

Adding this to a file and making it executable using chmod +x name_of_file.scpt should create a script that can be run over ssh by executing ./name_of_file.scpt while in the same directory as the file.

Note: When you run this script over ssh for the first time, the following popup should appear:

enter image description here

Simply clicking "OK" should allow the script to run properly. This preference will automatically be saved, and can be modified by going into System Preferences->Security & Privacy->Privacy->Accessibility

This worked for me on macOS Mojave, but it may work on other versions as well.