Skip to content

Instantly share code, notes, and snippets.

@meeDamian meeDamian/10-uname Secret
Last active Aug 1, 2018

Embed
What would you like to do?
#!/bin/sh
# motd is generated with empty environment. This ensures that TERM is set, so that colors are displayed properly
# src: https://unix.stackexchange.com/a/417223/31104
export TERM="${TERM:-xterm-256color}"
# RED=$(tput setaf 1)
# GREEN=$(tput setaf 2)
# YELLOW=$(tput setaf 3)
# BLUE=$(tput setaf 4)
# VIOLET=$(tput setaf 5)
# SOME_COLOR=$(tput setaf 6)
WHITE=$(tput setaf 7)
RESET_STYLE=$(tput sgr0)
echo
echo -n "${WHITE}Raspberry Pi ($(hostname))${RESET_STYLE} " && uname -rvm
uptime
#!/bin/sh
# motd is generated with empty environment. This ensures that TERM is set, so that colors are displayed properly
# src: https://unix.stackexchange.com/a/417223/31104
export TERM="${TERM:-xterm-256color}"
BOLD=$(tput bold)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
# BLUE=$(tput setaf 4)
# VIOLET=$(tput setaf 5)
SOME_COLOR=$(tput setaf 6)
# WHITE=$(tput setaf 7)
RESET_STYLE=$(tput sgr0)
ram_total=$(free -mh | grep Mem | awk '{ print $2 }')
ram_used=$(free -mh | grep Mem | awk '{ print $3 }')
swap_memory=$(free -mh | grep -i swap)
swap_total=$(echo ${swap_memory} | awk '{ print $2 }')
if [ "${swap_total}" != "0" ]; then
swap_used=$(echo ${swap_memory} | awk '{ print $3 }')
swap_info="swap: ${BOLD}${swap_used} / ${swap_total}${RESET_STYLE}"
fi
sd_total=$(df -h | grep "/$" | awk '{ print $2 }')
sd_used=$(df -h | grep "/$" | awk '{ print $3 }')
external_storage=$(df -h | grep '/dev/sd')
if [ ! -z "${external_storage}" ]; then
external_total=$(echo ${external_storage} | awk '{ print $2 }')
external_used=$(echo ${external_storage} | awk '{ print $3 }')
external_info="external: ${BOLD}${external_used} / ${external_total}${RESET_STYLE}"
fi
btc_path=$(command -v bitcoin-cli)
if [ ! -z ${btc_path} ]; then
btc_line1="₿itcoin Core"
bitcoin_user=$(ps -eo user:10,command | grep "[b]itcoind" | awk '{ print $1 }')
data_dir="/home/${bitcoin_user}/.bitcoin"
BLOCKCHAIN_INFO="$(bitcoin-cli -datadir=${data_dir} getblockchaininfo 2> /dev/null)"
chain="$(echo ${BLOCKCHAIN_INFO} | jq -r '.chain')"
if [ ! -z $chain ]; then
chain="${chain}net"
fi
blocks="$(echo ${BLOCKCHAIN_INFO} | jq -r '.blocks')"
# add current sync status
progress="$(echo ${BLOCKCHAIN_INFO} | jq -r '.verificationprogress')"
percentage=$(printf "%.2f%%" "$(echo $progress | awk '{print 100 * $1}')")
if [ -n $percentage ]; then
l2_extra="sync progress: ${percentage}"
fi
PEER_INFO="$(bitcoin-cli -datadir=${data_dir} getpeerinfo 2> /dev/null)"
peers_count=$(echo ${PEER_INFO} | jq 'length')
balance="$(bitcoin-cli -datadir=${data_dir} getbalance 2> /dev/null)"
NETWORK_INFO="$(bitcoin-cli -datadir=${data_dir} getnetworkinfo 2> /dev/null)"
networks="$(echo ${NETWORK_INFO} | jq -r '[.localaddresses[] | [.address, .port|tostring] | join(":")] | join("\t")')"
version="$(echo ${NETWORK_INFO} | jq -r '.subversion' | grep -Po '((\d+\.?){3})')"
MEMPOOL_INFO="$(bitcoin-cli -datadir=${data_dir} getmempoolinfo 2> /dev/null)"
mempool="$(echo ${MEMPOOL_INFO} | jq -r '.size')"
else
btc_line2="$(tput -T xterm setaf 1)NOT RUNNING"
fi
if [ ! -z "${btc_line1}" ] && [ ! -z ${chain} ]; then
btc_line1="${btc_line1} ${BOLD}(v${version}, ${chain})${RESET_STYLE}"
btc_line2="peers connected: ${BOLD}${peers_count}${RESET_STYLE}\t${SOME_COLOR}last block: ${BOLD}${blocks} (${percentage})${RESET_STYLE}"
btc_line3="balance: ${BOLD}${balance}${RESET_STYLE}\t${SOME_COLOR}mempool transactions: ${BOLD}${mempool}${RESET_STYLE}"
if [ ! -z "${networks}" ]; then
btc_line4="${networks}"
fi
else
ps cax | grep bitcoind >/dev/null 2>&1
if [ $? -eq 0 ]; then
btc_line2="${YELLOW}STARTING…"
else
btc_line2="${RED}NOT RUNNING"
fi
fi
echo "${GREEN}
.~~. .~~. resources used
'. \ ' ' / .' mem: ${BOLD}${ram_used} / ${ram_total}${RESET_STYLE}${GREEN}\t\t${swap_info}${RED}
.~ .~~~..~. ${GREEN}storage: ${BOLD}${sd_used} / ${sd_total}${RESET_STYLE}${GREEN}\t${external_info}${RED}
: .~.'~'.~. :
~ ( ) ( ) ~ ${SOME_COLOR}${btc_line1}${RED}
( : '~'.~.'~' : ) ${SOME_COLOR}${btc_line2}${RED}
~ .~ ( ) ~. ~ ${SOME_COLOR}${btc_line3}${RED}
( : '~' : )
'~ .~~~. ~' ${SOME_COLOR}accessible as${RED}
'~' ${SOME_COLOR}${BOLD}${btc_line4}${RESET_STYLE}${RED}
${RESET_STYLE}"
#!/bin/sh
# motd is generated with empty environment. This ensures that TERM is set, so that colors are displayed properly
# src: https://unix.stackexchange.com/a/417223/31104
export TERM="${TERM:-xterm-256color}"
BOLD=$(tput bold)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
# VIOLET=$(tput setaf 5)
# SOME_COLOR=$(tput setaf 6)
WHITE=$(tput setaf 7)
RESET_STYLE=$(tput sgr0)
ln_path=$(command -v lnd)
if [ ! -z ${ln_path} ]; then
ln_line1="lnd ⚡"
fi
ln_line1="${BLUE}lnd ${YELLOW}"
lnd_user=$(ps -eo user:10,command | grep "[l]nd" | awk '{ print $1 }' | grep -v root)
if [ ! -z "${lnd_user}" ]; then
data_dir="/home/${lnd_user}/.lnd/"
INFO=$(lncli --lnddir=${data_dir} getinfo)
version="$(echo ${INFO} | jq -r '.version')"
network="$(echo ${INFO} | jq -r '.chains[0]')"
testnet="$(echo ${INFO} | jq -r '.testnet')"
if [ "${network}" != "bitcoin" ]; then
network="${network}-"
else
network=""
fi
if [ "${testnet}" = true ]; then
network="${network}testnet"
else
network="${network}mainnet"
fi
blocks="$(echo ${INFO} | jq -r '.block_height')"
pubkey="$(echo ${INFO} | jq -r '.identity_pubkey')"
peers_count=$(echo ${INFO} | jq '.num_peers')
best_connstring=$(echo ${INFO} | jq -r '.uris[0]')
BALANCE=$(lncli --lnddir=${data_dir} walletbalance)
on_chain="$(echo ${BALANCE} | jq -r '.total_balance')"
on_chain=$(printf "%0.8f\n" ${on_chain})
CHANNELS=$(lncli --lnddir=${data_dir} listchannels)
ch_open="$(echo ${CHANNELS} | jq '.channels | length')"
channels_mine=$(echo ${CHANNELS} | jq '[.channels[].local_balance] | map(tonumber) | add | if . == null then 0 else . / 1e8 end')
max_out=$(echo ${CHANNELS} | jq '[.channels[].local_balance] | map(tonumber) | max | if . == null then 0 else . / 1e8 end')
if [ "${max_out}" != "0" ]; then
max_send=" (max: ${max_out})"
fi
channels_their=$(echo ${CHANNELS} | jq '[.channels[] | .remote_balance] | map(tonumber) | add | if . == null then 0 else . / 1e8 end')
max_in=$(echo ${CHANNELS} | jq '[.channels[] | .remote_balance] | map(tonumber) | max | if . == null then 0 else . / 1e8 end')
if [ "${max_in}" != "0" ]; then
max_receive=" (max: ${max_in})"
fi
PENDING_CHANNELS=$(lncli --lnddir=${data_dir} pendingchannels)
ch_pending="$(echo ${PENDING_CHANNELS} | jq '.pending_open_channels | length')"
ch_closing="$(echo ${PENDING_CHANNELS} | jq '[.pending_closing_channels, .pending_force_closing_channels, .waiting_close_channels] | map(length) | add')"
ln_line1="${ln_line1} ${BLUE}${BOLD}(${version}, ${network})${RESET_STYLE}"
ln_line2="id: ${BOLD}${pubkey}${RESET_STYLE}"
ln_line3="peers connected: ${BOLD}${peers_count}${RESET_STYLE}\t${BLUE}synced to block: ${BOLD}${blocks}${RESET_STYLE}"
ln_line4="channels: ${BOLD}${ch_all}${RESET_STYLE} (${YELLOW}${BOLD}pending: ${ch_pending}${RESET_STYLE}${BLUE}, ${GREEN}${BOLD}open: ${ch_open}${RESET_STYLE}${BLUE}, ${RED}${BOLD}closing: ${ch_closing}${RESET_STYLE}${BLUE})"
ln_line5="balance:"
ln_line6="on-chain: ${BOLD}${on_chain}${RESET_STYLE}\t${BLUE}LN spend : ${BOLD}${channels_mine}${max_send}${RESET_STYLE}"
ln_line7="\t\t\t\tLN receive: ${BOLD}${channels_their}${max_receive}${RESET_STYLE}"
ln_line12="best connstring:"
ln_line14="${BOLD}${best_connstring}"
else
ln_line2="${RED}NOT RUNNING"
fi
echo "${YELLOW} ____ ${BLUE}${ln_line1}${YELLOW}
/ / ${BLUE}${ln_line2}${YELLOW}
/ / ${BLUE}${ln_line3}${YELLOW}
/ /_ ${BLUE}${ln_line4}${YELLOW}
/_ / ${BLUE}${ln_line5}${YELLOW}
/ / ${BLUE}${ln_line6}${YELLOW}
/ /_ ${BLUE}${ln_line7}${YELLOW}
/ /
/ / ${BLUE}${ln_line9}${YELLOW}
/ / ${BLUE}${ln_line10}${YELLOW}
// ${BLUE}${ln_line11}${YELLOW}
/ ${BLUE}${ln_line12}${YELLOW}
${ln_line14}
${RESET_STYLE}"
#!/bin/sh
# motd is generated with empty environment. This ensures that TERM is set, so that colors are displayed properly
# src: https://unix.stackexchange.com/a/417223/31104
export TERM="${TERM:-xterm-256color}"
BOLD=$(tput bold)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
BLUE=$(tput setaf 4)
# VIOLET=$(tput setaf 5)
# SOME_COLOR=$(tput setaf 6)
WHITE=$(tput setaf 7)
RESET_STYLE=$(tput sgr0)
ln_path=$(command -v lightning-cli)
if [ -z ${ln_path} ]; then
exit 0
fi
ln_line1="${BLUE}c-lightning ${YELLOW}"
lightning_user=$(ps -eo user:10,command | grep "[l]ightningd" | awk '{ print $1 }')
if [ ! -z "${lightning_user}" ]; then
data_dir="/home/${lightning_user}/.lightning/"
# NOTE: This script is run by root, why sudo here is required is beyond me
INFO=$(sudo lightning-cli --lightning-dir=${data_dir} getinfo)
version="$(echo ${INFO} | jq -r '.version')"
network="$(echo ${INFO} | jq -r '.network')"
blocks="$(echo ${INFO} | jq -r '.blockheight')"
PEERS=$(sudo lightning-cli --lightning-dir=${data_dir} listpeers)
peers_count=$(echo ${PEERS} | jq '.peers | length')
all_channels=$(echo ${PEERS} | jq '[[.peers[] | select(.channels != null)][].channels[]]')
ch_all=$(echo ${all_channels} | jq 'length')
ch_all=${ch_all:-0}
ch_pending=$(echo ${all_channels} | jq '[.[] | select((.state == "OPENINGD") or (.state == "CHANNELD_AWAITING_LOCKIN"))] | length')
ch_pending=${ch_pending:-0}
ch_open=$(echo ${all_channels} | jq '[.[] | select(.state == "CHANNELD_NORMAL")] | length')
ch_open=${ch_open:-0}
ch_closing=$(echo ${all_channels} | jq '[.[] | select((.state == "CHANNELD_SHUTTING_DOWN") or (.state == "CLOSINGD_SIGEXCHANGE") or (.state == "CLOSINGD_COMPLETE") or (.state == "FUNDING_SPEND_SEEN"))] | length')
ch_closing=${ch_closing:-0}
FUNDS=$(sudo lightning-cli --lightning-dir=${data_dir} listfunds)
on_chain=$(echo ${FUNDS} | jq '[.outputs[].value] | add | if . == null then 0 else . / 1e8 end')
on_chain=$(printf "%0.8f\n" ${on_chain})
channels_mine=$(echo ${FUNDS} | jq '[.channels[].channel_sat] | add | if . == null then 0 else . / 1e8 end')
max_out=$(echo ${FUNDS} | jq '[.channels[].channel_sat] | max | if . == null then 0 else . / 1e8 end')
if [ "${max_out}" != "0" ]; then
max_send=" (max: ${max_out})"
fi
channels_their=$(echo ${FUNDS} | jq '[.channels[] | .channel_total_sat - .channel_sat] | add | if . == null then 0 else . / 1e8 end')
max_in=$(echo ${FUNDS} | jq '[.channels[] | .channel_total_sat - .channel_sat] | max | if . == null then 0 else . / 1e8 end')
if [ "${max_in}" != "0" ]; then
max_receive=" (max: ${max_in})"
fi
ipv4=$(echo ${INFO} | jq -r '.address[] | select(.type == "ipv4") | "\(.address):\(.port)"')
ipv6=$(echo ${INFO} | jq -r '.address[] | select(.type == "ipv6") | "\(.address):\(.port)"')
torv2=$(echo ${INFO} | jq -r '.address[] | select(.type == "torv2") | "\(.address):\(.port)"')
torv3=$(echo ${INFO} | jq -r '.address[] | select(.type == "torv3") | "\(.address):\(.port)"')
pubkey="$(echo ${INFO} | jq -r '.id')"
best_addr="${torv3:-${ipv6:-${torv2:-$ipv4}}}"
ln_line1="${ln_line1} ${BLUE}${BOLD}(${version}, ${network})${RESET_STYLE}"
ln_line2="id: ${BOLD}${pubkey}${RESET_STYLE}"
ln_line3="peers connected: ${BOLD}${peers_count}${RESET_STYLE}\t${BLUE}synced to block: ${BOLD}${blocks}${RESET_STYLE}"
ln_line4="channels: ${BOLD}${ch_all}${RESET_STYLE} (${YELLOW}${BOLD}pending: ${ch_pending}${RESET_STYLE}${BLUE}, ${GREEN}${BOLD}open: ${ch_open}${RESET_STYLE}${BLUE}, ${RED}${BOLD}closing: ${ch_closing}${RESET_STYLE}${BLUE})"
ln_line5="balance:"
ln_line6="on-chain: ${BOLD}${on_chain}${RESET_STYLE}\t${BLUE}LN spend : ${BOLD}${channels_mine}${max_send}${RESET_STYLE}"
ln_line7="\t\t\t\tLN receive: ${BOLD}${channels_their}${max_receive}${RESET_STYLE}"
ln_line9="accessible as"
# I'm sorry for the `if` hell below 😬
if [ ! -z ${ipv6} ]; then
ln_line9="${ln_line9} ipv6: ${BOLD}${ipv6}${RESET_STYLE}"
fi
if [ ! -z ${torv3} ]; then
ln_line10="torv3: ${BOLD}${torv3}${RESET_STYLE}"
fi
if [ ! -z ${torv2} ]; then
legacy_line="torv2: ${BOLD}${torv2}${RESET_STYLE}"
fi
if [ ! -z "${legacy_line}" ]; then
legacy_line="${legacy_line}\t"
fi
if [ ! -z ${ipv4} ]; then
legacy_line="${legacy_line}${BLUE}ipv4: ${BOLD}${ipv4}${RESET_STYLE}"
fi
if [ ! -z "${ln_line10}" ]; then
ln_line11="${legacy_line}"
else
ln_line10="${legacy_line}"
fi
ln_line12="best connstring:"
ln_line14="${BOLD}${pubkey}@${best_addr}"
else
ln_line2="${RED}NOT RUNNING"
fi
echo "${YELLOW} ____ ${BLUE}${ln_line1}${YELLOW}
/ / ${BLUE}${ln_line2}${YELLOW}
/ / ${BLUE}${ln_line3}${YELLOW}
/ /_ ${BLUE}${ln_line4}${YELLOW}
/_ / ${BLUE}${ln_line5}${YELLOW}
/ / ${BLUE}${ln_line6}${YELLOW}
/ /_ ${BLUE}${ln_line7}${YELLOW}
/ /
/ / ${BLUE}${ln_line9}${YELLOW}
/ / ${BLUE}${ln_line10}${YELLOW}
// ${BLUE}${ln_line11}${YELLOW}
/ ${BLUE}${ln_line12}${YELLOW}
${ln_line14}
${RESET_STYLE}"
#!/bin/sh
# motd is generated with empty environment. This ensures that TERM is set, so that colors are displayed properly
# src: https://unix.stackexchange.com/a/417223/31104
export TERM="${TERM:-xterm-256color}"
RED=$(tput setaf 1)
# GREEN=$(tput setaf 2)
# YELLOW=$(tput setaf 3)
# BLUE=$(tput setaf 4)
# VIOLET=$(tput setaf 5)
# SOME_COLOR=$(tput setaf 6)
# WHITE=$(tput setaf 7)
RESET_STYLE=$(tput sgr0)
ram_swap=$(free -m | grep Swap | awk '{ print $3 }')
if [ "${ram_swap}" -eq "0" ]; then
exit 0
fi
echo "${RED}${ram_swap}M of SWAP is being used! It can significanly shorten the life span of your microSD card.
Disable SWAP with:${RESET_STYLE}
sudo swapoff --all && sudo apt-get remove dphys-swapfile && sudo apt-get autoremove
${RED}
Or disable this warning with:${RESET_STYLE}
sudo chmod -x /etc/update-motd.d/30-swap-warning"
#!/bin/sh
if [ `whoami` != "root" ]; then
echo "Run as sudo to update your motd."
exit 1
fi
MOTD_DIR=/etc/update-motd.d/
mkdir -p "${MOTD_DIR}"
wget -qN 'https://gist.githubusercontent.com/meeDamian/0006c766340e0afd16936b13a0c7dbd8/raw/8b9a821feccd3c95d62e7cc937146db469fadc8f/10-uname' -P "${MOTD_DIR}"
wget -qN 'https://gist.githubusercontent.com/meeDamian/0006c766340e0afd16936b13a0c7dbd8/raw/8b9a821feccd3c95d62e7cc937146db469fadc8f/20-raspberry-bitcoin' -P "${MOTD_DIR}"
wget -qN 'https://gist.githubusercontent.com/meeDamian/0006c766340e0afd16936b13a0c7dbd8/raw/3552b9a418d81d12ae004850cbcb4578a43fdfca/23-raspberry-lnd' -P "${MOTD_DIR}"
wget -qN 'https://gist.githubusercontent.com/meeDamian/0006c766340e0afd16936b13a0c7dbd8/raw/3552b9a418d81d12ae004850cbcb4578a43fdfca/26-raspberry-lightning' -P "${MOTD_DIR}"
wget -qN 'https://gist.githubusercontent.com/meeDamian/0006c766340e0afd16936b13a0c7dbd8/raw/8b9a821feccd3c95d62e7cc937146db469fadc8f/30-swap-warning' -P ${MOTD_DIR}
chmod +x "${MOTD_DIR}10-uname" "${MOTD_DIR}20-raspberry-bitcoin" "${MOTD_DIR}23-raspberry-lnd" "${MOTD_DIR}26-raspberry-lightning" "${MOTD_DIR}30-swap-warning"
run-parts --lsbsysinit /etc/update-motd.d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.