Recent programming uses SFTP upload files, and need to use crontab preset timed upload events. Unlike FTP, SFTP does not provide options such as-I can encode the password directly into the program. Using the SFTP directive, the user is automatically requested to enter a password.
Summarize the three ways you can avoid sftp entering passwords:
1. Lftp mode
Lftp is a very famous file Transfer tool for character interface. Supports FTP, HTTP, FISH, SFTP, HTTPS, and FTPS protocols.
Example: (This example is for downloading 192.168.107.132 server/HOME/HUANGMR All files under the example)
#!/bin/sh
host=192.168.107.132
user=huangmr
pass=huangmr
echo "Starting to sftp ..."
lftp-u ${ User},${pass} sftp://${host}:22 <<eof
cd/home/huangmr
mget *.*
EOF
2. Expect mode
Expect is a free programming language used to automate and interact with tasks without human intervention.
To use expect, you need to install TCL before installing the expect package.
Tcl:http://prdownloads.sourceforge.net/tcl/tcl8.4.16-src.tar.gz
Expect:http://sourceforge.net/projects/expect/files/expect/5.45/expect5.45.tar.gz/download
Example:
[Plain] View plain copy #!/usr/local/bin/expect -f #<---insert here your expect program location #procedure to attempt connecting; result 0 if ok, 1 elsewhere proc connect {passw} { expect { "(yes/no)?" {send "yes/r";exp_continue} #第一次使用SFTP时候会要求输入yes/no " Password: " {send $PASSW/R" # Auto-Enter password expect { "sftp*" { #检测返回sftp > return 0 } } } &NBSP;&NBSP;}&Nbsp; # timed out return 1 } #read the input parameters set user [ lindex $argv 0] set passw [lindex $argv 1] set host [lindex $argv 2] set location [lindex $argv 3] set file1 [lindex $argv 4] #puts "am citit:/n"; #puts "user: $user"; #puts "PASSW: $PASSW "; #puts " host: $host "; #puts " location: $ Location "; #puts " file1: $file 1 "; # check if all were provided if { $user == "" | | $passw == "" | | $host == "" | | $location == "" | | $file 1 == " } { puts " Usage: <user > <passw>
Expect can also be invoked in two ways
1./my.exp $usr $pwd $host $local $file
2. Inserting directly into the code
expect<<!
...
!
3. (recommended) Generate key pair
Because it is not necessary to uninstall the key in this way, it is a safer first step: generate the key pair, I use the RSA key. Using the command "ssh-keygen-t RSA" [USER1@RH user1]$ ssh-keygen-t RSA generating public/private RSA key p Air. Enter file in which to save the key (/home/user1/.ssh/id_rsa): Created directory '/home/user1/.s Sh '. Enter passphrase (empty for no passphrase): enter same passphrase again: Your ident Ification has been saved In/home/user1/.ssh/id_rsa. Your Public Key has been saved in/home/user1/.ssh/id_rsa.pub. The key fingerprint is: e0:f0:3b:d3:0a:3d:da:42:01:6a:61:2f:6c:a0:c6:e7 user1@rh.test.com & nbsp; [User1@rh user1]$
is prompted to enter the key pair save location during the build process, direct carriage return, accept the default value on the line. Then you will be prompted to enter a different password than your password, go straight to the car and leave it empty.
Of course, you can also enter one. (I am lazy, do not want to enter the password every time.) Thus, the key pair is generated.
where the public key is saved in the ~/.ssh/id_rsa.pub
private key is saved in ~/.ssh/id_rsa and then changed. Permissions for the SSH directory, using the command "chmod 755 ~/.ssh"
[user 1@RH user1]$ chmod 755 ~/.ssh