總結三種Shell指令碼程式設計中避免SFTP輸入密碼的方法
阿新 • • 發佈:2019-01-02
最近程式設計中用到sftp上傳檔案,且需要用crontab預設定時上傳事件。而sftp不同於ftp,沒有提供選項如 -i 可以將密碼直接編碼程序序。使用sftp指令,會自動請求使用者輸入密碼。
總結一下可以避免sftp輸入密碼的三種方式:
1. lftp方式
[plain] view plaincopyprint?- #!/bin/sh
- HOST=172.16.2.X
- USER=kg_sftp
-
PASS=tnzk4a7w
- echo "Starting to sftp..."
- lftp -u ${USER},${PASS} sftp://${HOST} <<EOF
- cd /kagou/datafile
- mget *.*
- bye
- EOF
- echo "done"
2. expect方式
Expect是一個免費的程式設計工具語言,用來實現自動和互動式任務進行通訊,而無需人的干預。
要使用expect需要預先安裝tcl這個東西,然後再安裝expect包。
例子:
[plain] view plaincopyprint?
-
#!/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" #自動輸入密碼
- expect {
-
"sftp*" { #檢測返回sftp>
- return 0
- }
- }
- }
- }
- # 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: $file1";
- #check if all were provided
- if { $user == "" || $passw == "" || $host == "" || $location == "" || $file1 == "" } {
- puts "Usage: <user> <passw> <host> <location> <file1 to send>/n"
- exit 1
- }
- #sftp to specified host and send the files
- spawn sftp [email protected]$host
- set rez [connect $passw]
- if { $rez == 0 } {
- send "cd $location/r"
- set timeout -1
- send "put $file1/r"
- #send "ls -l/r"
- #send "quit/r"
- #send "mkdir testsftp/r"
- send "quit/r"
- expect eof
- exit 0
- }
- puts "/nCMD_ERR: connecting to server: $host!/n"
- exit 1
- 0
expect也可以用兩種形式呼叫
1 ./my.exp $usr $pwd $host $local $file
2. 程式碼中直接插入
expect<<!
...
!
3. (推薦)生成金鑰對
因為這種方式不用把金鑰解除安裝程式裡,所以更安全
第一步:生成密匙對,我用的是rsa的金鑰。使用命令 "ssh-keygen -t rsa" [[email protected] user1]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/user1/.ssh/id_rsa): Created directory '/home/user1/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification 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[email protected] [[email protected] user1]$生成的過程中提示輸入金鑰對儲存位置,直接回車,接受預設值就行了。接著會提示輸入一個不同於你的password的密碼,直接回車,讓它空著。
當然,也可以輸入一個。(我比較懶,不想每次都要輸入密碼。) 這樣,金鑰對就生成完了。
其中公共金鑰儲存在 ~/.ssh/id_rsa.pub
私有金鑰儲存在 ~/.ssh/id_rsa 然後改一下 .ssh 目錄的許可權,使用命令 "chmod 755 ~/.ssh"
[[email protected] user1]$ chmod 755 ~/.ssh 之後把這個金鑰對中的公共金鑰複製到你要訪問的機器上去,並儲存為 ~/.ssh/authorized_keys [[email protected] user1]$ scp ~/.ssh/id_rsa.pub rh1:/home/user1/.ssh/authorized_keys
[email protected]'s password: id_rsa.pub 100% 228 3.2MB/s 00:00 [[email protected] user1]$
之這樣就大功告成了。之後再用ssh scp sftp 之類的訪問那臺機器時,就不用輸入密碼
了,用在script上更是方便。