lftp:
lftp需要安装客户端,命令:yum install lftp
脚本如下:$1、$2等分别代表脚本入参
#!/bin/bash
HOST=$1
PORT=22
USER=$2
PASS=$3
DDIR=/home/
LDIR=/home/
FILE=$4
echo "Starting to sftp ..."
lftp -u ${USER},${PASS} sftp://${HOST} <<EOF
cd $DDIR
lcd $LDIR
get $FILE ###这里get可以换成put(推送)、rm(删除)
bye
EOF
echo "Done!"
expect:
expect需要安装脚本语言执行解释器,命令:yum install expect
脚本如下:$argv 代表脚本入参 0 1 2..代表参数位置 参数位置从0开始,0是第一个参数
#!/usr/bin/expect
set timeout 10
set username [lindex $argv 0]
set password [lindex $argv 2]
set hostname [lindex $argv 1]
set filepath [lindex $argv 3]
set filename [lindex $argv 4]
spawn sftp $username@$hostname
expect "password:"
send "$password\n"
expect "sftp>"
send "cd $filepath\n"
expect "sftp>"
send "get $filename\n" ###这里get可以换成put(推送)、rm(删除)
expect "sftp>"
send "exit\n"
interact
lftp 命令用法参考:http://i.linuxtoy.org/docs/guide/ch42s04.html
expect 命令用法参考:https://www.cnblogs.com/lixigang/articles/4849527.html