SSH到多个服务器,然后sftp并将文件复制到服务器?

时间:2019-01-04 07:49:57

标签: linux bash ssh sftp

我想使用bash脚本ssh到多台服务器,并使用crontab自动化该脚本。由于身份验证的需要,我使用“期望”来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

谢谢

1 个答案:

答案 0 :(得分:-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

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