0

我想使用 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

谢谢

4

1 回答 1

-1
  1. 使用使用语法在 ~/.ssh/config 中添加您的服务器:

```

Host server1
    HostName 192.168.1.10

Host server2
    HostName 192.168.1.11

```

  1. 创建 ssh 密钥:ssh-keygen,
  2. 将您的公钥添加~/.ssh/rsa.pub~/.ssh/authorized_keys服务器上。

现在,您可以使用以下方式发送文件scpscp file.zip server1:~/ https ://nerderati.com/2011/03/17/simplify-your-life-with-an-ssh-config-file/

于 2019-01-04T07:57:02.340 回答