三種Shell腳本編程中避免SFTP輸入密碼的方法


最近編程中用到sftp上傳文件,且需要用crontab預設定時上傳事件。而sftp不同於ftp,沒有提供選項如 -i 可以將密碼直接編碼進程序。使用ftp指令,會自動請求用戶輸入密碼。

總結一下可以避免sftp輸入密碼的三種方式:

1. lftp方式

LFTP是一款非常著名的字符界面的文件傳輸工具。支持FTP、HTTP、FISH、SFTP、HTTPS和FTPS協議。
例子:(本例為下載例子)

  #!/bin/sh   

  1. HOST=172.16.2.X  
  2. USER=kg_sftp    
  3. PASS=tnzk4a7w    
  4. echo "Starting to sftp..."  
  5. lftp -u ${USER},${PASS} sftp://${HOST} <<EOF   
  6. cd /kagou/datafile    
  7. mget *.*    
  8. bye    
  9. EOF    
  10. echo "done"   

2. expect方式

Expect是一個免費的編程工具語言,用來實現自動和交互式任務進行通信,而無需人的干預。

要使用expect需要預先安裝tcl這個東西,然后再安裝expect包。

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

例子:


  1. #!/usr/local/bin/expect -f    
  2. #<---insert here your expect program location  
  3. #procedure to attempt connecting; result 0 if OK, 1 elsewhere  
  4.  proc connect {passw} {  
  5.   expect {  
  6.     "(yes/no)?" {send "yes/r";exp_continue} #第一次使用SFTP時候會要求輸入yes/no   
  7.     "password:" {send "$passw/r"            #自動輸入密碼  
  8.   expect {  
  9.      "sftp*" {        #檢測返回sftp>  
  10.    return 0  
  11.       }    
  12.   }  
  13.      }  
  14.   }  
  15.   # timed out  
  16.   return 1  
  17.  }  
  18.    
  19.  #read the input parameters  
  20.  set user [lindex $argv 0]  
  21.  set passw [lindex $argv 1]  
  22.  set host [lindex $argv 2]  
  23.  set location [lindex $argv 3]  
  24.  set file1 [lindex $argv 4]  
  25.    
  26.  #puts "Am citit:/n";  
  27.  #puts "user: $user";  
  28.  #puts "passw: $passw";  
  29.  #puts "host: $host";  
  30.  #puts "location: $location";  
  31.  #puts "file1: $file1";  
  32.   
  33.    
  34.  #check if all were provided  
  35.  if { $user == "" || $passw == "" || $host == "" || $location == "" || $file1 == "" }  {  
  36.    puts "Usage: <user> <passw> <host> <location> <file1 to send>/n"  
  37.    exit 1  
  38.  }  
  39.    
  40.  #sftp to specified host and send the files  
  41.  spawn sftp $user@$host  
  42.    
  43.  set rez [connect $passw]  
  44.  if { $rez == 0 } {  
  45.    send "cd $location/r"  
  46.    set timeout -1  
  47.    send "put $file1/r"  
  48.    #send "ls -l/r"  
  49.    #send "quit/r"  
  50.    #send "mkdir testsftp/r"  
  51.    send "quit/r"  
  52.    expect eof  
  53.    exit 0  
  54.  }  
  55.  puts "/nCMD_ERR: connecting to server: $host!/n"  
  56.  exit 1  
  57.  0  
expect也可以用兩種形式調用

1   ./my.exp $usr $pwd $host $local $file

2. 代碼中直接插入 

expect<<!

...

!

 

3. (推薦)生成密鑰對

因為這種方式不用把密鑰卸載程序里,所以更安全

第一步:生成密匙對,我用的是rsa的密鑰。使用命令 "ssh-keygen -t rsa"
   [user1@rh 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 user1@rh.test.com
   [user1@rh user1]$

生成的過程中提示輸入密鑰對保存位置,直接回車,接受默認值就行了。接着會提示輸入一個不同於你的password的密碼,直接回車,讓它空着。
當然,也可以輸入一個。(我比較懶,不想每次都要輸入密碼。) 這樣,密鑰對就生成完了。

其中公共密鑰保存在 ~/.ssh/id_rsa.pub
私有密鑰保存在 ~/.ssh/id_rsa
然后改一下 .ssh 目錄的權限,使用命令 "chmod 755 ~/.ssh"
   [user1@rh user1]$ chmod 755 ~/.ssh
 
之后把這個密鑰對中的公共密鑰復制到你要訪問的機器上去,並保存為
   ~/.ssh/authorized_keys
   [user1@rh user1]$ scp ~/.ssh/id_rsa.pub rh1:/home/user1/.ssh/authorized_keys 
  
  
user1@rh1's password:
   id_rsa.pub                                    100%  228     3.2MB/s   00:00
   [user1@rh user1]$

之這樣就大功告成了。之后再用ssh scp sftp 之類的訪問那台機器時,就不用輸入密碼
了,用在script上更是方便。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM