utils/sshscp.sh

本文介绍了一个使用Bash脚本实现的SCP命令工具,该工具可通过expect进行交互式认证,支持递归复制等功能。文章详细展示了如何解析命令行参数、处理异常情况以及与远程主机建立连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#! /bin/bash

exelocal=1

sshcmd_comm()
{
	#参数赋值,注意这里有加双引号
	srccommand="$1"
	descommand="$2"
	password="$3"
	r_option="$4"
	timeout=180
	#判断srccommand或者descommand 是否为null
	if [ "x$srccommand" = "x" -o "x$descommand" = "x" ]; then
		echo "wrong parameter"
		exit 1
	fi
	#这个地方应该是写错了吧,数字判断是否相等应该用==
	if [ $r_option = 1 ]; then
		r_option="-r"
	else
		r_option=""
	fi
	#通过expect 执行交互命令
	expect <<-END1
		set timeout -1
		spawn scp -o "ConnectTimeout ${timeout}" ${r_option} $srccommand $descommand
		expect {
			"Are you sure you want to continue connecting (yes/no)?" {
				send "yes\r"
				expect -re "\[P|p]assword:"
				send "${password}\r"
			}
			
			-re "\[P|p]assword:" {
				send "${password}\r"
			}
			timeout {
				send_user "connection to $targetip timed out: \$expect_out(buffer)\n"
				exit 13
			}
				
			eof {
			#捕获ssh 远程执行的命令的返回的结果
				catch wait result
				exit [lindex \$result 3]
			}
		}
		expect {
			eof {
                                catch wait result
                                exit [lindex \$result 3]
                        }
			
			-re "\[P|p]assword:" {
				send_user "invalid password or account. \$expect_out(buffer)\n"
				exit 13
			}
			
			timeout {
				send_user "connection to $targetip time out: \$expect_out(buffer)\n"
				exit 13
			}
		}
	END1
	return $?
}

usage()
{
	echo "Usage: sshscp.sh -s "src" -d "des" [-p login_password] [-r]"
}

delete_known_hosts()
{
#往known_hosts 中写空文件的方式来删除
	known_hosts=/root/.ssh/known_hosts
        sudo > ${known_hosts}
}

src=
des=
loginpassword=
r_option=0
#parse 调用这个脚本时的可选参数
while getopts "u:p:s:d:hr" OPTIONS
do
	case $OPTIONS in
		p) loginpassword="$OPTARG";;
		s) src="$OPTARG";;
		d) des="$OPTARG";;
		r) r_option=1;;
		h) usage;exit 1;;
		\?) echo "ERROR - Invalid Parameter"; echo "ERROR - Invalid parameter" >&2; usage; exit 1;;
		*) echo "ERROR - Invalid Parameter"; echo "ERROR - Invalid parameter" >&2; usage; exit 1;;
	esac
done
#判断字符串是否为null
if [ "x$src" = "x" -o "x$des" = "x" ]; then
	echo "$src  $des"
	usage
	exit 1
fi
#方式ssh 时失败,因此提示清空/root/.ssh/known_hosts 文件
delete_known_hosts
#调用前面的定义的函数,执行scp 命令
sshcmd_comm "$src" "$des" "$loginpassword" "${r_option}"
#返回sshcmd_comm 执行的结果
exit $?

[2025-07-31 02:11:27] [INFO] 尝试 1/5: 直接下载镜像 quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 Error: Get "https://quay.mirrors.ustc.edu.cn/v2/": dial tcp: lookup quay.mirrors.ustc.edu.cn on 223.5.5.5:53: no such host [2025-07-31 02:11:30] [INFO] 尝试 2/5: 直接下载镜像 quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 Error: Get "https://quay.mirrors.ustc.edu.cn/v2/": dial tcp: lookup quay.mirrors.ustc.edu.cn on 223.5.5.5:53: no such host [2025-07-31 02:11:37] [INFO] 尝试 3/5: 直接下载镜像 quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 Error: Get "https://quay.mirrors.ustc.edu.cn/v2/": dial tcp: lookup quay.mirrors.ustc.edu.cn on 223.5.5.5:53: no such host [2025-07-31 02:11:46] [INFO] 尝试 4/5: 直接下载镜像 quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 Error: Get "https://quay.mirrors.ustc.edu.cn/v2/": dial tcp: lookup quay.mirrors.ustc.edu.cn on 223.5.5.5:53: no such host [2025-07-31 02:11:59] [INFO] 尝试 5/5: 直接下载镜像 quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 Error: Get "https://quay.mirrors.ustc.edu.cn/v2/": dial tcp: lookup quay.mirrors.ustc.edu.cn on 223.5.5.5:53: no such host [2025-07-31 02:12:14] [ERROR] 直接下载镜像 quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 失败 (重试 5 次) [2025-07-31 02:12:14] [ERROR] 镜像下载失败: quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 [2025-07-31 02:12:14] [WARN] 镜像下载失败: quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 [2025-07-31 02:12:14] [INFO] 使用 crane 直接下载镜像: docker.io/flannelcni/flannel:v0.22.0 [2025-07-31 02:12:14] [INFO] 尝试 1/5: 直接下载镜像 docker.io/flannelcni/flannel:v0.22.0 Error: Get "https://index.docker.io/v2/": dial tcp 128.121.146.228:443: connect: connection refused [2025-07-31 02:12:38] [INFO] 尝试 2/5: 直接下载镜像 docker.io/flannelcni/flannel:v0.22.0 Error: Get "https://index.docker.io/v2/": dial tcp 128.121.146.228:443: connect: connection refused [2025-07-31 02:13:06] [INFO] 尝试 3/5: 直接下载镜像 docker.io/flannelcni/flannel:v0.22.0 Error: Get "https://index.docker.io/v2/": dial tcp 31.13.87.19:443: connect: connection refused [2025-07-31 02:13:36] [INFO] 尝试 4/5: 直接下载镜像 docker.io/flannelcni/flannel:v0.22.0 Error: Get "https://index.docker.io/v2/": dial tcp 31.13.87.19:443: connect: connection refused [2025-07-31 02:14:09] [INFO] 尝试 5/5: 直接下载镜像 docker.io/flannelcni/flannel:v0.22.0 Error: Get "https://index.docker.io/v2/": dial tcp 69.63.178.13:443: connect: connection refused [2025-07-31 02:14:45] [ERROR] 直接下载镜像 docker.io/flannelcni/flannel:v0.22.0 失败 (重试 5 次) [2025-07-31 02:14:46] [ERROR] 镜像下载失败: docker.io/flannelcni/flannel:v0.22.0 [2025-07-31 02:14:46] [WARN] 镜像下载失败: docker.io/flannelcni/flannel:v0.22.0 [2025-07-31 02:14:46] [WARN] 以下镜像下载失败: [2025-07-31 02:14:46] [WARN] - quay.mirrors.ustc.edu.cn/coreos/flannel:v0.22.0 [2025-07-31 02:14:46] [WARN] - docker.io/flannelcni/flannel:v0.22.0 [2025-07-31 02:14:46] [WARN] 请手动下载或检查网络连接 [2025-07-31 02:14:46] [INFO] ===== 下载系统优化工具 ===== [2025-07-31 02:14:46] [INFO] 工具已存在,跳过下载: cheat-linux-amd64.gz [2025-07-31 02:14:46] [INFO] 工具已存在,跳过下载: btop-x86_64-linux-musl.tbz [2025-07-31 02:14:46] [INFO] 工具已存在,跳过下载: bottom_x86_64-unknown-linux-gnu.tar.gz [2025-07-31 02:14:46] [INFO] 工具已存在,跳过下载: fd-v8.7.1-x86_64-unknown-linux-gnu.tar.gz [2025-07-31 02:14:46] [INFO] 尝试 1/5: 下载 bat-v0.24.0-x86极-unknown-linux-gnu.tar.gz #=O#- # # curl: (35) OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to ghproxy.com:443 根据错误重新生成完整、有效、可用的下载脚本、离线部署脚本、服务器系统优化脚本,并且包含系统依赖脚本,脚本要求可以重复执行。
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值