在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结合使用的更多相关文章

  1. Shell结合Expect实现自动输入密码

    Shell结合Expect自动输入密码示例 #!/bin/bash cd /data/live /usr/bin/expect <<-EOF spawn git clone "s ...

  2. shell中expect介绍

    expect介绍 借助Expect处理交互的命令,可以将交互 过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成.尤其适用于需 要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员 ...

  3. Linux命令(27):shell 结合expect,多服务器批量分发数据

    shell 结合expect 写的批量scp脚本工具 except安装:http://www.cnblogs.com/lovychen/p/6525623.html expect用于自动化地执行lin ...

  4. shell 套用expect

    先用if 判断expect有没有安装 没有就yum install expect #!/bin/bash passwd='123456' /usr/bin/expect <<-EOF se ...

  5. Ansible 命令相关模块command, shell, raw, expect, script, telnet[转]

    本文主要介绍Ansible的几个命令模块,包括: command - 在远程节点上执行命令 shell - 让远程主机在shell进程下执行命令 script - 将本地script传送到远程主机之后 ...

  6. shell中expect免交互

    expect前言观察ssh登录的交互现象有些程序难以避免的需要交互.你该如何解决脚本与程序的交互问题?名词解释期待, 预期, 盼望, 预料,料想, 指望, 希望, 要求,想, 认为一.概述 我们通过S ...

  7. Shell 信号处理 & Expect 免交互

    监控脚本项目 信号处理 1 什么是信号 由键盘组合键或者 kill 命令发出操作称之为信号 信号是发送给进程的,进程在收到信号后会作出默认的响应 2 为何要在进程内处理信号 进程在收到信号后会有默认的 ...

  8. shell脚本 expect 实现自动登陆

    vi auto_ssh.exp #!/usr/bin/expect   set ipaddress "123.227.159.159" set passwd "你的密码& ...

  9. Shell之expect的测试

    测试:./sshLogin.sh Slave1 caipeichao 1qaz@WSX hadoop lk198981 HadoopCluster #!/usr/bin/expect -f #auto ...

随机推荐

  1. Day 38 Semaphore ,Event ,队列

    什么是信号量(multiprocess.Semaphore) 互斥锁同时只允许一个线程更改数据,而信号量semaphore是同时允许一定数量的线程更改数据. 假设商场里有4个迷你唱吧 ,所以通过同时可 ...

  2. django 模型对象的 update() get_or_create() 的使用

    update() 如果一个查询集是一个列表对象, 需要更新该列表对象里所有的单个数据集的数据,可以使用update()方法,而不须遍历整个查询集对象一个个逐一进行修改 obj_list = UserI ...

  3. HDU 6205(尺取法)2017 ACM/ICPC Asia Regional Shenyang Online

    题目链接 emmmm...思路是群里群巨聊天讲这题是用尺取法.....emmm然后就没难度了,不过时间上3000多,有点.....盗了个低配本的读入挂发现就降到2800左右, 翻了下,发现神犇Clar ...

  4. Google Guava 类库简介

    Guava 是一个 Google开发的 基于java的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency librar ...

  5. debug 工具

    git blame 查看某个文件的修改记录  二分查找确定 bug 来源 启动  输入 git bisect start,启动流程 输入 git bisect bad,标记当前是错误的 输入 gi ...

  6. (转)Python - 字符串对齐

    https://zhuanlan.zhihu.com/p/33923344-----------Python小知识:用format格式化输出字符串 版权声明:本文为博主原创文章,未经博主允许不得转载. ...

  7. (转)mysql5.7 根据二进制文件mysqlbinlog恢复数据库 Linux

    原文:http://blog.csdn.net/qq_15058425/article/details/61196085 1.开始mysqlbinlog日志功能 先找打my.cnf文件的位置: 2.编 ...

  8. 批处理 安装、卸载 window service

    注意:安装中调用了卸载的bat,安装之前先卸载...PS:可以删除 安装代码: @echo off set filename=LXServer.exe set servicename=Service1 ...

  9. Ajax关于readyState和status的讨论

    熟悉web开发的程序员想必对Ajax也不会陌生.现在已经有很多js框架封装了ajax实现,例如JQuery的ajax函数,调用起来非常方便.当然本文不打算讲框架的使用,我们将从Ajax的javascr ...

  10. 在windows里安装系统7、8、10或Offcie或Visio等推荐的激活工具

    不多说,直接上干货! (1)激活Windows或者Office前,你务必先进去KMSAuto Net的System界面,安装KMS-host Service; (2)然后回到Main主界面,选择Act ...