scp 自动输入密码

文摘 Linux 2020-03-12 阅读:5542

  当我们在编译代码的时候,往往需要从别外的主机或服务器上拉取文件,在使用scp的时候,每次都需要输入密码,如果使用非常频繁的,就会做很多无用功,下面有一种方式来解决这个问题。

1. 以下代码保存为: expect_scp

#!/usr/bin/expect
set timeout 10
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set src_file [lindex $argv 3]
set dest_file [lindex $argv 4]
spawn scp $username@$host:$src_file $dest_file
 expect {
 "(yes/no)?"
  {
    send "yes\n"
    expect "*assword:" { send "$password\n"}
  }
 "*assword:"
  {
    send "$password\n"
  }
}
expect "100%"
expect eof

2. 在需要使用的脚本中加入:

./expect_scp 服务器IP 用户名 密码 源目录 目的目录

以上操作,即可无需用户干预的拷贝文件。

3.另外 sudo自动输入密码参数

echo "password" | sudo  -S commander

0条评论

© 2024 芯缘异码. Powered by Typecho