三种Shell脚本编程中避免SFTP输入密码的方法

最近编程中用到sftp上传文件,且需要用crontab预设定时上传事件。而sftp不同于ftp,没有提供选项如 -i 可以将密码直接编码进程序。使用sftp指令,会自动请求用户输入密码。

总结一下可以避免sftp输入密码的三种方式:

1. lftp方式

LFTP是一款非常著名的字符界面的文件传输工具。支持FTP、HTTP、FISH、SFTP、HTTPS和FTPS协议。
例子:(本例为下载192.168.107.132服务器/home/huangmr下所有文件的例子)

 

#!/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 *.*  
bye  
EOF 
 
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

例子:


[plain]  view plain copy
  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上更是方便。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的shell脚本,它使用sftp登录远程服务器并执行下载、上传、删除和查看指定路径的操作。在使用之前,请将脚本的“HOST”、“USERNAME”、“PASSWORD”和“REMOTE_PATH”替换为您自己的实际信息。 ```bash #!/bin/bash # 远程服务器信息 HOST="remote_server_address" USERNAME="remote_username" PASSWORD="remote_password" # 远程路径 REMOTE_PATH="/path/to/remote/directory" # 下载文件 download_file() { echo "Enter the name of the file you want to download:" read FILENAME sftp ${USERNAME}@${HOST}:${REMOTE_PATH}/${FILENAME} <<EOF get ${FILENAME} EOF echo "Download complete!" } # 上传文件 upload_file() { echo "Enter the name of the file you want to upload:" read FILENAME sftp ${USERNAME}@${HOST}:${REMOTE_PATH} <<EOF put ${FILENAME} EOF echo "Upload complete!" } # 删除文件 delete_file() { echo "Enter the name of the file you want to delete:" read FILENAME sftp ${USERNAME}@${HOST}:${REMOTE_PATH} <<EOF rm ${FILENAME} EOF echo "Delete complete!" } # 查看文件列表 list_files() { sftp ${USERNAME}@${HOST}:${REMOTE_PATH} <<EOF ls EOF } # 主菜单 while true do echo "Please select an operation:" echo "1. Download file" echo "2. Upload file" echo "3. Delete file" echo "4. List files" echo "5. Exit" read CHOICE case $CHOICE in 1) download_file;; 2) upload_file;; 3) delete_file;; 4) list_files;; 5) echo "Goodbye!"; exit 0;; *) echo "Invalid choice. Please try again.";; esac done ``` 注意:这个脚本并不是完美的,它可能存在一些问题。在使用之前,请仔细检查并根据自己的需要进行修改。如果您不熟悉shell脚本编程,请在使用之前进行一些基本的学习。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值