因为OCS有几套系统,每次导数据都要在几套系统上做一遍,都是重复的工作,因些写一个脚本自己在几套系统上执行相关的命令。已经成功执行。
程序中telnet and ftp 用到here document,这个要注意end limiter must start at left。
下面是脚本的主要框架:
#!/bin/bash
#don't use chinese character
#####################################################
# define param
#####################################################
CONFIG_FILE="auto.cfg"
REMOTEHOST=""
USERNAME=""
PASSWD=""
SRC_DIR="userfile/llh/task"
DEST_DIR="userfile/llh/task"
COMMAND=$1
COMMAND_FILE=$2
#####################################################################
# function start
#####################################################################
#param:remoteHost,username,password,dir
function my_telnet
{
REMOTE_HOST=$1
USERNAME=$2
PASSWORD=$3
DEST_DIR=$4
COMMAND=$5
COMMAND_FILE=$6
echo "start telnet ${REMOTE_HOST}......"
#exec telnet
(
#login...
sleep 1
echo $USERNAME
sleep 1
echo $PASSWORD
#exec your command
#########command start##########
sleep 2
echo "cd $DEST_DIR"
sleep 2
echo "sh $COMMAND $COMMAND_FILE &"
sleep 3
#########command end############
echo "exit"
) | telnet ${REMOTE_HOST}
echo "exec finish......"
echo "exit telnet ${REMOTE_HOST}......"
}
#ftp put command and file
function ftp_put
{
#put command and file
REMOTEHOST=$1
USERNAME=$2
PASSWD=$3
SRC_COMMAND=$5
SRC_FILE=$6
DEST_DIR=$4
#ip=
#user
ftp -n $REMOTEHOST <<FTPEOF
user $USERNAME $PASSWD
binary
prompt
cd $DEST_DIR
put $SRC_COMMAND
put $SRC_FILE
close
bye
FTPEOF #here must start at first column.
}
#ftp put file only
function ftp_put_file
{
#put command and file
REMOTEHOST=$1
USERNAME=$2
PASSWD=$3
SRC_FILE=$5
DEST_DIR=$4
#ip=
#user
ftp -n $REMOTEHOST <<FTPEOF
user $USERNAME $PASSWD
binary
prompt
cd $DEST_DIR
put $SRC_FILE
close
bye
FTPEOF
}
function usage
{
echo "Usage $0 [command] [file]"
echo "Function:execute command at location and remote host like this:"
echo "command file"
}
function test_input
{
echo "welcome...."
}
##################################################
# function end
##################################################
################################################################################
# main start
################################################################################
test_input