我想使用 bash 脚本 ssh 到多个服务器,并使用 crontab 自动执行此脚本。由于身份验证需要,我使用“expect”ssh 到多个服务器。但是,我不知道如何使用 SFTP 将目标服务器中的文件复制到我的服务器。有人可以给我一些关于这个问题的线索。
这是我通过 SSH 连接到多个服务器的代码(在这种情况下,我通过隧道连接到服务器目标):
/home/users/script/expect.sh 45108 username password "command"
/home/users/script/expect.sh 45109 username password "command"
#45108 is port for tunneling, username and password is using like in shell terminal (ssh username@ipadd -p $server)
这是我使用的期望脚本:
#!/usr/bin/expect
set timeout 10
set node [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]
spawn ssh $username@localhost -p $node
expect {
"(yes/no)?"
{
send "yes\n"
expect "*assword:" { send "$password\n"}
}
"*assword:" { send "$password\n" }
}
expect {
"*#" { send "$command\n" }
}
expect {
"*#" { send "exit\n" }
}
expect eof
谢谢