-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxssh
More file actions
executable file
·131 lines (125 loc) · 3.46 KB
/
Copy pathxssh
File metadata and controls
executable file
·131 lines (125 loc) · 3.46 KB
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
#!/bin/bash
source ~/command/common
ip="localhost"
user=root
port=22
password=admin
default_remote_root=/root/
_oper=0
_name=""
_localpath="./"
_remote_root=""
_remotepath=""
function setclipboard() {
echo
}
function xssh_enter() {
if type expect >/dev/null 2>&1; then
expect -c "
set timeout 30000;
spawn ssh ${user}@${ip} -p ${port};
expect {
\"*password:\" {send \"${password}\r\"; exp_continue}
\"*#\" {send \" cd ${_remote_root}\r\"}
}
interact
"
else
echo "密码: ${password}"
echo "命令: cd ${_remote_root}"
echo ssh ${user}@${ip} -p ${port}
setclipboard "${password}"
ssh ${user}@${ip} -p ${port}
fi
}
function xssh_push() {
[ -z "${_localpath}" ]&&return
if type expect >/dev/null 2>&1; then
expect -c "
set timeout 30000;
spawn scp -P ${port} -r "${_localpath}" "${user}@${ip}:${_remotepath}"
expect {
\"*password:\" {send \"${password}\r\"}
}
interact
"
else
echo "密码: ${password}"
echo scp -P ${port} -r "${_localpath}" "${user}@${ip}:${_remotepath}"
setclipboard "${password}"
scp -P ${port} -r "${_localpath}" "${user}@${ip}:${_remotepath}"
fi
}
function xssh_pull() {
[ -z "${_localpath}" ]&&return
if type expect >/dev/null 2>&1; then
expect -c "
set timeout 30000;
spawn scp -P ${port} -r "${user}@${ip}:${_remotepath}" "${_localpath}"
expect {
\"*password:\" {send \"${password}\r\"}
}
interact
"
else
echo "密码: ${password}"
echo scp -P ${port} -r "${user}@${ip}:${_remotepath}" "${_localpath}"
setclipboard "${password}"
scp -P ${port} -r "${user}@${ip}:${_remotepath}" "${_localpath}"
fi
}
function show_help() {
local -a list
list="xssh"
list=("${list[@]}" "Usage: xssh [-s|-l|-h]")
list=("${list[@]}" "xssh: login ssh")
list=("${list[@]}" "xssh -s [local:remote]: push local file/dir to ssh server")
list=("${list[@]}" "xssh -l [remote:local]: pull remote file/dir from ssh server to local")
list=("${list[@]}" "xssh -c [config file]: set config file")
list=("${list[@]}" "xssh -i: ip")
list=("${list[@]}" "xssh -p: port")
list=("${list[@]}" "xssh -u: user")
list=("${list[@]}" "xssh -w: password")
list=("${list[@]}" "xssh -h: show help")
__msgbox "${list[@]}"
}
function main() {
while getopts :s:l:c:p:w:u:mti:r:h opt;do
case ${opt} in
s)_oper=1;_localpath="${OPTARG%%:*}";_remotepath="${OPTARG##*:}";;
l)_oper=2;_remotepath="${OPTARG%%:*}";_localpath="${OPTARG##*:}";;
m)_config_file="${HOME}/account/mac_config";;
t)_config_file="${HOME}/account/shen_ou_config";;
c)_config_file="$OPTARG";;
p)port="$OPTARG";;
i)ip="$OPTARG";;
u)user="$OPTARG";;
w)password="$OPTARG";;
r)_remote_root="$OPTARG";;
*)show_help;exit;;
esac
done
if [ -n "${_config_file}" ];then
ip=`sed '/^public_ip=/!d;s/.*=//' ${_config_file}`
password=`sed '/^password=/!d;s/.*=//' ${_config_file}`
port=`sed '/^port=/!d;s/.*=//' ${_config_file}`
user=`sed '/^user=/!d;s/.*=//' ${_config_file}`
_remote_root=`sed '/^root=/!d;s/.*=//' ${_config_file}`
fi
[ -z "${port}" ]&&port=22
[ -z "${user}" ]&&user=root
[ -z "${_remote_root}" ]&&_remote_root="${default_remote_root}"
[ "${_remote_root%%/}" = "$_remote_root" ]&&_remote_root="${_remote_root}/"
if ! [ "${_localpath:0:1}" = "/" -o "${_localpath:0:1}" = "~" ];then
_localpath="./${_localpath}"
fi
if ! [ "${_remotepath:0:1}" = "/" -o "${_remotepath:0:1}" = "~" ];then
_remotepath="${_remote_root}${_remotepath}"
fi
case ${_oper} in
0)xssh_enter;;
1)xssh_push;;
2)xssh_pull;;
esac
}
main "$@"