最近编程中用到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. #read the input parameters
  19. set user [lindex $argv 0]
  20. set passw [lindex $argv 1]
  21. set host [lindex $argv 2]
  22. set location [lindex $argv 3]
  23. set file1 [lindex $argv 4]
  24. #puts "Am citit:/n";
  25. #puts "user: $user";
  26. #puts "passw: $passw";
  27. #puts "host: $host";
  28. #puts "location: $location";
  29. #puts "file1: $file1";
  30. #check if all were provided
  31. if { $user == "" || $passw == "" || $host == "" || $location == "" || $file1 == "" }  {
  32. puts "Usage: <user> <passw> <host> <location> <file1 to send>/n"
  33. exit 1
  34. }
  35. #sftp to specified host and send the files
  36. spawn sftp $user@$host
  37. set rez [connect $passw]
  38. if { $rez == 0 } {
  39. send "cd $location/r"
  40. set timeout -1
  41. send "put $file1/r"
  42. #send "ls -l/r"
  43. #send "quit/r"
  44. #send "mkdir testsftp/r"
  45. send "quit/r"
  46. expect eof
  47. exit 0
  48. }
  49. puts "/nCMD_ERR: connecting to server: $host!/n"
  50. exit 1
  51. 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:e7user1@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上更是方便。

三种Shell脚本编程中避免SFTP输入密码的方法的更多相关文章

  1. 拾遗:Perl 在 Shell 脚本编程中的应用

    Perl 对我用途,仅是作为 Shell 脚本中的文本处理器:在较大的软件工程里,更多的是使用 C.go 等编译型语言. Perl 是一种历史比较悠久的动态编程语言,在各种类 Unix 系统中得到了应 ...

  2. Shell脚本编程中的几个问题

    条件语句 正确的写法: if [ $1 = "-f" ] #注意这里,前后方括号和中间的内容之间必须有空格! then commands fi 错误的写法: if [$1 == & ...

  3. Shell脚本编程中截取字符串方法

    例如: 假设变量var=http://www.baidu.com/111.png 1.#号截取(删左留右) echo ${var#*//} # 号是运算符,*// 表示从左边开始删除第一个 // 号及 ...

  4. [linux] shell脚本编程-xunsearch安装脚本学习

    安装脚本setup.sh #!/bin/sh # FULL fast install/upgrade script # See help message via `--help' # $Id$ # s ...

  5. centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课

    centos  shell脚本编程2 if 判断  case判断   shell脚本中的循环  for   while   shell中的函数  break  continue  test 命令   ...

  6. centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课

    centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件.目录属性  shell数组简单用法 $( ) 和$ ...

  7. Linux shell脚本编程(三)

    Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...

  8. 好记性比如烂笔头--linux学习笔记7关于linux中的shell脚本编程

    之前看的各种面试,貌似都有shell脚本编程,没了解之前感觉很复杂,现在了解了些,没想象中那么难. 逻辑主要是这样的 编写.sh的脚本文件,文件里面的代码,就是在命令行输入的可执行命令的加强版,所谓加 ...

  9. Linux shell脚本编程(二)

    Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...

随机推荐

  1. 远程管理控制ssh

    传统的网络服务程序,FTP.POP.telnet 本质上都是不安全的,因为它们在网络上通过明文传送口令和数据,这些数据非常容易被截获.SSH叫做Secure Shell.通过SSH,可以把传输数据进行 ...

  2. linux之gzip命令

    命令格式: gzip [选项] 压缩(解压缩)的文件名 参数: -d 将压缩文件解压. -l  对每个压缩文件,显示压缩文件的大小,未压缩文件的大小,压缩比,未压缩文件的名字 -v 对每一个压缩和解压 ...

  3. ServiceStatusUtils判断服务是否运行

    import android.app.ActivityManager; import android.app.Service; import android.content.Context; impo ...

  4. CSS 浮动 float 属性

    浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止. 由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样. 请看下图,当把框 1 向右浮动时,它 ...

  5. JVM内存模型及配置参数

    JVM 分为堆.栈.方法区.程序计数器.本地方法栈 栈内存存放局部变量表.操作栈.动态链接.方法出口等信息 1.  局部变量表存放了编译期可知的各种基本数据类型(boolean.byte.char.s ...

  6. JMeter使用plugins插件进行服务器性能监控

    JMeter使用plugins插件进行服务器性能监控 性能测试时,我们的关注点有两部分 1 服务本身:并发响应时间 QPS 2 服务器的资源使用情况:cpu memory I/O disk等 JMet ...

  7. 最短路径:Dijkstra算法 C#

    class Program { ; static void Main(string[] args) { Console.WriteLine("各点距离矩阵如下:"); Consol ...

  8. 深入理解红黑树及C++实现

    介绍 红黑树是一种特殊的平衡二叉树(AVL),可以保证在最坏的情况下,基本动态集合操作的时间复杂度为O(logn).因此,被广泛应用于企业级的开发中. 红黑树的性质 在一棵红黑树中,其每个结点上增加了 ...

  9. 【转】jstat命令查看jvm的GC情况 (以Linux为例)

    [From]https://www.cnblogs.com/yjd_hycf_space/p/7755633.html jstat命令可以查看堆内存各部分的使用量,以及加载类的数量.命令的格式如下: ...

  10. 事件冒泡 --- 仿select下拉框

    要求:点击按钮时,下拉框显示:点击页面其他部分时,下拉框消失: 1. 不靠谱代码 <!DOCTYPE html> <html> <head lang="en&q ...