-
Notifications
You must be signed in to change notification settings - Fork 2
/
ssh-handler.sh
executable file
·63 lines (49 loc) · 1.52 KB
/
ssh-handler.sh
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
#!/bin/bash
# inspired by this post
# https://soultrace.net/open-ssh-urls-in-chrome-and-firefox/
#d=${1#ssh://}
#gnome-terminal -e "ssh $d" &
#terminator -e "ssh $d" &
# xdg-open ssh://user:pass@host:port
parseURL(){
[ "$1" == "-i" ] && flg_cur=1 && shift 1
local s=${1##ssh://}
local a=${s%%@*}
[ "$a" == "$s" ] && a=''
local h=${s##*@}
local user=${a%%:*}
local pass=${a##*:}
[ "$pass" == "$a" ] && pass=''
local host=${h%%:*}
local port=${h##*:}
port=${port%%/}
[ "$port" == "$h" ] && port=''
decodeURIComponent(){
echo "$1" | sed -e 's/%\([0-9A-Fa-f][0-9A-Fa-f]\)/\\\x\1/g' | xargs -0 printf "%b";
}
pass=`decodeURIComponent "$pass"`
local cmd="$host"
[ -n "$user" ] && cmd="$user@$cmd"
[ -n "$port" ] && cmd="$cmd -p $port"
cmd="ssh $cmd"
cmd="$cmd -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
#[ -n "$pass" ] && cmd="sshpass -p '$pass' $cmd"
#[ -n "$pass" ] && cmd="sshpass -p '$pass' $cmd; [ \$? == 5 ] && echo login fail && $cmd"
[ -n "$pass" ] && cmd="bash -c \"TERM=xterm-color sshpass -p '$pass' $cmd; [ \\\$? == 5 ] && echo 'incorrect password' && $cmd\""
echo "url: $1"
echo "cmd: $cmd"
echo
[ -n "$flg_cur" ] && { eval "$cmd"; return; }
#$cmd
#terminator -e "$cmd" &
gnome-terminal -e "$cmd"
#echo "ret: $?"
}
## test
# parseURL 'ssh://host'
# parseURL 'ssh://host:port'
# parseURL 'ssh://aaa@host:port'
# parseURL 'ssh://aaa:123@host:port'
# parseURL 'ssh://aaa:%25aa%25bb!%40%23%24%25%5E%26*()%3F%20%20%5C%2F@host:port'
# parseURL 'ssh://chen:abc@10.1.1.30:1234'
parseURL "$@"