Skip to content

Instantly share code, notes, and snippets.

@aussiehash
Created June 12, 2019 12:45
Show Gist options
  • Save aussiehash/4a378ecad86deb9cadc3be4cb0d568de to your computer and use it in GitHub Desktop.
Save aussiehash/4a378ecad86deb9cadc3be4cb0d568de to your computer and use it in GitHub Desktop.
###### User friendly interface script for interaction with ./dojo [command] ######
## Written for use with Ubuntu Desktop 18.04 LTS ##
This script is setup to run locally from the machin that your Dojo is installed on, but if you wanted to use it via SSH
there is a write up below the script on how to set that up.
## Open a terminal
$ mkdir scripts
$ cd scripts && touch control_dojo.sh
## Make it executable and then edit
$ chmod +x control_dojo.sh
$ nano control_dojo.sh
## Copy and paste the code below (between the --------------'s ), and make appropriate changes
Begining of conrol_dojo.sh script
-------------------------------------------------------------------------------------------------------------------------
#!/bin/bash
######### Dojo information ####################################################################
# All you need to edit is the following variable for the script to work if running locally.
PATH_T0_DOJO_DOT_SH="/home/path/to/dojo_dir/docker/my-dojo/"
# If you want to use it remotely from a different machine, change SSH_OPTION to "yes"
# and input the correct SSH_PORT & IP information, as well as make sure you have the
# remote machine's rsa pubkeys in your Dojo machine`s root user`s `/root/.ssh/authorized_keys`
# file, then run this script on that remote machine.
SSH_OPTION="no"
SSH_PORT="2222"
IP="xxx.xxx.x.xxx"
##############################################################################################
SSH_CMD="ssh -tt -p $SSH_PORT root@$IP"
CMD1="cd $PATH_T0_DOJO_DOT_SH"
DOJO_CMD=("---EXIT---" "help" "bitcoin-cli" "logs" "onion" "restart" "start" "stop" "install" "uninstall")
LOG_MODULES=("---BACK---" "bitcoind" "db" "tor" "api" "tracker" "pushtx" "pushtx-orchest")
while true; do
GOBACK="no"
echo " "
echo "---------------- SAMOURAI DOJO INTERACTION SCRIPT ----------------"
# Display options for user selection
for ((i=0; i < ${#DOJO_CMD[*]}; i++)); do
echo " "
echo " $i ) ${DOJO_CMD[$i]}"
done
# Prompt for user selection
while true; do
echo " "
read -p "Please enter a number corresponding to what you'd like to do: " NUM
echo "------------------------------------------------------------------"
# Numbers outside available options loop back
if [[ $NUM -lt 0 || $NUM -gt ${#DOJO_CMD[*]}-1 ]]; then
echo " "
echo "Option not available, please try again..."
echo " "
sleep 2
# User selected Exit
elif [ $NUM -eq 0 ]; then
exit 0
# Prompt for confirmation on start, stop, restart, install, and uninstall options
elif [ $NUM -gt 4 ]; then
while true; do
echo " "
read -p "Please confirm you would like to ${DOJO_CMD[$NUM]} the Dojo [y/n]: " yn
case $yn in
[Yy]* ) CONFIRM="yes"; break;;
[Nn]* ) GOBACK="yes"; break;;
* ) echo "Please answer y or n."
esac
done
else
break
fi
# Prompt again for uninstall
if [ $NUM -eq 9 ]; then
while true; do
echo " "
read -p "Are you ABSOLUTELY sure you'd like to ${DOJO_CMD[$NUM]} the Dojo [y/n]:" yn
case $yn in
[Yy]* ) CONFIRM="yes"; break;;
[Nn]* ) GOBACK="yes"; break;;
* ) echo "Please answer y or n."
esac
done
fi
# Break primary while loop if user selects yes for confirmations
if [[ "$GOBACK" = "yes" || "$CONFIRM" = "yes" ]]; then
break
fi
done
# Options for logs
if [ "${DOJO_CMD[$NUM]}" = "logs" ]; then
while true; do
EXECUTE="no"
echo " "
echo " ---Available Logs---"
echo " "
# Display options for user selection
for ((i=0; i < ${#LOG_MODULES[*]}; i++)); do
echo " $i) ${LOG_MODULES[$i]}"
echo " "
done
echo "----------- press CTRL+C to exit the log when finished -----------"
echo " "
read -p "Please enter a number corresponding to what logs you'd like to view: " LNUM
echo " "
# Numbers outside available options loop back
if [[ $LNUM -lt 0 || $LNUM -gt ${#LOG_MODULES[*]}-1 ]]; then
echo "Option not available, please try again..."
echo " "
sleep 2
# User selection to go back
elif [ $LNUM -eq 0 ]; then
GOBACK="yes"
break
# Additional command options for api tracker pushtx pushtx-orchest
elif [ $LNUM -gt 3 ]; then
echo "Available options are '-d [VALUE]' **OR** '-n [VALUE]'"
echo " "
read -p "Please enter one now: " AVAIL_OPTIONS
EXECUTE="yes"
else
AVAIL_OPTIONS=""
EXECUTE="yes"
fi
if [ "$EXECUTE" = "yes" ]; then
trap "echo" SIGINT SIGTERM
if [ "SSH_OPTION" = "yes" ]; then
$SSH_CMD "$CMD1 && ./dojo.sh logs ${LOG_MODULES[$LNUM]} $AVAIL_OPTIONS"
else
$CMD1 && sudo ./dojo.sh logs ${LOG_MODULES[$LNUM]} $AVAIL_OPTIONS
fi
trap - SIGINT SIGTERM
fi
done
fi
# bitcoin-cli interaction
if [ "${DOJO_CMD[$NUM]}" = "bitcoin-cli" ]; then
while true; do
read -p "bitcoin-cli [what command?] (x to go back): " BCMD
if [[ "$BCMD" = "X" || "$BCMD" = "x" ]]; then
break
else
if [ "SSH_OPTION" = "yes" ]; then
$SSH_CMD "$CMD1 && ./dojo.sh ${DOJO_CMD[$NUM]} $BCMD"
else
$CMD1 && sudo ./dojo.sh ${DOJO_CMD[$NUM]} $BCMD
fi
echo "------------------------------------------------------------------"
fi
done
# If user selected yes to a prompt, run the selection
elif [ "$GOBACK" != "yes" ]; then
if [ "SSH_OPTION" = "yes" ]; then
$SSH_CMD "$CMD1 && ./dojo.sh ${DOJO_CMD[$NUM]}"
else
$CMD1 && sudo ./dojo.sh ${DOJO_CMD[$NUM]}
fi
echo " "
# Prompt for another action
while true; do
read -p "Do something else? [y/n]: " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit 0;;
* ) echo "Please answer y or n."
esac
done
fi
done
--------------------------------------------------------------------------------------------------------------------------
End of control_dojo.sh script
########################### Add a .desktop file to your App Tray to launch control_dojo.sh ###############################
## Get the Samourai logo for app icon...
$ cd /path/to/your/Dojo Directory/ ## <-- for example, mine is at ~/dojo_dir/
$ wget https://easymineinvestments.com/wp-content/uploads/2017/10/sam-logo2.png
$ mv sam-logo2.png samourai_logo.png
## Make a new .desktop file
$ sudo nano /usr/share/applications/samourai-dojo.desktop
## Copy and paste the code below (between the -----------'s ) and make changes that are ***starred***
Begining of samourai-dojo.desktop file
--------------------------------------------------------------------------------------------------------------------------
[Desktop Entry]
Encoding=UTF-8
Type=Application
Version=1.0
Terminal=false
Exec=gnome-terminal --title="Samourai Dojo" --geometry=120x25-0+0 --zoom=0.8 -- bash -c 'cd ***/home/user/path/to/script/***; ./control_dojo.sh; $SHELL'
Name=Samourai Dojo
Icon=***/home/user/path/to***/samourai_logo.png
Name[en_US]=Samourai Dojo
---------------------------------------------------------------------------------------------------------------------------
End of samourai-dojo.desktop file
## Save and exit
ctrl+x --> y --> return
## Now it should be in your Show Applications tray
######### [OPTIONAL] Setup passwordless ssh login to your Machine running Dojo #############################################
I set it up to be used with passwordless root login via ssh, also, as you need sudo privledges to
interact with the ./dojo [commands]. I did it so that I can control the Dojo from my Host machine.
This option may not be for you if you don't want to permit root login via ssh to the machine running
your Dojo.
I have my VMs set up with passwordless pubkeys and UFW so that my host is the only machine that can
login to the VMs via ssh. It is somewhat of a security risk if you do not structure yourself properly,
so please be cautious.
If you wanted to use it remotely via SSH, below are haggard instructions for setting that up to work.
My ssh setup as an example... passwordless ########
|-------------------> # VM 1 #
| pubkey ########
########## pubkey + 2FA + user pass ################ | UFW (Host machine &
# Laptop # ---------------------------------> # Host Machine # <-----| other VMs) VMs only)
########## ################ |
UFW (Laptop & VMs Only) | passwordless ########
|-------------------> # VM 2 #
| pubkey ########
| UFW (Host machine &
| other VMs) VMs only)
|
| passwordless ########
|-------------------> # VM 3 #
| pubkey ########
| UFW (Host machine &
etc. other VMs) VMs only)
## **On the machine that runs Dojo, as non-root user** ##
## If you have already generated passwordless ssh keys, go to STEP 2
## STEP 1:
$ ssh-keygen -b 4096
enter --> enter --> enter
## Correct permissions
$ sudo chmod 700 ~/.ssh
## Get your public key
## STEP 2:
$ cat ~/.ssh/id_rsa.pub
## Copy the pubkey
## Add your non-root user as an authorized login to your root user on your Dojo
## STEP 3:
$ sudo -s
$ if [ -d /root/.ssh ]; then nano /root/.ssh/authorized_keys; else mkdir /root/.ssh; nano /root/.ssh/authorized_keys; fi
##Paste your non-root user's pubkey into your root user's authorized_key file
## Save and exit
ctrl+x --> y --> return
## Correct permissions
$ chmod 600 /root/.ssh/authorized_keys
## Log out of root user
$ exit
## On your laptop or remote machine that you login to your Dojo with,
## repeate STEP 1 & STEP 2, then do STEP 3 again for your Dojo root user
## Add your laptop or remote machine's pubkeys to the authorized_keys of your non-root Dojo user
## STEP 4:
$ nano ~/.ssh/authorized_keys
### Paste your laptop's pubkey into your non-root user's authorized_key file
### Save and exit
ctrl+x --> y --> return
## Correct permissions
$ sudo chmod 600 ~/.ssh/authorized_keys
## Configure sshd_config on Dojo machine
## If you login to the machine that runs Dojo from anywhere else, you will need to add that machine's pubkyes to
## the user's authorized_keys file, otherwise you will be locked out...
$ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
$ sudo nano /etc/ssh/sshd_config
## Alterations to /etc/ssh/sshd_config:
Port 2222 # <-- only if you want to change it, make sure to update UFW and the script above...
PermitRootLogin yes
PubkeyAuthentication yes
PasswordAuthentication no
## Save and exit
ctrl+x --> y --> return
## Restart sshd service
$ sudo service sshd restart
## DO NOT EXIT OUT OF THE TERMINAL CURRENTLY LOGGED INTO YOUR DOJO MACHINE
## On a remote machine you set this up to work with, open a terminal and try to login to the Dojo via ssh.
## Be sure to try loging into both Dojo's non-root & root users
## Done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment