Centos 7.6 安装expect,使用expect远程登录,并异地多机房备份数据库
2020-05-13 17:42
92 查看
检查是否安装expect
[root@VM_32_11_centos ~]# rpm -qa|grep expect [root@VM_32_11_centos ~]#
没有任何返回,就说明没有安装expect
安装expect
[root@VM_32_11_centos ~]# yum install expect Loaded plugins: fastestmirror, langpacks Repository epel is listed more than once in the configuration Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast epel | 4.7 kB 00:00:00 extras | 2.9 kB 00:00:00 os | 3.6 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/7): epel/7/x86_64/group_gz | 95 kB 00:00:00 (2/7): epel/7/x86_64/updateinfo | 1.0 MB 00:00:00 (3/7): os/7/x86_64/group_gz | 153 kB 00:00:00 (4/7): updates/7/x86_64/primary_db | 176 kB 00:00:00 (5/7): extras/7/x86_64/primary_db | 190 kB 00:00:00 (6/7): os/7/x86_64/primary_db | 6.1 MB 00:00:01 (7/7): epel/7/x86_64/primary_db | 6.8 MB 00:00:01 Determining fastest mirrors Resolving Dependencies --> Running transaction check ---> Package expect.x86_64 0:5.45-14.el7_1 will be installed --> Processing Dependency: libtcl8.5.so()(64bit) for package: expect-5.45-14.el7_1.x86_64 --> Running transaction check ---> Package tcl.x86_64 1:8.5.13-8.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ======================================================================================================================================================================= Package Arch Version Repository Size ======================================================================================================================================================================= Installing: expect x86_64 5.45-14.el7_1 os 262 k Installing for dependencies: tcl x86_64 1:8.5.13-8.el7 os 1.9 M Transaction Summary ======================================================================================================================================================================= Install 1 Package (+1 Dependent package) Total download size: 2.1 M Installed size: 4.9 M Is this ok [y/d/N]: y Downloading packages: (1/2): expect-5.45-14.el7_1.x86_64.rpm | 262 kB 00:00:00 (2/2): tcl-8.5.13-8.el7.x86_64.rpm | 1.9 MB 00:00:00 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 2.8 MB/s | 2.1 MB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 1:tcl-8.5.13-8.el7.x86_64 1/2 Installing : expect-5.45-14.el7_1.x86_64 2/2 Verifying : 1:tcl-8.5.13-8.el7.x86_64 1/2 Verifying : expect-5.45-14.el7_1.x86_64 2/2 Installed: expect.x86_64 0:5.45-14.el7_1 Dependency Installed: tcl.x86_64 1:8.5.13-8.el7 Complete! [root@VM_32_11_centos ~]#
中途输入一下y 回车。
最后看到Complete! 就说明已经安装完成。
可以使用命令检查一下:
[root@VM_32_11_centos ~]# rpm -qa|grep expect expect-5.45-14.el7_1.x86_64 [root@VM_32_11_centos ~]#
expect 示例程序
程序用途:远程登录数据库服务器备份数据库,并将备份文件二次备份到其他服务器。
准备三台机器:
192.168.1.100 安装了expect
192.168.1.101 安装了mysql 数据库
192.168.1.102 用于存放备份数据文件
远程登录192.168.1.100 服务器,安装expect 工具(上面的操作步骤即可)。
创建脚本文件:
[root@VM_32_11_centos ~]# touch expect_test.sh [root@VM_32_11_centos ~]# [root@VM_32_11_centos ~]# vim expect_test.sh
输入如下代码:
#!/usr/bin/expect set ip 192.168.1.101 set user mysql set pass password set timeout 600 set time [exec date "+%Y%m%d_%H%M%S"] set path [lrange $argv 0 0] spawn ssh $user@$ip expect { "(yes/no)" {send "yes\r"; exp_continue} "password:" {send "$pass\r"} } set db_username username set db_password password set db_database test_db expect "mysql@*" { send "/home/mysql/bin/mysqldump -h127.0.0.1 -P3306 -u$db_username -p$db_password $db_database -S /home/mysql/mysql.sock | gzip > $path/db_back_$db_database_$time.sql.gz\r" } set remote_ip 192.168.1.102 set remote_user user set remote_password password expect "mysql@*" { send "scp $path/db_back_$db_database_$time.sql.gz $remote_user@$remote_ip:/home/$remote_user/db/back/db_back_$db_database_$time.sql.gz\r" } expect { "(yes/no)" {send "yes\r"; exp_continue} "password:" {send "$remote_password\r"} } expect "mysql@*" {send "exit\r"} exit
最后保存退出,然后就可以执行命令:
expect expect_test.sh /local/path
说明:这里的/local/path 根据实际情况指定,程序中表示备份文件的存储路径。
相关文章推荐
- centos 安装 mysql + 中文乱码 + 远程登录 + 数据库备份和恢复
- 使用Xmanager - Xbrowser打开远程最小化方式安装的CentOS 7.6的图形化界面
- centos7下使用yum安装mysql并创建用户,数据库以及设置远程访问
- [转]Centos6.5使用yum安装mysql—配置MySQL允许远程登录
- centos 6.5 下 安装 mysql 并实现远程登录使用
- CentOS安装使用vnc进行远程桌面登录
- 不安装 oracle的客户,就可以使用pl/sql访问远程oracle 数据库的方法
- 用Instant Client,不安装oracle客户端使用sqlplus连接远程数据库
- CentOS 6.4 升级 Mysq5.5l方法 和 用户远程登录数据库
- 使用ssh工具远程登录centos中文乱码问题
- paramiko模块安装和使用(远程登录服务器)
- Linux中使用expect脚本实现远程机器自动登录
- 不安装Oracle客户端情况下使用PL/SQL 远程连接数据库
- 利用oracle instant client,不安装客户端使用plsql远程连接数据库
- 使用批处理文件异地备份数据库(最近几天的数据)
- 远程登录:Linux下 telnet 服务的安装与使用
- 【原创】centos6.5安装vnc-server实现windows/linux下的图形化远程登录
- CentOS6 图形界面(gnome)安装,使用vnc进行远程连接
- 本机不安装Oracle客户端,使用PL/SQL Developer连接远程数据库
- 使用expect实现ssh不输入密码登录远程服务器