Skip to content

Instantly share code, notes, and snippets.

@heri16
Last active April 6, 2023 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heri16/2a227121c7037de3ed14e092332961b2 to your computer and use it in GitHub Desktop.
Save heri16/2a227121c7037de3ed14e092332961b2 to your computer and use it in GitHub Desktop.
Automate login to keybase using Expect TCL

Usage

  • env KEYBASE_USERNAME=heri16 expect keybase-provision.sh

Interactive Terminal

chmod +x ./keybase-provision.sh
env KEYBASE_USERNAME=heri16 ./keybase-provision.sh

Unattended Terminal

chmod +x ./keybase-provision.sh
env KEYBASE_USERNAME=heri16 KEYBASE_DEVICENUM=2 ./keybase-provision.sh 1>/dev/null

Docker

docker build . -t keybase-img
docker run -e KEYBASE_USERNAME=heri16 -e KEYBASE_DEVICENUM=2 --rm -it keybase-img
FROM keybaseio/client:stable-slim
RUN apt-get update && apt-get install -y expect
COPY keybase-provision.sh /provision.sh
RUN chmod +x /provision.sh
CMD ["/provision.sh", "keybase", "chat", "send", "--channel", "'#general'", "stockbitcrypto", "'Hello World from bot'"]
#!/usr/bin/env expect
# Set the timeout period for the script to wait for the output
set timeout -1
# Check if KEYBASE_USERNAME is provided; if not, exit with a message
if {[info exists env(KEYBASE_USERNAME)]} {
set username $env(KEYBASE_USERNAME)
} else {
puts stderr "Error: Please provide the username via `KEYBASE_USERNAME`."
puts stderr "Usage: ./keybase-login.exp <username> <provisioning_devicenum> <new_devicename>"
exit 1
}
# Check if a KEYBASE_DEVICENUM is provided; if not, set the default value of 1
if {[info exists env(KEYBASE_DEVICENUM)]} {
set prov_devicenum $env(KEYBASE_DEVICENUM)
} else {
set prov_devicenum 1
}
# Check if a KEYBASE_DEVICENAME is provided; if not, set the default value of hostname
if {[info exists env(KEYBASE_DEVICENAME)]} {
set new_devicename $env(KEYBASE_DEVICENAME)
} else {
set new_devicename [exec hostname -s]
}
# Remove the first argument (program name) and store the remaining arguments
if {$argc > 0} {
set program_name [lindex $argv 0]
set remaining_args [lrange $argv 1 end]
}
# Spawn the keybase cli that generates the output
spawn keybase login $username
expect "Choose a device:"
sleep 1; send -- "$prov_devicenum\r"
expect "Enter a public name for this device:"
sleep 1; send -- "$new_devicename\r"
expect "Type this verification code into your other device:"
# Extract the verification code using regular expression and save it in the variable 'code'
expect -re {(\w+ \w+ \w+ \w+ \w+ \w+ \w+ \w+)}
set code $expect_out(1,string)
expect "It will then prompt you for the verification code above."
# Print the extracted verification code to stderr
# puts stderr "Verification code for `keybase device add`:\n$code"
# Wait for the spawned program to close
expect eof
if {[info exists program_name]} {
# Spawn the user program with additional arguments
spawn $program_name {*}$remaining_args
# Enable direct interaction between the user and the spawned process
interact
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment