-
Notifications
You must be signed in to change notification settings - Fork 8
/
remote.sh
executable file
·191 lines (179 loc) · 6.01 KB
/
remote.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/sh
# \
exec expect -- "$0" ${1+"$@"}
###################################################
# 用法: 脚本名 -i IP.地址 -p 密码 -t 超时时间 -m 命令模式 -c 具体命令
###################################################
# 功能描述:
# 1:在远端的服务器上执行命令(使用-m ssh-cmd);
# 2:把本地文件拷贝至远端服务器(使用-m scp-out);
# 备注:如果远端没有scp命令,需要使用cat+ssh
# 把本地文件拷贝至远端服务器
# 3:把远端文件拷贝至本地(使用-m scp-in);
# 4:对于建立互信的环境可以不使用-p参数设置密码;
# 5:默认的用户名为root
# 6:默认的端口号为22
# 7:默认的脚本超时时间为120秒
# 8:执行结果以标准输出的形式输出
# 9:密码或用户名错误返回128
# 10:超时返回129
###################################################
#设置默认值
set port 22
set user "root"
set timeout 120
set password ""
set host ""
set mode ""
set command ""
set src ""
set dst ""
###############################################
# 显示帮助信息
###############################################
proc help {} {
global argv0
send_user "usage: $argv0\n"
send_user " -i <ip> Host or IP\n"
send_user " -P <port> Port. Default = 22\n"
send_user " -u <user> UserName. Default = root\n"
send_user " -p <password> Password.\n"
send_user " -t <timeout> Timeout. Default = 120\n"
send_user " -m <mode> Mode. include: ssh-cmd, scp-out, scp-in\n"
send_user " -c <command> Ssh Command\n"
send_user " -s <src> Scp Source File\n"
send_user " -d <dst> Scp Destination File\n"
send_user " -a <aes-file> Use aes encrypt passwd\n"
send_user " -v Version\n"
send_user " -h Help\n"
send_user "Sample:\n"
send_user "$argv0 -i 10.88.10.11 -p pass -t 5 -m ssh-cmd -c ifconfig\n"
send_user "$argv0 -i 10.88.10.11 -p pass -m scp-out -s /etc/passwd -d /tmp/passwd\n"
}
###############################################
# 输出错误日志
###############################################
proc errlog {errmsg h code} {
global host
send_user "\nError: $errmsg on $host (${code}) \n"
if {[string compare "$h" "yes"] == 0} {
help
}
exit $code
}
#参数个数不能为0
if {[llength $argv] == 0} {
errlog "argv is null" "yes" "1"
}
#参数解析
while {[llength $argv]>0} {
set flag [lindex $argv 0]
switch -- $flag "-i" {
set host [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-P" {
set port [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-u" {
set user [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-p" {
set password [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-t" {
set timeout [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-m" {
set mode [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-c" {
set command [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-s" {
set src [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-d" {
set dst [lindex $argv 1]
set argv [lrange $argv 2 end]
} "-a" {
set password [ exec openssl enc -aes-256-cbc -salt -a -d -k "$key" -in [lindex $argv 1] 2> /dev/null ]
set argv [lrange $argv 2 end]
} "-v" {
send_user "Ver: 1.0.0.0\n"
exit 0
} "-h" {
help
exit 0
} default {
set user [lindex $argv 0]
set argv [lrange $argv 1 end]
break
}
}
#主机名或IP为空
if {"$host" == ""} {
errlog "host is null" "yes" "1"
}
#执行命令
if {[string compare "$mode" "ssh-cmd"] == 0} {
if {"$command" == ""} {
errlog "command is null" "yes" "1"
}
spawn ssh -oServerAliveInterval=60 -oStrictHostKeyChecking=no -oVerifyHostKeyDNS=yes -oUserKnownHostsFile=/dev/null -p $port $user@$host "$command"
} elseif {[string compare "$mode" "scp-out"] == 0} {
if {"$src" == "" || "$dst" == ""} {
errlog "src or dst is null" "yes" "1"
}
spawn scp -r -oServerAliveInterval=60 -oStrictHostKeyChecking=no -oVerifyHostKeyDNS=yes -oUserKnownHostsFile=/dev/null -P $port $src $user@$host:$dst
} elseif {[string compare "$mode" "scp-in"] == 0} {
if {"$src" == "" || "$dst" == ""} {
errlog "src or dst is null" "yes" "1"
}
spawn scp -r -oServerAliveInterval=60 -oStrictHostKeyChecking=no -oVerifyHostKeyDNS=yes -oUserKnownHostsFile=/dev/null -P $port $user@$host:$src $dst
} else {
errlog "mode($mode) invalid" "yes" "1"
}
#命令执行结果
expect {
-nocase -re "please try again" {
errlog "Bad Password/UserName, Or Account locked" "no" "128"
}
-nocase -re "password" {
send "$password\r"
exp_continue
}
timeout {
errlog "Executing timeout" "no" "129"
}
}
#获取命令执行结果
catch wait result
set ret [lindex $result 3]
if { $ret != 0 } {
#如有远端没有scp命令的话,scp会失败的,此时需要使用cat+ssh的方法拷贝数据
#暂不考虑从远端拷贝数据至本地,且远端无scp的场景
if {$ret == 1 && [string compare "$mode" "scp-out"] == 0} {
spawn /bin/sh -c "cat $src | ssh -oServerAliveInterval=60 -oStrictHostKeyChecking=no -oVerifyHostKeyDNS=yes -oUserKnownHostsFile=/dev/null -p $port $user@$host 'cat > $dst'"
#命令执行结果
expect {
-nocase -re "please try again" {
errlog "Bad Password/UserName, Or Account locked" "no" "128"
}
-nocase -re "password" {
send "$password\r"
exp_continue
}
timeout {
errlog "Executing timeout" "no" "129"
}
}
#获取命令执行结果
catch wait result
set ret [lindex $result 3]
if { $ret == 0 } {
exit 0
}
}
errlog "Execute failed" "no" "$ret"
}
exit $ret