在安装好expect 和tcl后,我们就可向各个主机中执行相同的命令了,实际上也就是执行的TCL脚本,如下就是一个TCL脚本。expect.tcl脚本逻辑:1) 先SSH到所要的执行命令的主机 ,spawn ssh $user@$hostname2) 然后在执行shell命令,脚本中执行的 是 函数exec_shell_command3)脚本中log 为执行日志函数expect.tcl
- #!/usr/local/bin/expect
-
- ###set log file handle
- set timeout 5
- set systemTime [clock seconds]
- set file "tcl.log"
- set fileid [open $file a]
- seek $fileid 0 start
-
- ###set the arguments
- set hostname [lindex $argv 0] #主机名
- set user [lindex $argv 1] #用户名
- set passwd [lindex $argv 2] #密码
-
-
- proc exec_shell_command {} { #执行命令函数
- send "rm -f xubc.log
"
- log "commands completed..."
- }
-
- proc log {msg} { #写日志函数
- global fileid
- set systemTime [clock seconds]
- puts $fileid "[clock format $systemTime -format %H:%M:%S] --- $msg"
- }
-
- log "$hostname begin....."
-
- if {$argc != 3} {
- log $argc
- log $argv
- log "Usage: error arguments.
"
- } else {
- spawn ssh $user@$hostname
- expect {
- "yes/no" { send "yes
";exp_continue }
- "password:" { send "$passwd
" }
- }
- log "Login $hostname Successfully..."
- exec_shell_command
- }
- log "exit $hostname..."
- send "exit
"
- log "$hostname end... "
- expect eof
主机名:master 用户:Hadoop 密码:123456执行脚本的命令: expect expect.tcl master hadoop 123456 再有上面的基础脚本后,对于多台服务器配置,我们可以把服务器信息的写入配置文件server.lst
- master hadoop 123456
- slave1 hadoop 123456
- slave2 hadoop 123456
- slave3 hadoop 123456
最后通过一个shell脚本,读取server.lst来完成每台机器的配置,实例中是 创建一个xubc.log的文件
expect.sh
- #!/bin/bash
-
- while read line
- do
- echo expect expect.tcl $line;
- expect expect.exp $line
- done < server.lst
sh expect.sh 将分别在每台服务器当前用户目录下创建 名为xubc.log 的文件,相关日志文件会在tcl.log中。相关阅读:Linux下Expect命令安装 http://www.linuxidc.com/Linux/2012-10/72761.htmLinux下Expect命令安装HP-UNIX的NFS配置相关资讯 Linux linux常用命令 Expect Linux Expect Expect的用法
- 常用的Linux命令 (今 08:47)
- Linux目录结构和常用命令 (08月29日)
- 一些 Linux 常用命令说明 (06月04日)
| - Linux 文件与目录的基本常用命令 (09月03日)
- Linux常用命令札记 (08月14日)
- Linux系统管理员常用命令实例分析 (04月11日)
|
本文评论 查看全部评论 (0)