shell与expect结合使用
在linux操作系统下,使用脚本自动化,一般由两种方案,方案一:telnet+ftp,方案二:ssh+scp+expect。
以下主要使用ssh+scp+expect为例进行说明使用方式。
第一步:安装expect:yum -y install expect
第二步:验证,执行expect是否正确
第三步:编写脚本
ssh_exec(){
ip=$
user=$
passwd=$
cmdstr=$
/usr/bin/expect <<EOF
set time
spawn ssh $user@$
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password:" {send "$passwd\r"}
}
expect "*#"
expect "*#"
send "$cmdstr\r"
expect "*#"
send "exit\r"
expect eof
EOF
}
function scp_get () {
local ip=$
local user=$
local passwd=$
local src=$
local dst=$
[ -z "$ip" -o -z "$passwd" ] && return
/usr/bin/expect << EOF
proc remote_exec {ip passwd src dst} {
spawn scp \$user@\$ip:\$src \$dst
exp_internal
expect {
"yes/no" { send "yes\\r";exp_continue}
"*password:" {send "\$passwd\\r"}
}
expect eof
}
remote_exec "$ip" "$user" "$passwd" "$src" "$dst"
EOF
}
#从本地服务器复制到远程服务器
function scp_put () {
local ip=$
local user=$
local passwd=$
local localfile=$
local dst=$
[ -z "$ip" -o -z "$passwd" ] && return
/usr/bin/expect << EOF
proc remote_exec {ip passwd localfile dst} {
spawn scp \$localfile \$user@\$ip:\$dst
exp_internal
expect {
"yes/no" { send "yes\\r";exp_continue}
"*password:" {send "\$passwd\\r"}
}
expect eof
}
remote_exec "$ip" "$user" "$passwd" "$localfile" "$dst"
EOF
}
ssh_exec 192.168.1.2 root 'df -h'
scp_get 192.168.1.2 root '/root/test.txt' '/opt/'
代码说明:
第四步:对脚本授权,执行:chmod -R 755 script.sh
第五步:脚本执行,./script.sh (备注:shell+expect脚本,不能使用sh script.sh执行,只能采用./script.sh执行)
shell与expect结合使用的更多相关文章
- Shell结合Expect实现自动输入密码
Shell结合Expect自动输入密码示例 #!/bin/bash cd /data/live /usr/bin/expect <<-EOF spawn git clone "s ...
- shell中expect介绍
expect介绍 借助Expect处理交互的命令,可以将交互 过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成.尤其适用于需 要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员 ...
- Linux命令(27):shell 结合expect,多服务器批量分发数据
shell 结合expect 写的批量scp脚本工具 except安装:http://www.cnblogs.com/lovychen/p/6525623.html expect用于自动化地执行lin ...
- shell 套用expect
先用if 判断expect有没有安装 没有就yum install expect #!/bin/bash passwd='123456' /usr/bin/expect <<-EOF se ...
- Ansible 命令相关模块command, shell, raw, expect, script, telnet[转]
本文主要介绍Ansible的几个命令模块,包括: command - 在远程节点上执行命令 shell - 让远程主机在shell进程下执行命令 script - 将本地script传送到远程主机之后 ...
- shell中expect免交互
expect前言观察ssh登录的交互现象有些程序难以避免的需要交互.你该如何解决脚本与程序的交互问题?名词解释期待, 预期, 盼望, 预料,料想, 指望, 希望, 要求,想, 认为一.概述 我们通过S ...
- Shell 信号处理 & Expect 免交互
监控脚本项目 信号处理 1 什么是信号 由键盘组合键或者 kill 命令发出操作称之为信号 信号是发送给进程的,进程在收到信号后会作出默认的响应 2 为何要在进程内处理信号 进程在收到信号后会有默认的 ...
- shell脚本 expect 实现自动登陆
vi auto_ssh.exp #!/usr/bin/expect set ipaddress "123.227.159.159" set passwd "你的密码& ...
- Shell之expect的测试
测试:./sshLogin.sh Slave1 caipeichao 1qaz@WSX hadoop lk198981 HadoopCluster #!/usr/bin/expect -f #auto ...
随机推荐
- Cordova - 彻底搞定安卓中的微信支付插件!
Cordova:8.0.0 Android studio:3.2.1 cordova-plugin-adam-wechat : 3.0.6 你看到这个标题肯定会惊讶,一个Cordova的微信支付插件, ...
- git修改文件权限方式
查看Repository中文件权限 git ls-tree HEAD 100644 blob 018321abfbff52d175a788597f5b5f3f17f67dc7 .gitignore 1 ...
- python里面如何拷贝一个对象?deepcopy 和 copy 有什么区别 ?
深拷贝就是说原内容改变但是拷贝的性内容不会改变,copy.copy和deepcopy对一个不可变类型进行拷贝, name结果相同都是浅拷贝指向引用如果是可变的话, 即使元组在最外层, 那么deepco ...
- redis cluster 添加 删除 重分配 节点
redis cluster配置好,并运行一段时间后,我们想添加节点,或者删除节点,该怎么办呢. 一,redis cluster命令 //集群(cluster) CLUSTER INFO 打印集群的信 ...
- pythonweb框架Flask学习笔记02-一个简单的小程序
#-*- coding:utf-8 -*- #导入了Flask类 这个类的实例将会是我们的WSGI应用程序 from flask import Flask #创建一个Flask类的实例 第一个参数是应 ...
- POJ 1102
#include<iostream>// cheng da cai zi 11.14 using namespace std; int main() { int i; int j; int ...
- ElasticSearch-SQL 安装和使用
ES上线之后,用lucene语法来查询数据,学习成本略高,所以考虑用es-sql来简化这部分的操作. ES版本:5.4.0,节点部署如下: master node:3 client node:2,po ...
- Oracle中的层次查询详解
1 语法格式 select [level], column, expr... from table [where condition] start with condition connect by ...
- tomcat启动(二)org.apache.catalina.startup.Bootstrap分析
/** * Bootstrap loader for Catalina. This application constructs a class loader * for use in loading ...
- C++11中多线程库
一.Linux 线程API 线程是在操作系统层面支持的,所以多线程的学习建议还是先找一本linux系统编程类的书,了解linux提供线程的API,了解使用线程设计程序的基本操纵.完全使用系统调用编写多 ...