-
Notifications
You must be signed in to change notification settings - Fork 8
/
exec_remote
executable file
·147 lines (135 loc) · 4.01 KB
/
exec_remote
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
#!/bin/bash
source_functions()
{
. scripts/log_print
}
###################################################
#
# 在远端服务器运行命令
#
###################################################
function exec_cmd_r()
{
local g_port=$1
local g_user_name=$4
local g_timeout=$2
local host=$3 #远端主机名
local passwd=$5 #主机密码
local command=$6 #运行的命令
local ret=0 #返回值
local cmd_file="/tmp/exec_cmd_r.$$.$(date +%s%N)"
source_functions
#入参判断
if [ -z "$host" ]; then
error "host is null"
return 1
fi
if [ -z "$command" ]; then
error "command is null"
return 1
fi
ping -c 3 -i 0.2 -W 3 $host &> /dev/null
if [ $? != 0 ];then
log ERROR "Ping $host failed"
exit 1
fi
#scripts/remote.sh -i 192.168.38.141 -p root -t 5 -m ssh-cmd -c "ls ~"
bash scripts/remote.sh -i "$host" -P "$g_port" -u "$g_user_name" -p "$passwd" -t "$g_timeout" -m "ssh-cmd" -c "$command" > ${cmd_file}
ret=$?
cat "${cmd_file}" | grep -v "^spawn ssh" | grep -v "^Warning" | grep -v "^Password:" | grep -v "^Authorized" | grep -v "password:" | grep -v "^Permission denied"
rm "${cmd_file}" -f
if [ $ret -ne 0 ]; then
log ERROR "Run ${command} in ${host} failed, ret code:${ret}"
fi
return $ret
}
###################################################
#
# 把文件拷贝至远端服务器
#
###################################################
function cp_file_to_r()
{
local g_port=$1
local g_user_name=$4
local g_timeout=$2
local host=$3 #远端主机名
local passwd=$5 #主机密码
local src=$6 #源文件
local dst=$7 #目标文件
local ret=0 #返回值
source_functions
#入参判断
if [ -z "$host" ]; then
log ERROR "host is null"
return 1
fi
if [ -z "$src" ]; then
log ERROR "src is null"
return 1
fi
if [ -z "$dst" ]; then
log ERROR "dst is null"
return 1
fi
ping -c 3 -i 0.2 -W 3 $host &> /dev/null
if [ $? != 0 ];then
log ERROR "Ping $host failed"
return 1
fi
#./remote.sh -i 192.168.38.141 -p root -t 5 -m ssh-cmd -c "ls ~"
bash scripts/remote.sh -i "$host" -P "$g_port" -u "$g_user_name" -p "$passwd" -t "$g_timeout" -m "scp-out" -s "$src" -d "$dst" > /dev/null 2>&1
ret=$?
if [ $ret -ne 0 ]; then
log ERROR "Put ${src} to ${host}:${dst} failed , ret code:${ret}"
else
log INFO "Put ${src} to ${host}:${dst} successfully."
fi
return $ret
}
##################################################
#
# 把远端服务器文件拷贝至本地
#
###################################################
function cp_file_from_r()
{
local g_port=$1
local g_user_name=$4
local g_timeout=$2
local host=$3 #远端主机名
local passwd=$5 #主机密码
local src=$6 #源文件
local dst=$7 #目标文件
local ret=0 #返回值
local name=""
local ret=0 #返回值
#入参判断
if [ -z "$host" ]; then
log ERROR "host is null"
return 1
fi
if [ -z "$src" ]; then
log ERROR "src is null"
return 1
fi
if [ -z "$dst" ]; then
log ERROR "dst is null"
return 1
fi
ping -c 3 -i 0.2 -W 3 $host &> /dev/null
if [ $? != 0 ];then
log ERROR "Ping $host failed"
return 1
fi
#获取 源文件的文件名称
name="${host}_$(basename "${src}")"
bash scripts/remote.sh -i "$host" -P "$g_port" -u "$g_user_name" -p "$passwd" -t "$g_timeout" -m "scp-in" -s "$src" -d "${dst}/${name}" > /dev/null 2>&1
ret=$?
if [ $ret -ne 0 ]; then
log ERROR "get ${dst}/${name} from ${host}:${src} failed , ret code:${ret}"
else
log INFO "get ${dst}/${name} from ${host}:${src} successfully."
fi
return $ret
}