-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmosh
executable file
·133 lines (112 loc) · 3.21 KB
/
mosh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
###############################################################
## Mosh 2: Alternative frontend for Mosh ##
## ##
## Developed by Varun Agrawal (Varun@VarunAgw.com) ##
###############################################################
show_help() {
echo -e "Alternative frontend for MOSH by Varun Agrawal (www.VarunAgw.com)"
echo -e "Usage: $0 [user@]host [mosh-options] [ssh-options]"
echo -e ""
echo -e "[mosh-options]"
echo -e "-pd\t--predict=adaptive local echo for slower links [default]"
echo -e "-pa\t--predict=always use local echo even on fast links"
echo -e "-pn\t--predict=never never use local echo"
echo -e "-pe\t--predict=experimental aggressively echo even when incorrect"
echo -e ""
echo -e "\t--ip=xxx.xxx.xxx.xxx tell server IP to MOSH if it fails to find it"
echo -e "\t--get return session connection command instead of connecting"
echo -e ""
echo -e "Example: $0 ec2 -pe -v -p 2020"
}
if [ "--h" == "$*" ] || [ "--help" == "$*" ] || [ -z "$*" ] ; then
show_help
exit
fi
error() {
echo -e "MOSH: $*" >&2
}
# Set default value of arguments
ARG_PREDICT="adaptive"
ARG_IP=""
ARG_SSH=""
ARG_SERVER=""
ARG_GET=false
for i in "$@"
do
case $i in
-pd|--predict=adaptive)
ARG_PREDICT="adaptive"
;;
-pa|--predict=always)
ARG_PREDICT="always"
;;
-pn|--predict=never)
ARG_PREDICT="never"
;;
-pe|--predict=experimental)
ARG_PREDICT="experimental"
;;
--ip=*)
ARG_IP="${i#*=}"
;;
--get)
ARG_GET=true
;;
*)
ARG_SSH="$ARG_SSH ${i#*=}"
;;
esac
done
if [ -n "$ARG_SSH" ]; then
ARG_SSH_ARRAY=($ARG_SSH)
ARG_SERVER=${ARG_SSH_ARRAY[0]}
else
error "User/Hostname is missing. Exiting!"
exit 1
fi
if [ -n "$ARG_IP" ]; then
IP="$ARG_IP"
else
if [[ "$ARG_SERVER" =~ .+@(.+) ]]; then
HOSTNAME="${BASH_REMATCH[1]}"
else
config=`cat ~/.ssh/config`
# grep -Po don't support ^. Prepending new line for Host on first line
HOSTNAME=`echo $'\n'"$config" | grep -Pizo "(?s)\n\s*Host.*?\s$ARG_SERVER(\s|\n).*?\n\s*HostName .*?\n"`
HOSTNAME=`echo "$HOSTNAME" | tail -n 1`
HOSTNAME=`echo "$HOSTNAME" | sed -r "s/.*HostName\s*//i" | sed -r "s/\s+//i"`
fi
if [ -z "$HOSTNAME" ]; then
error "Cannot find hostname. Please run with --ip=xxx.xxx.xxx.xxx. Exiting!"
exit 1
fi
if [[ $HOSTNAME =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
IP="$HOSTNAME"
else
IP=`dig +short $HOSTNAME`
fi
if [ -z "$IP" ]; then
error "Cannot find IP address of server. Please run with --ip=xxx.xxx.xxx.xxx. Exiting!"
exit 1
fi
fi
SSH_COMMAND="ssh -t $ARG_SSH mosh-server new"
SSH_OUTPUT=`$SSH_COMMAND`
SSH_OUTPUT=`echo $SSH_OUTPUT | sed -r "s/\r|\n|//g" | sed -r "s/(^ *)|( *$)//g"`
if ! [[ "$SSH_OUTPUT" == MOSH\ CONNECT\ * ]]; then
error ""
error "SSH Failed to server"
if [ -n "$SSH_OUTPUT" ]; then
error "$SSH_OUTPUT"
fi
exit 1
fi
MOSH_CONNECTION=($SSH_OUTPUT)
MOSH_PORT=${MOSH_CONNECTION[2]}
MOSH_KEY=${MOSH_CONNECTION[3]}
if [ true == $ARG_GET ]; then
echo "MOSH_PREDICTION_DISPLAY=$ARG_PREDICT MOSH_KEY=$MOSH_KEY mosh-client $IP $MOSH_PORT"
else
MOSH_PREDICTION_DISPLAY=$ARG_PREDICT MOSH_KEY=$MOSH_KEY mosh-client $IP $MOSH_PORT
fi