通过 ssh 终端上传或下载文件的shell脚本

 
更多

通过 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
打赏

本文固定链接: https://www.cxy163.net/archives/3133 | 绝缘体

该日志由 绝缘体.. 于 2022年05月18日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 通过 ssh 终端上传或下载文件的shell脚本 | 绝缘体
关键字: , , , ,

通过 ssh 终端上传或下载文件的shell脚本:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter