Created
November 23, 2014 16:26
-
-
Save eri451/fa1ec2d40d8c89a1c1ee to your computer and use it in GitHub Desktop.
remote tmux init
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
# Remote | |
# in case the remote has no session called my-session | |
RemoteCommand=$(cat << _EOT | |
" | |
tmux new -d -s my-session -n e-mail \"mutt\"; | |
tmux new-window -d -n irc \"weechat-curses\"; | |
tmux new-window -d; | |
tmux new-window -d; | |
tmux new-window -d; | |
tmux attach -t my-session:2; | |
" | |
_EOT | |
) | |
# what really is to do | |
remote_tmux(){ | |
local tld=$1 | |
echo -e "[ using '${tld}' as top level domain ]\n" | |
ssh remote.${tld} tmux has-session -t my-session &> /dev/null | |
if [[ $? == 0 ]] | |
then | |
ssh -t remote.${tld} tmux attach-session -t my-session | |
else | |
ssh -t remote.${tld} bash -c ${RemoteCommand} | |
fi | |
unset tld | |
} | |
# checks if alternative tld can be used | |
remote_alttld(){ | |
echo -e "[ looking for alternative nameserver ]\n" | |
ping -c 2 192.168.0.2 | |
if [[ $? != 0 ]] | |
then | |
echo -e "\n[ not available ]\n" | |
return 1 | |
fi | |
echo -e "\n[ checking DNS for 'remote.tld' ]" | |
dig remote.tld @192.168.0.2 | tee - | grep "192\.168\.0\.1" &> /dev/null | |
if [[ $? != 0 ]] | |
then | |
echo -e "\n[ seems not to be the alternative network ]\n" | |
return 1 | |
fi | |
return 0 | |
} | |
# default checks | |
remote_default(){ | |
echo -e "[ looking for a network connection ]\n" | |
local gateway=$(ip r | grep default | cut -d" " -f3) | |
if [[ ${gateway} = "" ]] | |
then | |
echo -e "[ you not seem to be connected to a network ]\n" | |
return 1 | |
fi | |
ping -c 2 ${gateway} | |
if [[ $? != 0 ]] | |
then | |
echo -e "\n[ the gateway seems not to be reachable ]\n" | |
return 1 | |
fi | |
dig ANY remote.com | tee - | grep "\(10\.0\.0\.1\|2001:abcd:ef01:234::5\)" &> /dev/null | |
if [[ $? != 0 ]] | |
then | |
echo -e "[ it seems the remote cannot be resolved here ]\n" | |
return 1 | |
fi | |
echo -e "[ having network ]\n" | |
unset gateway | |
return 0 | |
} | |
remote(){ | |
remote_default | |
if [[ $? != 0 ]] | |
then | |
exit 1 | |
fi | |
remote_alttld | |
if [[ $? != 0 ]] | |
then | |
remote_tmux com | |
exit 0 | |
else | |
remote_tmux local | |
exit 0 | |
fi | |
} | |
## Main | |
case "$1" in | |
"remote" ) remote;; | |
"") | |
echo -e "configured sessions are:\n remote"; | |
;; | |
*) | |
echo -e "usage: muxxer [session-name]" >2; | |
echo -e "configured sessions are:\n remote"; | |
;; | |
esac | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment