免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4316 | 回复: 0
打印 上一主题 下一主题

[新手入门] 如何在shell脚本里使用sftp批量传送文件(转) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-20 17:21 |只看该作者 |倒序浏览

如何在shell脚本里使用sftp批量传送文件
主要步骤如下:
1.为运行shell脚本的本地用户生成密钥对
2.将其中的公钥分发到sftp欲登录的远程服务器上
3.编写并以上面的本地用户运行shell脚本
一.生成密钥对
在shell脚本中使用sftp时必须用到密钥对(公钥和私钥).可使用下列方式生成(SSH 2.X版本),这里本地用户记为:local_user:
$ ssh-keygen –d
屏幕提示:
Generating public/private dsa key pair.
Enter file in which to save the key (/home/local_user/.ssh/id_dsa):
# 按回车保存为: /home/local_user/.ssh/id_dsa,即当前用户local_user的私钥
Enter passphrase (empty for no passphrase):
# 按回车,表示读取密钥时不需要密钥的密码
Enter same passphrase again:
# 确认密钥的密码,必须和上面的输入相同
Your identification has been saved in /home/local_user/.ssh/id_dsa.
# 私钥保存信息
Your public key has been saved in /home/local_user/.ssh/id_dsa.pub.
# 公钥保存信息
The key fingerprint is:
ec:41:e8:08:38:0b:f8:1e:bc:92:98:32:fc:d7:69:7d ...
# 密钥指纹
二.分发公钥
为了使用密钥,必须将公钥分发到欲登录的远程服务器上,这里远程服务器记为remote_host,欲登录的远程用户记为remote_user
1.copy公钥到欲登录的远程服务器的远程用户的家目录下,例如:
copy id_dsa.pub到remote_host:/home/remote_user/.ssh/
若目录/home/remote_user/.ssh/不存在,请先创建之.
2.将copy来的公钥文件改名为authorized_keys
3.修改公钥文件的访问权限
chmod 644 authorized_keys
三.示例
目标:
从远程服务器remote_host:/home/remote_user/data/
传送下列文件到本地计算机的当前目录: /home/local_user/data/:
20050201
20050202
20050203
20050204
20050205
方式1: 批模式
sftp提供了一个选项-b,用于集中存放sftp命令(该选项主要用于非交互模式的sftp).因此对于上面的目标,可以生成如下的命令文件:
cd /home/remote_user/data/
lcd /home/local_user/data/
-get 20050201 .
-get 20050202 .
-get 20050203 .
-get 20050204 .
-get 20050205 .
quit
这里存为: sftp_cmds.txt
说明: get命令前加一个"-"以防止其执行错误时sftp执行过程被终止.
以下为脚本示例:
#!/bin/sh
sftp -b ./sftp_cmds.txt remote_user@remote_host
方式二:
#!/bin/sh
sftp remote_user@remote_host


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/7282/showart_501233.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP