Last active
April 17, 2018 14:38
-
-
Save TauPan/3cb72c76312ee2f0f45ecc071340f4b0 to your computer and use it in GitHub Desktop.
Some tmux scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
) & |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TMUX_MAIN_SESSION
is set in my shell profile