Skip to content

Instantly share code, notes, and snippets.

@TauPan
Last active April 17, 2018 14:38
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 TauPan/3cb72c76312ee2f0f45ecc071340f4b0 to your computer and use it in GitHub Desktop.
Save TauPan/3cb72c76312ee2f0f45ecc071340f4b0 to your computer and use it in GitHub Desktop.
Some tmux scripts
#!/usr/bin/env bash
update_command='reset; while pgrep -f apt-get; do echo -n "O"; sleep 60; done;aptitude update; aptitude -y -d safe-upgrade --full-resolver; aptitude safe-upgrade --full-resolver; purge-old-kernels ; cat /var/run/reboot-required*'
reachable () {
netcat -w 1 -z $*
}
hosts='dirk thomas abrasax orion'
sudo wakeonlan -f /etc/wakeonlan/abrasax.wol
echo
until reachable abrasax 22
do
sleep 0.1
echo -n .
done
echo
ssh root@abrasax systemctl restart openvpn
for host in $hosts
do
if reachable $host 22
then
tmux-ssh -n root@$host
sleep 10
tmux send-keys -t root@$host "$update_command" Enter
fi
done
#!/usr/bin/env bash
name="$1"
shift
args="$*"
tmux-select-window-globally "$name" || tmux -2 new-window -n "$name" "$args"
# exit status 1 if window does not exist afterwards
sleep 0.1
tmux select-window -t "$name"
#!/usr/bin/env bash
name="$1"
switched="false"
current_session=$(tmux display-message -p "#{session_name}")
tmux list-panes -a -F 'session_name="#{session_name}" window_index="#{window_index}" pane_index="#{pane_index}" window_name="#{window_name}" 'pane_title="'#{pane_title}'" | sort | {
while read line
do
eval $line
if [ "$window_name" = "$name" ]
then
if [ "$session_name" != "$current_session" ]
then
tmux -2 switch-client -t $session_name
fi
tmux -2 select-window -t $session_name:$window_name
tmux select-pane -t $session_name:$window_index.${pane_index}
switched="true"
break
fi
done
if [ "$switched" = "false" ]
then
exit 1
else
exit 0
fi
}
#!/usr/bin/env bash
# set -x
# see http://mywiki.wooledge.org/BashFAQ/035#getopts
#
# switches:
#
# -r allow rename of window afterwards
#
# -n open a new window, if appropriately named window is not present,
# otherwise use the current window
#
# -f force: don't check if a named window already exists... if it
# does, it should have a running shell (mostly for compatibility with
# tmuxinator)
allowrename=''
newwindow=''
force=''
OPTIND=1
# Resetting OPTIND is necessary if getopts was used previously in the script.
# It is a good idea to make OPTIND local if you process options in a function.
while getopts rnf opt;
do
case $opt in
r)
allowrename="true"
;;
n)
newwindow="true"
;;
f)
force="true"
;;
esac
done
shift "$((OPTIND-1))" # Discard the options and sentinel --
host="$1"
windowname=$(echo $host | sed -re 's/^([^\.:]+).*/\1/g')
shift
args="$*"
if [ -z "$force" ]
then
tmux-select-window-globally $windowname && exit 0
fi
if [ -n "$newwindow" ]
then
tmux new-window -n "$windowname"
else
tmux set-window-option allow-rename on
tmux rename-window $windowname
fi
if [ -n "$allowrename" ]
then
tmux set-window-option allow-rename on
tmux set-window-option automatic-rename on
else
tmux set-window-option allow-rename off
tmux set-window-option automatic-rename off
fi
# This whole batch of operations must run in the background, so the
# shell is available to receive input via tmux send-keys
(
tmux send-keys -t $windowname " exec ssh -t $host" Enter
tmux send-keys -t $windowname "exec bash -c 'tmux start-server; tmux -2 attach-session -d -t $TMUX_MAIN_SESSION || tmux -2 new-session -s $TMUX_MAIN_SESSION $args \; set-window-option allow-rename on \; set-window-option automatic-rename on'" Enter
tmux send-keys -t $windowname C-a : "source ~/.tmux.conf" Enter
tmux send-keys -t $windowname "setopt ignoreeof || export IGNOREEOF=10 " Enter
) &
@TauPan
Copy link
Author

TauPan commented Nov 21, 2017

11:36 <cymen> TauPan: got any good examples? :)
12:12 <TauPan> cymen: Not sure if it's good, but here is an example: https://gist.github.com/TauPan/3cb72c76312ee2f0f45ecc071340f4b0 (I'm open for suggestions, of course.)
12:13 <TauPan> I use this to update my boxes at home, all of which run a debian derivate and one has to be woken up with wakeonlan.
12:16 <TauPan> (tmux-select-window-globally is potentially vulnerable if a malicious entity manages to control your session/window/pane names or titles)
12:17 <TauPan> (e.g. if I had the topic of an irc channel in the pane title and somebody would place a shell injection in there, I'd be toast ;)

@TauPan
Copy link
Author

TauPan commented Nov 21, 2017

TMUX_MAIN_SESSION is set in my shell profile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment