1

时光怂恿深爱的人放手 提交于 2020-01-08 22:51:14
环境准备
准备两台Centos6服务器,1台Centos7服务器
系统: Centos6.8,Centos7.3
内存:1G
cpu: 2核
IP地址: 10.0.0.22 10.0.0.23 10.0.0.41
# 安装ftp服务,在22上操作
yum -y install vsftpd
# 启动ftp服务
service vsftpd start
访问测试页面

在这里插入图片描述

在23上操作
yum -y install lftp
lftp 10.0.0.22
#  在22上操作
cd /var/ftp/
#  创建文件
touch 1.txt
# 编辑文件
vim 1.txt 
# 随便输入点内容
hello ftp!

在这里插入图片描述

# 在23上操作
[root@ localhost ~]# lftp 10.0.0.22
lftp 10.0.0.22:~> ls
-rw-r--r--    1 0        0              11 Nov 21 04:32 1.txt
drwxr-xr-x    2 0        0            4096 Mar 22  2017 pub
# 切换文件至opt目录
lftp 10.0.0.22:/> lcd /opt
lcd ok, local cwd=/opt
# 查看路径
lftp 10.0.0.22:/> lpwd 
/opt
lftp 10.0.0.22:/> 
# 下载文件
get 1.txt 

在这里插入图片描述

# 退出当前环境
lftp 10.0.0.22:/> exit
# 进入目录查看文件是否下载过来
[root@ localhost ~]# cd /opt/
[root@ localhost opt]# ls
1.txt  rh
[root@ localhost opt]# 
[root@ localhost opt]# lftp 10.0.0.22
lftp 10.0.0.22:~> lcd /usr/local/src/
lcd ok, local cwd=/usr/local/src
lftp 10.0.0.22:~> get 1.txt 
11 bytes transferred
lftp 10.0.0.22:/> 

在开一个23的窗口

# 安装expect服务
yum -y install expect
# 创建文件夹
mkdir ftp
# 进入目录
cd ftp/
# 编辑脚本文件
vim ftp.sh
#!/usr/bin/expect
set timeout 20
spawn lftp 10.0.0.22
expect "~>" {send "lcd /backup/\r"}
expect "~>" {send "get 1.txt\r"}
expect "transferred" {send "exit\r"}
expect eof
# 添加执行权限
chmod +x ftp.sh 
# 执行脚本
./ftp.sh 

在这里插入图片描述

这种是在shell环境下执行的脚本

在这里插入图片描述

# 删除文件夹
[root@ localhost ~]# rm -rf /backup
# 执行脚本
[root@ localhost ~]# sh ftp/ftp.sh 
spawn lftp 10.0.0.22
lftp 10.0.0.22:~> lcd /backup/
lcd ok, local cwd=/backup
lftp 10.0.0.22:~> get 1.txt
11 bytes transferred          
lftp 10.0.0.22:/> exit
[root@ localhost ~]# ls /backup/
1.txt
[root@ localhost ~]# 
# 编辑脚本
vim dl.sh
# 添加如下内容
#!/bin/bash
for i in `cat iplist`
do
/usr/bin/expect << EOF
spawn ssh root@$i  $1
expect {
       "yes/no" {send "yes\r";exp_continue}
       "password:" {send "123456\r"}
}
expect eof
EOF
done
# 给脚本加执行权限
chmod +x dl.sh 
# 执行脚本,查看各个服务器root目录下的文件
./dl.sh 'ls /root/'

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

# 执行脚本并进入目录创建文件
./dl.sh 'cd /opt/ && touch wj.conf'

查看是否在其它服务器上创建文件
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!