From bd10cd17c2d2e29c72ee8ad248fecc4df6a7711a Mon Sep 17 00:00:00 2001 From: waldek Date: Mon, 17 Jan 2022 11:33:57 +0100 Subject: [PATCH] initial commit --- install.sh | 47 ++++++++++++++++++++++++++++++++++++++++++++ readme.md | 16 +++++++++++++++ tmux_broadcast.sh | 34 ++++++++++++++++++++++++++++++++ tmux_viewer.sh | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100755 install.sh create mode 100644 readme.md create mode 100755 tmux_broadcast.sh create mode 100755 tmux_viewer.sh diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..cb20607 --- /dev/null +++ b/install.sh @@ -0,0 +1,47 @@ +#!/usr/bin/bash + +if [ "$EUID" -ne 0 ] + then echo "Please run as root" + exit 1 +fi + +VIEWER="tmuxviewer" +SSHD_CONFIG_FOLDER="/etc/ssh/sshd_config.d/" +CONFIG_FILE="$VIEWER.config" +CONFIG_FILE_PATH="$SSHD_CONFIG_FOLDER$CONFIG_FILE" +BIN_FOLDER="/usr/bin/" +VIEWER_BIN="tmux_viewer.sh" +BROADCAST_BIN="tmux_broadcast.sh" + +function install_tmuxviewer () { + echo "adding $VIEWER_BIN and $BROADCAST_BIN to $BIN_FOLDER" + sudo cp $VIEWER_BIN $BIN_FOLDER$VIEWER_BIN + sudo cp $BROADCAST_BIN $BIN_FOLDER$BROADCAST_BIN + echo "adding user $VIEWER" + sudo useradd --home /tmp/$VIEWER --shell /usr/bin/tmux_viewer.sh $VIEWER + echo "unsetting password for $VIEWER" + sudo passwd -d $VIEWER 1> /dev/null + sudo mkdir -p $SSHD_CONFIG_FOLDER + sudo echo "Match User $VIEWER\n\tPermitEmptyPassword yes" > $CONFIG_FILE_PATH +} + +function uninstall_tmuxviewer () { + echo "removing user $VIEWER" + sudo userdel $VIEWER + sudo rm $CONFIG_FILE_PATH + sudo rm $BIN_FOLDER$VIEWER_BIN + sudo rm $BIN_FOLDER$BROADCAST_BIN +} + +case $1 in + install) + uninstall_tmuxviewer 2> /dev/null + install_tmuxviewer + ;; + uninstall) + uninstall_tmuxviewer + ;; + *) + echo "usage (as root): $0 [install|uninstall]" + ;; +esac diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..a0c8bce --- /dev/null +++ b/readme.md @@ -0,0 +1,16 @@ +# Tmux Viewer + +Simple script combo to allow people to see what you're doing in a specific tmux session. + +## Dependencies + +```sudo apt install tmux``` + +## Install + +```sudo bash install.sh install``` + +## Uninstall + +```sudo bash install.sh uninstall``` + diff --git a/tmux_broadcast.sh b/tmux_broadcast.sh new file mode 100755 index 0000000..d18d006 --- /dev/null +++ b/tmux_broadcast.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +USER="$(whoami)" +VIEWER="tmuxviewer" +SOCKET="/tmp/$VIEWER/$USER" + +if groups $USER | grep -q "sudo"; then + mkdir -p /tmp/$VIEWER/ + sudo chown $VIEWER:$VIEWER "/tmp/$VIEWER" + sudo chmod 777 /tmp/$VIEWER/ 2> /dev/null + sudo chown $USER:$VIEWER $SOCKET + sudo chmod 770 $SOCKET +else + echo "sorry, you're not allowed to broadcast.." + exit 1 +fi + + +tmux -S $SOCKET has-session -t shared 2> /dev/null + +if [ $? != 0 ]; then + if groups $USER | grep -q "sudo"; then + echo "no shared session running, creating one..." + tmux -S $SOCKET new-session -A -s shared -d + sudo chown $USER:$VIEWER $SOCKET + chmod 770 $SOCKET + else + echo "you are not allowed to take over..." + exit 1 + fi +fi + +echo "attaching to $SOCKET" +tmux -S $SOCKET attach -t shared diff --git a/tmux_viewer.sh b/tmux_viewer.sh new file mode 100755 index 0000000..344dbad --- /dev/null +++ b/tmux_viewer.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +VIEWER="tmuxviewer" +SOCKETS="/tmp/$VIEWER/" +SESSIONS="$(ls $SOCKETS)" +HOSTNAME="$(hostname).$(grep domain /etc/resolv.conf | cut -d " " -f 2)" + +function select_session () { + if [ $(echo $SESSIONS | wc -w) == 1 ]; then + SOCKET=$SOCKETS$SESSIONS + echo $SOCKET + else + echo "please choose a session to view: (or exit to quit)" + select session in $(echo $SESSIONS); do + if [ -n "$session" ]; then + SOCKET=$SOCKETS$session + echo "connection to $session at $SOCKET" + view_session + else + case $REPLY in + exit) + echo "will quit now" + break;; + *) + echo "refreshing list..." + select_session + ;; + esac + fi + + done + fi +} + +function view_session () { + clear + while : + do + tmux -S $SOCKET attach -t shared -r 2>/dev/null + clear + echo "$HOSTNAME is not broadcasting at $SOCKET..." + sleep 1 + done +} + +function main () { + select_session +} + +main