May
18
2022
通过 ssh 终端上传或下载文件的shell脚本
作者:
绝缘体.. 发布:
2022-05-18 13:00 分类:
未分类 阅读:
抢沙发
通过 ssh 终端上传或下载文件的shell脚本。
#!/bin/bash
:<<!
DESC: 上传或下载文件
本脚本顺利执行的前提是已经在服务器配置好了公钥并在本地配置好了私钥的情况下
AUTHOR: www.phpernote.com
!
#up:上传;down:下载;
action=$1
#需要上传的本地文件位置
localFilePath=$2
#下载文件的保存位置
localSavePath=$3
#下载文件的默认保存位置
localSaveDefaultPath=${HOME}
#需要下载的远程文件位置
remoteFilePath=$2
#上传文件的在服务器上的保存位置
remoteSavePath=$3
#上传文件的在服务器上的默认保存位置
remoteSaveDefaultPath="/home/yhm/"
#服务器IP地址
serverIp="123.56.71.70"
#服务器端口
serverPort="22"
if [ -z "$action" ]; then
echo "请选择操作:[ 1 ] 上传文件 [ 2 ] 下载文件"
read actionSelect
if [ "$actionSelect" == "1" ]; then
action="up"
echo
read -p "请输入要上传的本地文件地址:"
if [ ! -f "$REPLY" ]; then
echo "您输入的本地文件 $REPLY 不存在!!!"
exit
else
localFilePath="$REPLY"
read -p "请输入保存到远程服务器的目录位置,默认($remoteSaveDefaultPath):"
remoteSavePath="$REPLY"
fi
elif [ "$actionSelect" == "2" ]; then
action="down"
read -p "请输入要下载的服务器文件地址:"
if [ -z "$REPLY" ]; then
echo "要下载的服务器文件地址不能为空"
exit
else
remoteFilePath=$REPLY
read -p "请输入保存到的目录位置,默认($localSaveDefaultPath):"
localSavePath="$REPLY"
fi
fi
fi
if [ "$action" == "up" ]; then
if [ -z $remoteSavePath ]; then
remoteSavePath="$remoteSaveDefaultPath"
fi
#上传文件
scp -P "$serverPort" -r "$localFilePath" root@"$serverIp":"$remoteSavePath"
elif [ "$action" == "down" ]; then
if [ -z $localSavePath ]; then
localSavePath="$localSaveDefaultPath"
fi
#下载文件
scp -P "$serverPort" root@"$serverIp":"$remoteFilePath" "$localSavePath"
else
echo "命令输入错误,程序退出!!!"
exit
fi
微信扫一扫,打赏作者吧~