Skip to content

Sftp搭建

sftp搭建

what: sftp搭建

when: 2021/9/28

who:JRT

where:anywhere

拉取镜像

docker pull emberstack/sftp

配置文件

docker-compose.yml

version: '3'
services:
  sftp:
    image: "emberstack/sftp:latest"
    ports:
      - "8082:22"
    volumes:
      - '/data/jrt/package:/home/sftp/'
      - '/data/jrt/sftp.json:/app/config/sftp.json'

sftp.json

{
    "Global": {
        "Chroot": {
            "Directory": "%h",
            "StartPath": "sftp"
        },
        "Directories": ["sftp"]
    },
    "Users": [
        {
            "Username": "sftp",
            "Password": "sftp"
        }
    ]
}

sftp启动

docker-compose -p sftp  up -d

sftp命令

root@ecs:/data/jrt# sftp
usage: sftp [-46aCfpqrv] [-B buffer_size] [-b batchfile] [-c cipher]
          [-D sftp_server_path] [-F ssh_config] [-i identity_file] [-l limit]
          [-o ssh_option] [-P port] [-R num_requests] [-S program]
          [-s subsystem | sftp_server] host
       sftp [user@]host[:file ...]
       sftp [user@]host[:dir[/]]
       sftp -b batchfile [user@]host

sftp使用

#连接主机,直接执行免去expect交互,a.sh中是需要在sftp中执行的命令, id_rsa是主机私钥
sftp -b ./a.sh  -oIdentityFile=./id_rsa  root@81.68.77.238
#上传,将本地a.txt 上传至登录sftp的目录,默认是/home/$user ,下面就是传至/home/$user/sftp
#sftp > put a.txt /sftp
#查的是远程sftp仓库的目录
#sftp > ls
#将sftp仓库进入的当前目录a.txt 下载到本地
#sftp > get a.txt

expect使用

#######################################编译expect#####################################
#利用sftp传输脚本生成的包
yum install vim wget -y
yum install glibc-headers gcc-c++ make unzip -y
cd /opt
#编译tcl,expect编译依赖tcl
wget  http://core.tcl.tk/tcl/zip/release/tcl.zip
unzip tcl.zip
cd ./tcl/unix
./configure && make -j4 && make install
cd /opt
#编译expect,expect提供了shell中的人机交互
wget https://jaist.dl.sourceforge.net/project/expect/Expect/5.45.3/expect5.45.3.tar.gz
tar -xzvf expect5.45.3.tar.gz && cd expect5.45.3/
./configure --build=arm-linux && make -j4 && make install

#安装sftp,传输需要
yum install openssh-clients -y

cd /root/rpmbuild/RPMS
##############################################使用#################################
#利用expect工具和sftp协议进行传输编译的包
#/usr/local/bin/expect说的是expect安装的位置,可以用which查,
/usr/local/bin/expect <<-EOF
set timeout -1
spawn sftp -oPort=$port  $user@$ip
expect {
"yes/no" { send "yes\r"; exp_continue }
"Password:" { send "$passwd\r"; exp_continue }
"sftp" { send "put -r  ./$var2/ $addr\r" }
}
send "exit\r"
expect eof
EOF

参考地址

https://registry.hub.docker.com/r/emberstack/sftp