使用 shell 脚本操作 SFTP 文件 [英] SFTP file manipulation using shell script

查看:299
本文介绍了使用 shell 脚本操作 SFTP 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人尝试使用 shell 脚本访问和操作 SFTP 服务器中的文件基本上,这是我必须做的:1.打开SFTP,访问指定目录2. 遍历所有 .txt 文件3.读取每个.txt文件的文件名,只获取包含/s 'XX'子字符串的文件(即 hello-XX.txt)4. 通过附加-OK"字符串重命名该文件(即hello-XX-OK.txt)

Has anyone tried to access and manipulate files in an SFTP server using shell script Basically, here's what I have to do: 1. Open SFTP, access the designated directory 2. Loop through all .txt files 3. Read filename of each .txt files and only get the file/s that contain/s 'XX' substring (i.e. hello-XX.txt) 4. Rename that file by appending an '-OK' string (i.e.hello-XX-OK.txt)

感谢您的投入.

推荐答案

这是我为自己的目的而工作的起点.我正在使用 sshpass 以便可以指定密码,但如果您正确交换了 RSA 公钥,您应该能够删除额外的 goo.

Here's a starting point that I got working for my own purposes. I'm using sshpass so that the password can be specified but you should be able to remove that extra goo if you have the RSA public keys properly exchanged.

#!/bin/bash

function sftpexec()
{
    (SSHPASS="password" sshpass -e sftp user@server | tail -n +2) << !
$1
!
}

FILES=$(sftpexec ls)
echo $FILES

这将在起始目录中列出服务器上的所有文件.

What this will do is list all the files on the server in the starting directory.

这篇关于使用 shell 脚本操作 SFTP 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆