Simple Shell Programming Sample with Expect--shutdowm GOMS in batch
Shell脚本编程详解-吐血共享

第12章 Shell 脚本编程●Shell 命令行的运行 ●编写、修改权限和执行Shell 程序的步骤 ●在Shell 程序中使用参数和变量 ●表达式比较、循环结构语句和条件结构语句 ● 在Shell 程序中使用函数和调用其他Shell 程序12-1 Shell 命令行书写规则◆ Shell 命令行的书写规则对Shell 命令行基本功能的理解有助于编写更好的Shell 程序,在执行Shell 命令时多个命令可以在一个命令行上运行,但此时要使用分号(;)分隔命令,例如:[root@localhost root]# ls a* -l;free;df长Shell 命令行可以使用反斜线字符(\)在命令行上扩充,例如: [root@localhost root]# echo “this is \>long command ” This is long command注意:“>”符号是自动产生的,而不是输入的。
12-2 编写/修改权限及执行Shell 程序的步骤◆ 编写Shell 程序◆ 执行Shell 程序Shell 程序有很多类似C 语言和其他程序设计语言的特征,但是又没有程序语言那样复杂。
Shell 程序是指放在一个文件中的一系列Linux 命令和实用程序。
在执行的时候,通过Linux 操作系统一个接一个地解释和执行每条命令。
首先,来编写第一个Shell 程序,从中学习Shell 程序的编写、修改权限、执行过程。
12-2-1 编辑Shell程序编辑一个内容如下的源程序,保存文件名为date,可将其存放在目录/bin下。
[root@localhost bin]#vi date#! /bin/shecho “Mr.$USER,Today is:”echo &date “+%B%d%A”echo “Wish you a lucky day !”注意:#! /bin/sh通知采用Bash解释。
windows shell编程指南与实例

windows shell编程指南与实例Windows Shell是Windows操作系统中的一个重要组件,它可以让用户通过命令行界面与操作系统进行交互。
本文将为读者介绍Windows Shell编程的基本知识和实例,帮助读者更好地理解和使用Windows Shell。
## 一、Windows Shell简介Windows Shell是Windows操作系统的用户界面,它提供了一种与操作系统进行交互的方式。
在Windows系统中,用户可以通过Shell来执行命令、管理文件和目录、配置系统等操作。
Windows Shell有多种版本,包括命令提示符(Command Prompt)和Windows PowerShell等。
## 二、Windows Shell编程基础### 1. 命令提示符命令提示符是Windows Shell的一种基本形式,它提供了一种简单的命令行界面,用户可以直接在命令提示符窗口中输入命令并执行。
下面是一些常用的命令提示符命令:- `dir`:显示当前目录下的文件和子目录。
- `cd`:切换当前目录。
- `mkdir`:创建新目录。
- `del`:删除文件。
- `copy`:复制文件。
### 2. Windows PowerShellWindows PowerShell是Windows操作系统的高级命令行工具,它提供了更强大和灵活的功能。
与命令提示符不同,Windows PowerShell支持使用脚本语言编写批处理脚本,可以实现更复杂的操作。
下面是一个简单的Windows PowerShell脚本示例:```$files = Get-ChildItem -Path "C:\Folder" -Recurse -File foreach ($file in $files) {if ($file.Length -gt 1024) {Write-Host "$($) is larger than 1KB."}}```上述脚本使用`Get-ChildItem`命令获取指定目录下的所有文件,并遍历每个文件,如果文件大小大于1KB,则输出文件名。
expect 用法

expect 用法
Expect是一种很有用的自动化脚本语言,它可以使用可扩展标记语
言(Expect 命令)来创建交互式脚本,用于控制远程程序,从而进行自动
化操作。
Expect以字符流的形式和被控进程进行交互。
Expect的脚本中的命
令模仿了shell的命令,如expect、send、spawn、match等。
可以通过
脚本,模拟用户的操作,以自动完成一系列的任务。
Expect的特点是可以控制不同的网络程序,甚至可以控制telnet、
ftp和rlogin等程序,以自动完成一系列的交互操作。
Expect可以接受
预定义的文本输入,并根据输入再输出预定义的文本,以此来解释用户的
命令。
Expect支持一系列的脚本语言,包括shell脚本、Perl语言、Tcl
语言以及Python等,因此可以根据需要,使用不同的脚本语言编写
Expect脚本,以实现自动化操作。
Expect也有一系列的对象,包括进程、文件及其它设备,可以将此
对象作为脚本中的变量,获取还有与设备、文件和进程交互的函数,以便
在脚本中访问这些对象。
Expect还有一些更高级的功能,包括信号处理、条件判断以及时间处理等,因此可以更加方便快捷地建立完整的自动化脚本。
linux expect syntax error

linux expect syntax error【解决Linux expect语法错误问题】简介:Linux expect是一个自动化交互脚本工具,通过模拟用户输入和对输出的响应,实现自动化任务的执行。
在使用Linux expect时,可能会遇到语法错误的问题。
本文将以解决Linux expect语法错误问题为主题,从原因分析到解决方案,一步一步回答,并提供相关案例和建议。
正文:第一步: 理解Linux expect语法错误的原因在使用Linux expect过程中,语法错误可能由多种原因引起。
常见的原因包括:1. 语法错误: 在编写expect脚本时,可能会因为拼写错误、缺少符号或使用错误的语法结构等而导致语法错误。
2. 脚本执行错误: expect脚本在执行过程中,可能会遭遇未预料到的输入或输出,从而导致语法错误。
3. 环境配置问题: Linux expect脚本依赖于正确的环境配置,如安装expect工具、设置正确的环境变量等。
如果这些配置存在问题,也可能导致语法错误。
第二步: 分析并定位语法错误一旦遇到语法错误,我们需要通过分析和定位错误来解决问题。
下面是一些常见的方法:1. 仔细检查脚本: 检查脚本中可能存在的拼写错误、符号错误以及语法结构错误等。
特别注意与引号配对使用的情况。
2. 运行脚本进行调试: 可以尝试直接运行脚本并观察错误提示信息。
根据提示信息,定位错误的具体位置。
3. 查看脚本日志: 在脚本中添加日志记录功能,以便在执行过程中输出相关信息。
通过查看日志,可以追踪错误发生的原因。
第三步: 解决语法错误一旦定位并分析了语法错误,我们可以针对具体情况采取相应的解决措施。
下面是一些常见的解决方案:1. 修正语法错误: 找到并修复脚本中存在的语法错误。
可以参考相关文档或教程提供的正确语法进行修改。
2. 调整脚本逻辑: 如果脚本在执行过程中遭遇未预料到的输入或输出,可以通过调整脚本逻辑来解决语法错误。
linux expect 用法

linux expect 用法Linux expect 是一种自动化交互式命令行工具,可以用于执行控制台交互式应用程序的自动化操作。
它可以模拟用户的输入和响应,实现自动化脚本的编写。
使用 expect 可以方便地自动化执行以下任务:1. 自动登录远程服务器或设备2. 执行 Telnet、SSH、FTP 等协议的自动化操作3. 自动化安装和配置程序4. 管理交互式应用程序的自动化操作下面是一个简单的 expect 脚本示例:```shell#!/usr/bin/expect -fset timeout 10spawn ssh username@hostnameexpect "password:"send "password123\r"expect "$ "send "ls -l\r"expect "$ "send "exit\r"expect eof```上述脚本模拟了 SSH 登录远程主机,并执行了 ls -l 命令,然后退出。
其中:- `spawn` 命令用于启动一个新的进程- `expect` 命令用于等待预期的输出或输入- `send` 命令用于发送用户输入给交互式应用程序- `timeout` 命令用于设置等待超时时间- `eof` 命令用于等待当前进程结束这只是一个简单的例子,实际使用中可能需要根据具体的场景进行更复杂的操作和处理。
注意:在运行 expect 脚本之前,需要先安装 expect 工具,可以使用以下命令进行安装:```shellsudo apt-get install expect # Debian/Ubuntusudo yum install expect # CentOS/RHEL```。
expect脚本自动应答详解

expect脚本⾃动应答详解Linux 下 expect 脚本语⾔中交互处理常⽤命令1. #!/usr/bin/expect告诉操作系统脚本⾥的代码使⽤那⼀个 shell 来执⾏。
这⾥的 expect 其实和 Linux 下的 bash、windows 下的 cmd 是⼀类东西。
注意:这⼀⾏需要在脚本的第⼀⾏,从⽽告知操作系统采⽤ expect 作为 shell 执⾏脚本。
注意:当使⽤ #!/usr/bin/expect -d 时,expect 脚本将运⾏在调试模式,届时脚本执⾏的全过程将被展⽰出来。
2. set timeout设置超时时间,计时单位是:秒,timeout -1 为永不超时。
例如:set timeout 30 为设置超时时间为 30 秒。
则当某个 expect 判断未能成功匹配的 30 秒后,将跳过该 expect 判断,执⾏后续内容。
3.spawn它主要的功能是给运⾏进程加个壳,⽤来传递交互指令。
spawn 是进⼊ expect 环境后才可以执⾏的 expect 内部命令,如果没有装 expect 或者直接在默认的 shell 下执⾏是找不到 spawn 命令的。
所以不要⽤ “which spawn“ 之类的命令去找 spawn 命令。
好⽐在 windows ⾥的 dir 就是⼀个内部命令,这个命令由 shell ⾃带,你⽆法找到⼀个 或 dir.exe 的可执⾏⽂件。
例如:spawn ssh -l username 192.168.1.1 将为 ssh -l username 192.168.1.1 加壳,届时该命令的交互指令将可以被处理。
4.expect这⾥的 expect 是 expect 的⼀个内部命令,需要在 expect 环境中执⾏。
该命令⽤于判断交互中上次输出的结果⾥是否包含某些字符串,如果有则⽴即返回。
否则如果有设置超时时间,则等待超时时长后返回。
例如:expect "password:" 为判别交互输出中是否包含 "password:" 字符串。
expect 参数
expect 参数Expect参数是一种在Linux系统中使用的命令行工具,它可以用于自动化测试、自动化部署、自动化运维等多种场景。
本文将介绍Expect参数的基本用法和常见应用场景。
Expect参数的基本用法Expect参数是基于Tcl语言的,它可以通过编写脚本来实现自动化操作。
下面是一个简单的Expect脚本示例:```#!/usr/bin/expectset timeout 10spawn ssh user@hostexpect "password:"send "mypassword\r"expect "$ "send "ls -l\r"expect "$ "send "exit\r"```这个脚本的作用是通过ssh连接到远程主机,执行ls -l命令并退出。
其中,set timeout 10表示设置超时时间为10秒,spawn sshuser@host表示启动ssh连接,expect "password:"表示等待输入密码的提示符,send "mypassword\r"表示发送密码并回车,expect "$ "表示等待命令提示符,send "ls -l\r"表示发送ls -l命令并回车,expect "$ "表示等待命令执行完毕,send "exit\r"表示退出ssh连接。
除了ssh连接,Expect参数还可以用于telnet连接、ftp上传下载、串口通信等多种场景。
需要注意的是,Expect脚本中的命令和参数都需要使用双引号括起来,否则可能会出现语法错误。
Expect参数的常见应用场景1. 自动化测试在自动化测试中,Expect参数可以用于模拟用户的操作,自动化执行测试用例。
关于expect脚本输出的问题
关于expect脚本输出的问题写了⼀个expect脚本执⾏ssh命令远程登录然后telnet另外⼀台机器⼤致如下:1 #!/usr/bin/expect -f2 set timeout 603 set port_type [lindex $argv 0]4 set p1 [lindex $argv 1]5 set p2 [lindex $argv 2]6 set p3 [lindex $argv 3]7 set p4 [lindex $argv 4]8 set p5 [lindex $argv 5]910if { $port_type eq "http" } {11 #puts "{\"retcode\":\"0\",\"retinfo\":\"in-http\"}"12 #puts "port_type=$port_type p1=$p1 p2=$p2 p3=$p3 p4=$p4 p5=$p5 p6=$p6"13 spawn ssh $p2@$p114 expect_after eof { exit 0 }1516 expect "yes/no" { send "yes\r" }17 expect "*assword:" { send "$p3\r" }1819 #spawn20 #expect "Welcome" { send "telnet $p4 $p5\r" }21 expect "*#" { send "telnet $p4 $p5\r" }22 expect "Connected to" { puts "{\"retcode\":\"0\",\"retinfo\":\"success\"}"23 exit24 }25 puts "{\"retcode\":\"-2\",\"retinfo\":\"fail\"}"2627 } elseif { $port_type eq "mysql" } {28 set p6 [lindex $argv 6]29 set p7 [lindex $argv 7]30 #puts "{\"retcode\":\"0\",\"retinfo\":\"in-mysql\"}"31 #puts "port_type=$port_type p1=$p1 p2=$p2 p3=$p3 p4=$p4 p5=$p5 p6=$p6 p7=$p7"32 spawn ssh $p2@$p133 expect_after eof { exit 0 }3435 expect "yes/no" { send "yes\r" }36 expect "*assword:" { send "$p3\r" }3738 expect "Welcome" { send "mysql -h $p4 -P $p5 -u $p6 -p $p7\r" }39 expect "#" { send "mysql -h $p4 -P $p5 -u $p6 -p $p7\r" }40 expect "$" { send "mysql -h $p4 -P $p5 -u $p6 -p $p7\r" }4142 expect "Welcome to the MySQL" {43 puts "{\"retcode\":\"0\",\"retinfo\":\"success\"}"44 exit45 }46 puts "{\"retcode\":\"-2\",\"retinfo\":\"fail\"}"47 } elseif { $port_type eq "oracle" } {48 set p6 [lindex $argv 6]49 set p7 [lindex $argv 7]50 set p8 [lindex $argv 8]51 #puts "{'retcode':0,'retinfo':'into-oracle'}"52 #puts "{\"retcode\":\"0\",\"retinfo\":\"in-mysql\"}"53 #puts "port_type=$port_type p1=$p1 p2=$p2 p3=$p3 p4=$p4 p5=$p5 p6=$p6 p7=$p7 p8=$p8"54 spawn ssh $p2@$p155 expect_after eof { exit 0 }5657 expect "yes/no" { send "yes\r" }58 expect "*assword:" { send "$p3\r" }5960 expect "Welcome" { send "sqlplus $p4/$p5@//$p6:$p7/$p8\r" }61 expect "#" { send "sqlplus $p4/$p5@//$p6:$p7/$p8\r" }62 expect "$" { send "sqlplus $p4/$p5@//$p6:$p7/$p8\r" }6364 expect "SQL*Plus" {65 puts "{\"retcode\":\"0\",\"retinfo\":\"success\"}"66 exit67 }68 puts "{\"retcode\":\"-2\",\"retinfo\":\"fail\"}"69 }70 #interact然后PHP中:$a = system('expect.sh');然后⽤xshell客户端执⾏curl -d "{\"interface\":\"test\",\"p1\":\"http\",\"p2\":\"120\",\"p3\":\"127.0.0.52\",\"p4\":\"username\",\"p5\":\"password\",\"tel_ip\":\"127.0.0.53\",\"tel_port\":\"80\"}" " /test.php"这时候xshell会显⽰所有expect执⾏步骤所以初步尝试修改成了exec('expect.sh',$a);结果虽然不会直接显⽰在xshell客户端但是还是会把所有执⾏步骤的代码输出到$a⾥⾯⽽我本来只是想把expect⾥⾯的puts “sometext”命令的输出返回给$a 后来问同事知道了可以修改成$a = exec('expect.sh');这样就完美解决问题了!!。
实验4 shell 程序设计 实验报告
实验4 shell 程序设计1.采用shell编程语言(类似C语言),完成(1)请用C语言编写程序,将1-100之间能被3或7整除的数显示出来.实验结果:student@linux-suse:~> ./homework1.sh3 6 7 9 12 14 15 18 21 24 2728 30 33 35 36 39 42 45 48 49 5154 56 57 60 63 66 69 70 72 75 7778 81 84 87 90 91 93 96 98 99end!程序代码:for ((i = 1; i <= 100; i++))doj=$[$i % 3]if test $j -eq 0thenecho -n $iecho -n " "elsej=$[$i % 7]if test $j -eq 0thenecho -n $iecho -n " "fifidoneecho "end!"(2)请用C语言编写程序,从键盘输入年号,屏幕显示是不是闰年,若是闰年则显不“YES!”否则显示“NO!”。
实验结果:student@linux-suse:~> ./homework2.sh清输入年份2012yes!student@linux-suse:~> ./homework2.sh清输入年份1995no!程序代码:echo "清输入年份read yeari=$[$year % 4]if test $i -eq 0thenecho "yes!"elsej=$[$year % 100]if test $j -eq 0thenecho "yes!"elsek=$[$year % 400]if test $k -eq 0thenecho "yes!"elseecho "no!"fififi(3)请用C语言编写程序,将从键盘录入的10个任意数,由低向高排序后显示。
expect命令基本用法
一、概述我们通过Shell可以实现简单的控制流功能,如:循环、判断等。
但是对于需要交互的场合则必须通过人工来干预,有时候我们可能会需要实现和交互程序如telnet服务器等进行交互的功能。
而Expect 就使用来实现这种功能的工具。
Expect是一个免费的编程工具语言,用来实现自动和交互式任务进行通信,而无需人的干预。
Expect 的作者Don Libes在1990年开始编写Expect时对Expect做有如下定义:Expect是一个用来实现自动交互功能的软件套件(Expect [is a] software suite for automating interactive tools)。
使用它系统管理员的可以创建脚本用来实现对命令或程序提供输入,而这些命令和程序是期望从终端(terminal)得到输入,一般来说这些输入都需要手工输入进行的。
Expect则可以根据程序的提示模拟标准输入提供给程序需要的输入来实现交互程序执行。
甚至可以实现实现简单的BBS聊天机器人。
:)Expect是不断发展的,随着时间的流逝,其功能越来越强大,已经成为系统管理员的的一个强大助手。
Expect需要Tcl编程语言的支持,要在系统上运行Expect必须首先安装Tcl。
二、Expect工作原理从最简单的层次来说,Expect的工作方式象一个通用化的Chat脚本工具。
Chat脚本最早用于UUCP 网络内,以用来实现计算机之间需要建立连接时进行特定的登录会话的自动化。
Chat脚本由一系列expect-send对组成:expect等待输出中输出特定的字符,通常是一个提示符,然后发送特定的响应。
例如下面的Chat脚本实现等待标准输出出现Login:字符串,然后发送somebody 作为用户名;然后等待Password:提示符,并发出响应sillyme。
引用:Login: somebody Password: sillyme这个脚本用来实现一个登录过程,并用特定的用户名和密码实现登录。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Simple Shell Programming Sample with Expect--shutdown GOMS in batch 【需求】 设备管理员Chen Wien 想批量远程关闭win xp机器(已有相关guide),以及linux相关机器,如GOMS,这里对GOMS (linux类server)一个简单的批处理。
如果只是关3,5台机器,批量脚本效率体现不了,如果是10,30,100台,…..,那是必须的,You know!
【方案】
Shell Overview:
Shell 在系统中的位置 Linux网络体系结构
写一个shell脚本,调用GOMS自带的expect 命令,然后把shell脚本放到其中的一个GOMS机器(假设GOMS-KK)执行。脚本先对其他GOMS进行批量关闭,通过ping 确认其他所有GOMS关闭后,再关闭GOMS-KK本身。并记录相关操作log,以备查验。
由于NetAct上面没有安装expect,脚本暂时只能在GOMS上面运行。 [OR ] 通过hit macro调用expect/shell脚本方式,log存本地PC,Anyway it is similar. 【脚本实现】 Simple workflow: 1. Clear history log : /var/log/shutgoms_bat_xxx.log firstly. 2. Modify Goms ip list to be shut down ,check each shutdown operation result 3. Check connection after shutdown to confirm again by ping. 4. If all other GOMS shut down, then at last shut down the GOMS where the shell scripts are. 5. Write the log of whole process: /var/log/shutgoms_bat_xxxx.log. There are 3 script files to be written as below: 1) shutgoms.sh (using expect) 2)scplog2netact.sh --after shutdown all goms, transfer log to netact [Optional ] 3) shutallgoms.sh (using bash), it will call shutgoms.sh & scplog2netact.sh. 1. shutgoms.sh #!/usr/bin/expect set host [lindex $argv 0] set username [lindex $argv 1] set nemupwd [lindex $argv 2] set rootpwd [lindex $argv 3] spawn ssh $username@$host expect { "(yes/no)?" { send "yes\n" } } expect { "Warning" { exec sleep 3 expect { "*assword:" { send "$nemupwd\n" } } } } expect { "*assword:" { send "$nemupwd\n" } } exec sleep 1 #already switch to target goms send "su - \n" expect { "*assword:" { send "$rootpwd\n" #halt send "shutdown -h now\n"
} } # send "exit\r" expect eof
2. scplog2netact.sh #!/usr/bin/expect set file [lindex $argv 0] set host [lindex $argv 1] set netact_ip [lindex $argv 2] set host_pwd [lindex $argv 3] spawn scp $file $host@$netact_ip:/var/log
expect { "(yes/no)?" { send "yes\n" } } expect { "*assword:" { send "$host_pwd\n" } } expect eof 3.shutallgoms.sh: -shut the goms list you want to shut #!/bin/sh ##############step1: input goms ip list to be shutdown############################## #when need add one more goms, pls add “Goms_ip,nemu_usr,nemu_pwd,root_pwd # to the below 4 variable in order with blank space to separate Goms_ip=(10.68.195.109 10.68.195.93 10.68.195.105) Nemu_usr=(Nemuadmin Nemuadmin Nemuadmin) Nemu_pwd=(nemuuser nemuuser nemuuser) Root_pwd=(root root root) ###############step2:clean history log################################ yy=`date +%Y` mm=`date +%m` dd=`date +%d` Log="/var/log/shutgoms_bat_$yy$mm$dd.log" rm -rf $Log Shut_local_goms=0 #if all other goms shutdown ok, then shutdown local machine
#check the length of 4 arrays above should be equal, else exit if [ "${#Goms_ip[@]}" -eq "${#Nemu_usr[@]}" ] && [ "${#Nemu_usr[@]}" -eq "${#Nemu_pwd[@]}" ] && [ "${#Nemu_pwd[@]}" -eq "${#Root_pwd[@]}" ];then
echo "Input ip list are matched." >>$Log else echo "Input element num of ip list array is not equal, pls modify!" >>$Log exit fi ##get local goms ip (south and north) ret=`zship` #Northbound IP: 10.68.195.106 Southbound IP: 10.68.195.105 south_ip=`echo $ret |cut -d: -f2|cut -d " " -f2` north_ip=`echo $ret |cut -d: -f3|cut -d " " -f2` echo "south ip is:$south_ip,north ip is:$north_ip" ############step3:shut down goms one by one############################## #If Goms_ip is local machine ip, overpass firstly, finally shutdown itself. for((i=0;i<${#Goms_ip[@]};i++));do if [ "${Goms_ip[i]}" == "$south_ip" ] || [ "${Goms_ip[i]}" == "$north_ip" ];then echo "This is local goms ip:No $i --${Goms_ip[i]},overpass firstly" >>$Log continue fi echo "shut down goms:No $i --${Goms_ip[i]}" >>$Log #shutdown the goms ./shutgoms.sh ${Goms_ip[i]} ${Nemu_usr[i]} ${Nemu_pwd[i]} ${Root_pwd[i]} done ###step4: confirm connection by ping after shut down##################### #If Goms_ip is local machine ip, it will overpass firstly, for goms_i in ${Goms_ip[@]}; do if [ "$goms_i" == "$south_ip" ] || [ "$goms_i" == "$north_ip" ];then echo "This is local goms ip:$goms_i,overpass firstly" >>$Log continue fi echo "ping goms:$goms_i" >>$Log #ping the goms, check the result, if some goms fail to shutdown, set Shut_local_goms=1 if getres_byping $goms_i then echo "$goms_i:ping still ok" else echo "$goms_i:ping nok now" fi done