2020-05-12 10:41:59
浏览数 (1)
transfer_file.sh
代码语言:javascript
复制#!/usr/bin/expect
###############################################
# Author : PedroQin
# Date : 2020-04-26 20:26:20
# Description :
# Version : 1.0.0
###############################################
set timeout -1
set script_name $argv0
# send or recive file
set send_receive [lindex $argv 0]
set dir_name [lindex $argv 1]
set file_name [lindex $argv 2]
set server_offset [lindex $argv 3]
set server_id [lindex $argv 4]
set username [lindex $argv 5]
set host [lindex $argv 6]
set passwd [lindex $argv 7]
set hostname [lindex $argv 8]
set remain_argv [lrange $argv 9 999]
set basename [lindex [split "$file_name" "/"] end]
set server_offset [expr $server_offset 1]
if {$send_receive=="send"} {
# send file
if {$remain_argv!=""} {
send_user "$server_offset. scp to $server_id $host ##**== ==**## ==> "
if {$server_offset=="1"} {
spawn scp -r $file_name $username@$host:$dir_name/
} else {
spawn scp -r $dir_name/$basename $username@$host:$dir_name/
}
expect {
"yes/no" { send "yesn"; exp_continue}
"password:" { send "$passwdn" ; exp_continue}
}
send_user "$server_offset. ssh to $server_id $host ##**== ==**## ==> "
spawn ssh $username@$host
expect {
"yes/no" { send "yesn"; exp_continue}
"$host's password:" { send "$passwdn" ; exp_continue}
"$hostname*#" { send "$script_name $send_receive $dir_name $file_name $server_offset $remain_argv && wait && sync && exitn" ; exp_continue}
}
} else {
send_user "$server_offset. scp to $server_id $host ##**== ==**## ==> "
spawn scp -r $dir_name/$basename $username@$host:$dir_name/
expect {
"yes/no" { send "yesn"; exp_continue}
"password:" { send "$passwdn" ; exp_continue}
}
}
} else {
# recive file
spawn mkdir -p $dir_name
if {$remain_argv!=""} {
send_user "$server_offset. ssh to $server_id $host ##**== ==**## ==> "
spawn ssh $username@$host
expect {
"yes/no" { send "yesn"; exp_continue}
"$host's password:" { send "$passwdn" ; exp_continue}
"$hostname*#" { send "$script_name $send_receive $dir_name $file_name $server_offset $remain_argv && wait && sync && exitn" ; exp_continue}
}
send_user "$server_offset. scp from $server_id $host ##**== ==**## ==> "
spawn scp -r $username@$host:$dir_name/$basename $dir_name/
expect {
"yes/no" { send "yesn"; exp_continue}
"$host's password:" { send "$passwdn" ; exp_continue}
}
} else {
send_user "$server_offset. scp from $server_id $host ##**== ==**## ==> "
spawn scp -r $username@$host:$file_name $dir_name/
expect {
"yes/no" { send "yesn"; exp_continue}
"password:" { send "$passwdn" ; exp_continue}
}
}
}
#interact
#expect eof
multi_scp.sh
代码语言:javascript
复制#!/bin/bash
###############################################
# Author : PedroQin
# Date : 2020-04-26 22:43:32
# Description :
# Version : 1.0.0
###############################################
config_file="/etc/multi_scp_conf.xml"
parameters=""
# the file is not in $dir
not_dir=0
# show message in green
function green_message()
{
tput bold
echo -ne "