

在linux系统使用expect命令自动填充ssh或scp等命令的密码
source link: https://blog.whuzfb.cn/blog/2021/01/16/linux_expect/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

expect命令使用范例
shell脚本文件的名称为download.sh
,内容如下:
#!/usr/bin/env bash
# 任何一句命令执行出错,都会终止该脚本
set -e
# 设置为调试模式,在执行过程中会同时输出各变量的值
# set -x
# 连接scp
#######################
# expect {
# -re ".*es.*o.*" {
# exp_send "yes\r"
# exp_continue
# }
# -re ".*sword.*" {
# exp_send "mypwd12345\r"
# }
# }
# 把远程机器上的文件下载到本地机器
# 参数1: 远程机器上的文件(或文件夹)的绝对路径
# 参数2: 存放位置,即本地机器上的文件夹
# 当scp下载大文件时,需要耗费更多时间,设置timeout
function scp_download(){
src=$1
dst=$2
if [ $# -eq 2 ]
then
need_ssh=0
else
need_ssh=$3
fi
name=zfb
host="192.168.1.56"
passwd="1234567890pwd"
if [ $3 -eq 1 ]
then
# ssh登录远程机器,为目标文件添加读写执行权限
/usr/bin/expect<<EOF
spawn ssh $name@$host
expect {
-timeout -1
"*yes/no" {
send "yes\r";exp_continue
}
"*password:" {
send "$passwd\r"
# zfb@my-Server:~$
expect "~.*$"
send "sudo chmod 777 $src\r"
expect "*assword:"
send "$passwd\r"
expect "~.*$"
send "exit\r"
expect eof
}
}
EOF
fi
# 使用scp自动下载文件
/usr/bin/expect<<EOF
set timeout 600
spawn scp -r $name@$host:$src $dst
expect {
"*yes/no" {
send "yes\r";exp_continue
}
"*password:" {
send "$passwd\r"
expect eof
}
}
EOF
}
# 脚本运行参数
# 参数1: 远程机器的文件(夹)绝对路径, 例如 /media/data/test/a.txt
# 参数2:保存位置,即本地路径
# 若本地不存在,则创建目录
if [ ! -d $2 ]
then
echo $2" does not exist, ready to mkdir "$2
mkdir -p $2
echo "mkdir successfully!"
fi
data=$2/opendata
if [ ! -d $data ]
then
echo $data" does not exist, ready to mkdir "$data
mkdir -p $data
echo "mkdir successfully!"
fi
scp_download $1/test.txt $2
scp_download $1/test/testaa.py $2
mv $2/testaa.py $2/testbb.py
scp_download $1/test/opendata/testcc.txt $data
# testdd.txt在远程机器上,无读取权限,需要ssh进行chmod添加权限
scp_download $1/test/opendata/testdd.txt $data 1
Recommend
-
52
1. 前言 我们在设计数据库的时候一定会带上新增、更新的时间、操作者等审计信息。 之所以带这些信息是因为假如有一天公司的数...
-
9
在进行持久层数据维护(新增或修改)的时候,我们通常需要记录一些非业务字段,比如:create_time、update_time、update_by、create_by等用来维护数据记录的创建时间、修改时间、修改人、创建人等信息。通常情况下我们需要对这些字段进行手动赋值。...
-
11
Safari浏览器自动填充不能使用怎么办?MacW教程MacW每天为大家分享软件教程和技巧。
-
12
Monolog 自动填充 RequestId / TraceId 请求链路追踪标识在记录业务日志时,我们经常需要为每一条日志都补充上类似 RequestId 类的唯一标识串,方便后期快速的...
-
8
解决浏览器保存密码自动填充问题时间: 11/23/2019作者: frontend-联合编辑浏览量: 7621. 问题描述话说有一天,我如往常一样打开我的开发网站进行登录操作。浏览器很平常的在我们进行登录操...
-
2
V2EX › 浏览器 关于谷歌浏览器的自动填充的问题 YaakovZiv · 13 小时 38 分钟前 · 894...
-
5
我们在日常开发中经常使用ORM框架,比如Mybatis、tk.Mybatis、Mybatis-Plus。不过最广泛的还是Mybatis-Plus,我们的一些表,都会有创建时间、更新时间、创建人、更新人。这些我们每次都要自己手动set吃力不讨好,所以Mybatis-Plus带来了自动填充,今天小编带大家一起...
-
11
by zhangxinxu from https://www.zhangxinxu.com/wordpress/?p=10525 鑫空间-鑫生活 本文欢迎分享与聚合,全文转载...
-
11
shell脚本中实现自动输入密码---expect安装使用 精选 原创 雍州无名 2022-11-23 1...
-
16
V2EX › 问与答 win 平台 1password 只支持填充网页密码?
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK