functionmake_tmp_file()
{
local
name
=
$
{
1
:
-
"rand"
}
local
magic
=
`
date
+
%
s
`
echo
"/tmp/__${magic}__${name}"
}
function
make_rand_file
(
)
{
make_tmp
_file
"$*"
}
# int exec_script(host, port, user, passwd, script, [timeout = 86400])
# 0, succ
# 1, fail
# 2, invalid params
# 3, fail
# 4, invalid script
# 5, login fail
# 6, put file fail
# 7, timeout
# 8, remote execute script fail
function
exec_script
(
)
{
if
[
$
# -lt 5 ]; then
return
2
fi
local
host
=
$
1
local
port
=
$
2
local
user
=
$
3
local
passwd
=
$
4
local
script
=
$
5
local
timeout
=
$
(
(
$
{
6
:
-
86400
}
)
)
# default 1 day
local
rscp
=
$
(
make_rand
_file
`
basename
$script
`
)
local
ret
=
0
if
[
!
-
f
$script
]
;
then
return
4
fi
echo
"script = ${script}, remote_script = ${rscp}"
ssh
_put
$host
$port
$user
$passwd
$script
$rscp
expect
-
c"
set
timeout
$timeout
set
flag
0
spawn
ssh
$user
@
$host
;
expect
{
\"
*
assword
*
\"
{
send
$passwd
\
r
;
}
\"
yes
\
/
no
)
?
\"
{
set
flag
1
;
send
yes
\
r
;
}
\"
Welcome
\"
{
}
}
if
{
\
$flag
==
1
}
{
expect
{
\"
*
assword
\"
{
send
$passwd
\
r
;
}
}
}
expect
{
\"
*
assword
*
\"
{
puts
\"
INVALID
PASSWD
,
host
=
$host
,
user
=
$user
,
passwd
=
$passwd
\"
;
exit
1
}
\"
#\ \" {} \"$\ \" {} \">\ \" {}
}
send
$rscp
\
r
;
expect
{
\"
#\ \" {} \"$\ \" {} \">\ \" {}
}
send
\"
rm
-
f
$rscp
\
r
\"
send
exit
\
r
;
expect
eof
{
exit
0
}
"
ret
=
$
?
if
[
$ret
-
ne
0
]
;
then
return
5
fi
return
0
}
# int exec_cmd(host, port, user, passwd, cmd, [timeout = 86400], [rfile = /dev/null])
# 0, succ
# 1, fail
# 2, invalid params
# 3, fail
# 4, invalid script
# 5, login fail
# 6, put file fail
# 7, timeout
# 8, remote execute script fail
function
exec_cmd
(
)
{
if
[
$
# -lt 5 ]; then
return
4
fi
local
host
=
$
1
local
port
=
$
2
local
user
=
$
3
local
passwd
=
$
4
local
cmd
=
$
5
local
timeout
=
$
(
(
$
{
6
:
-
86400
}
)
)
# default 1 day
local
rfile
=
$
{
7
-
"/dev/null"
}
#local log=$(make_tmp_file "${__GEAR_SH}_exec_cmd")
local
ret
=
0
expect
-
c"
set
timeout
$timeout
set
flag
0
spawn
ssh
$user
@
$host
;
expect
{
\"
*
assword
*
\"
{
send
$passwd
\
r
;
}
\"
yes
\
/
no
)
?
\"
{
set
flag
1
;
send
yes
\
r
;
}
\"
Welcome
\"
{
}
}
if
{
\
$flag
==
1
}
{
expect
{
\"
*
assword
\"
{
send
$passwd
\
r
;
}
}
}
expect
{
\"
*
assword
*
\"
{
puts
\"
INVALID
PASSWD
,
host
=
$host
,
user
=
$user
,
passwd
=
$passwd
\"
;
exit
1
}
\"
#\ \" {} \"$\ \" {} \">\ \" {}
}
log_file
$
{
rfile
}
send
\"
$cmd
\
r
\"
;
expect
{
\"
#\ \" {} \"$\ \" {} \">\ \" {}
}
send
exit
\
r
;
expect
eof
{
exit
0
}
"
cat
$
{
rfile
}
|
sed
-
n
'1,/[#$>].*exit/p'
|
sed
'1d;$d'
>
$
{
rfile
}
#if [ -f ${log} ]; then rm ${log}; fi
ret
=
$
?
if
[
$ret
-
ne
0
]
;
then
return
5
fi
return
0
}