两台服务器之间的SFTP文件传输,并从第三台服务器运行此脚本

问题描述 投票:0回答:1

我已经使用TCL Expect开发了一个脚本。该脚本的用法是-如果用户从服务器A运行它,它将检查服务器B与服务器C之间的sftp文件传输。以下是我的代码:

#!/usr/bin/expect -f
lassign $argv 1 2
spawn ping -c 2 -i 3 -W 1 $1
expect  {
   " 0%" {puts "Source is rechable!"}
   " 100.0%" {puts "Source is not rechable.Please restart IPSEC and check!";exit 1}
        }
#SSH to remote server $1
spawn ssh -o StrictHostKeyChecking=no root@$1
expect {                
"Password:" {send "password\r";exp_continue}
"*#" {send "ssh successful\r";exp_continue}
   }
send "\n"
#Creating a file in remote server which will be transferred via sftp
send "touch /tmp/mfile\n"
expect "#"
send "chmod 755 /tmp/mfile\n"
expect "#"
#sftp to server$2
spawn sftp -o StrictHostKeyChecking=no root@$2
expect {                
"Password:" {send "Training\r";exp_continue}
"sftp>" {send "sftp successful\r";exp_continue}
       }
send "\n"
#sending remote file
send "put /tmp/mfile\n"
send "\n"
sleep 2
send "File send to Remote Server successfully\n"
expect "sftp>"
send "cd /root/\n"
expect "sftp>"
send "rename mfile mfile_1\n"
expect "sftp>"
#sending back the file
send "get mfile_1 /tmp\n"
expect "sftp>"
sleep 2
send "quit\n"
send "exit\n"

问题是文件已使用此代码从服务器B传输到服务器C,但文件未发送回服务器B。我尚未在代码中添加其他逻辑,但首先要检查是否基本代码有效。关于回传文件的任何线索都将有所帮助。

shell tcl sftp expect
1个回答
0
投票
spawn始终在本地运行。对于

spawn ssh server-B ... spawn sftp server-C

[当您spawn sftp server-C时,服务器A将启动到服务器C的新SFTP会话。
© www.soinside.com 2019 - 2024. All rights reserved.