三种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
例子:
- #!/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 $user@$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. (推荐)生成密钥对
因为这种方式不用把密钥卸载程序里,所以更安全
生成的过程中提示输入密钥对保存位置,直接回车,接受默认值就行了。接着会提示输入一个不同于你的password的密码,直接回车,让它空着。
当然,也可以输入一个。(我比较懒,不想每次都要输入密码。) 这样,密钥对就生成完了。
其中公共密钥保存在 ~/.ssh/id_rsa.pub
私有密钥保存在 ~/.ssh/id_rsa
[user1@rh user1]$ chmod 755 ~/.ssh
之这样就大功告成了。之后再用ssh scp sftp 之类的访问那台机器时,就不用输入密码
了,用在script上更是方便。
三种Shell脚本编程中避免SFTP输入密码的方法的更多相关文章
- 拾遗:Perl 在 Shell 脚本编程中的应用
Perl 对我用途,仅是作为 Shell 脚本中的文本处理器:在较大的软件工程里,更多的是使用 C.go 等编译型语言. Perl 是一种历史比较悠久的动态编程语言,在各种类 Unix 系统中得到了应 ...
- Shell脚本编程中的几个问题
条件语句 正确的写法: if [ $1 = "-f" ] #注意这里,前后方括号和中间的内容之间必须有空格! then commands fi 错误的写法: if [$1 == & ...
- Shell脚本编程中截取字符串方法
例如: 假设变量var=http://www.baidu.com/111.png 1.#号截取(删左留右) echo ${var#*//} # 号是运算符,*// 表示从左边开始删除第一个 // 号及 ...
- [linux] shell脚本编程-xunsearch安装脚本学习
安装脚本setup.sh #!/bin/sh # FULL fast install/upgrade script # See help message via `--help' # $Id$ # s ...
- centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课
centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 ...
- centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课
centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件.目录属性 shell数组简单用法 $( ) 和$ ...
- Linux shell脚本编程(三)
Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...
- 好记性比如烂笔头--linux学习笔记7关于linux中的shell脚本编程
之前看的各种面试,貌似都有shell脚本编程,没了解之前感觉很复杂,现在了解了些,没想象中那么难. 逻辑主要是这样的 编写.sh的脚本文件,文件里面的代码,就是在命令行输入的可执行命令的加强版,所谓加 ...
- Linux shell脚本编程(二)
Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...
随机推荐
- git——sourceTree
基本操作 修改密码怎么办? Tools → Options → Authentication 修改密码:或者删除账户,重新拉取需用户名.密码,重新输入即可
- 七十六:flask.Restful之flask-Restful插件的基本使用
安装:flask 0.8以上.python2.6或者3.3以上:pip install flask-restful 使用方法:1.从flask_restful中导入Api,来创建对象 2.写一个视图函 ...
- cisco三层交换为vlan配置dhcp
dhcp(config)#vlan 2 dhcp(config-vlan)#name sales dhcp(config-vlan)#vlan 3 dhcp(config-vlan)#name ...
- Django book 2.0 的中文翻译
传送门大法好: http://djangobook.py3k.cn/2.0/
- 【VS开发】【电子电路技术】VPX技术介绍
VPX技术介绍 杨跃江 2009-12-18 杨跃江 13902479857 中科祥云(深圳)信息产业有限公司 1VPX 技术 新型VPX(VITA 46)标准是自从VME引入后的25年来,对于VME ...
- 【Linux开发】linux设备驱动归纳总结(五):3.操作硬件——IO静态映射
linux设备驱动归纳总结(五):3.操作硬件--IO静态映射 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- 时间转换:DateTimeExtensions
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 临时更换pip国内源
源 地址 阿里云 http://mirrors.aliyun.com/pypi/simple/ 豆瓣 http://pypi.douban.com/simple/ 清华大学 https://pypi. ...
- Java小知识---Java请求一个URL。获取网站返回的数据
对url发送请求,获得返回值: public static String SendGET(String url,String param){ String result="";// ...
- (5.12)mysql高可用系列——复制中的在线切换GTID模式/增加节点/删除节点
目录 [0]需求 前提,已经假设好基于传统异步复制的主库和从库1. [0.1]传统异步切换成基于GTID的无损模式 [0.2]增加特殊要求的从库 [1]操作环境 [2]构建 复制->半同步复制 ...