在shell脚本中使用expect

2017-9-28 liyangweb.com 李杨 服务器

expect是一个用来处理交互的命令。借助Expect,我们可以将交互过程写在一个脚本上,使之自动化完成。形象的说,ssh登录,ftp登录等都符合交互的定义。

expect有四个关键命令,作用如下:

  • send:用于向进程发送字符串
  • expect:从进程接收字符串
  • spawn:启动新的进程
  • interact:允许用户交互

关于四个命令的详细用法,这里不做过多介绍,网上资料很多,大家可以自行搜索,下面只列举一个最基本的用法,使用shell脚本登录远程服务器:

#!/usr/bin/expect
spawn ssh root@192.168.1.100
expect "*password:"
send "123456\r"
expect eof

我们来看此段代码的第一行,熟悉shell的朋友一定知道,这里是声明下面的代码使用的是expect来解析,但是expect的语法又和平时所用的bash语法不一样,那如果我们希望用熟悉的bash语法该怎么办 呢?看下面的代码:


#!/bin/bash
/usr/bin/expect <<-EOF
# 设置超时时间
set time 30
spawn ssh root@192.168.1.100
#这样写比较繁琐,这个示例用下面的写法,可以一次匹配好多参数
#expect "*password:"
#send "13456\r"
expect {
    "*yes/no" {send "yes\r"}
    "*password" {send "123456\r"}
}
expect eof
EOF

echo OK


第一行声明用了我们最熟悉的bash,那么说明里面的内容就可以用bash语法了,在EOF之外,尽情的挥洒bash代码吧!



标签: expect

评论:

lagunawoodsre
约 20 小时前
<a href="http://www.tmzred.com/nike-kobe-6-orange-green-shoesh">nike kobe 6 orange green</a> <a href="http://www.alcovozov.com/air-jordan-retro-29-cielo-azul-p%C3%BArpura-zapatosp">air jordan retro 29 cielo azul p煤rpura</a> <a href="http://www.ahmztradefx.com/mujeres-nike-flyknit-racer-negro-verde-zapatosk">mujeres nike flyknit racer negro verde</a> <a href="http://www.isampled.com/north-face-hyvent-waterproof-jacket-northfaceh">north face hyvent waterproof jacket</a> <a href="http://www.aldervets.com/air-jordan-retro-13-dam%C3%A4nner-blau-gr%C3%BCn-schuhej">air jordan retro 13 dam盲nner blau gr眉n</a> <a href="http://www.vanltl.com/herre-nike-free-tr-fit-lilla-gr%C3%B8n-skoh">herre nike free tr fit lilla gr酶n</a>
lagunawoodsre http://www.lagunawoodsre.com/
faridekhalaf
2019-02-09 08:54
<a href="http://www.mandydugan.com/hombres-asics-gel-kinsei-5-plata-negro-zapatosk">hombres asics gel kinsei 5 plata negro</a> <a href="http://www.dxnkofe.com/herre-nike-air-max-lunar-1-lyser%C3%B8d-gul-skoi">herre nike air max lunar 1 lyser酶d gul</a> <a href="http://www.yurasmirnov.com/air-jordan-retro-29-gr%C3%B8nn-obuvl">air jordan retro 29 gr酶nn</a> <a href="http://www.blovese.com/nike-kobe-9-low-womens-green-blue-shoesh">nike kobe 9 low womens green blue</a> <a href="http://www.ztsurfboards.com/nike-roshe-grigio-teal-maschio-scarpei">nike roshe grigio teal maschio</a> <a href="http://www.visheshcap.com/nike-air-max-gold-grau-schuhej">nike air max gold grau</a>
faridekhalaf http://www.faridekhalaf.com/
jonvietor
2019-02-07 06:02
<a href="http://www.pelumasvagina.com/limited-nike-olivecamo-youth-dj-moore-jersey-nfl-12-carolina-panthers-2017-salute-to-service-nflw">limited nike olivecamo youth dj moore jersey nfl 12 carolina panthers 2017 salute to service</a> <a href="http://www.recommenet.com/alle-rot-kd-7-for-verkauf-chaussuresa">alle rot kd 7 for verkauf</a> <a href="http://www.weissewelt.com/nike-free-run-5.0-v4-svart-leopard-fc-butya">nike free run 5.0 v4 svart leopard fc</a> <a href="http://www.wileyhyundai.com/mennns-nike-free-3.0-svart-r%C3%B8d-butyc">mennns nike free 3.0 svart r酶d</a> <a href="http://www.ariehonders.com/nike-hyperdunk-2013-svart-and-rosa-obuvg">nike hyperdunk 2013 svart and rosa</a> <a href="http://www.instabitgram.com/nike-kd-vii-big-ni%C3%B1os-negro-verde-kurpesa">nike kd vii big ni帽os negro verde</a>
jonvietor http://www.jonvietor.com/
ztsurfboards
2019-02-04 14:54
<a href="http://www.egyptmaritime.com/hommes-adidas-gazelle-boost-rouge-chaussuresl">hommes adidas gazelle boost rouge</a> <a href="http://www.erstube.com/nike-zoom-pegasus-33-femmes-blanc-oranger-chaussuresq">nike zoom pegasus 33 femmes blanc oranger</a> <a href="http://www.thecardplayer.com/air-jordan-retro-5-wei%C3%9F-royal-blau-schuhei">air jordan retro 5 wei脽 royal blau</a> <a href="http://www.theweedaddict.com/dam%C3%A4nner-air-jordan-retro-11-rot-gr%C3%BCn-schuheh">dam盲nner air jordan retro 11 rot gr眉n</a> <a href="http://www.thecampingman.com/nike-kd-7-kvinners-gr%C3%B8nn-rosa-obuvj">nike kd 7 kvinners gr酶nn rosa</a> <a href="http://www.otuzbircek.com/maschio-asics-gel-quantum-360-grigio-rosso-scarpep">maschio asics gel quantum 360 grigio rosso</a>
ztsurfboards http://www.ztsurfboards.com/
air jordan 7 pink
2019-02-04 09:25
<a href="http://www.shiftcampus.com/womens-asics-gel-kinsei-4-gold-red-shoesh">womens asics gel kinsei 4 gold red</a> <a href="http://www.fiestanetwork.com/mujeres-asics-gel-quantum-360-plata-azul-zapatosp">mujeres asics gel quantum 360 plata azul</a> <a href="http://www.sunbeltsaves.com/adidas-neo-lazy-gris-cielo-azul-zapatosk">adidas neo lazy gris cielo azul</a> <a href="http://www.drmastella.com/green-fuzzy-north-face-jacket-northfaceh">green fuzzy north face jacket</a> <a href="http://www.khzrubber.com/air-jordan-retro-3-dam%C3%A4nner-gr%C3%BCn-orangen-schuhej">air jordan retro 3 dam盲nner gr眉n orangen</a> <a href="http://www.jkdeskisehir.com/adidas-rose-gul-himmelbl%C3%A5-skoh">adidas rose gul himmelbl氓</a>
air jordan 7 pink http://www.rsvppersonnel.com/air-jordan-7-pink-shoesj
voydnet
2019-01-29 13:43
<a href="http://www.jupickuplines.com/michael-kors-jet-set-large-ew-tote-mkh">michael kors jet set large ew tote</a> <a href="http://www.saleuggboot.com/air-jordan-retro-18-svart-gul-obuvi">air jordan retro 18 svart gul</a> <a href="http://www.fragmanizletv.com/air-jordan-retro-3-femminile-argento-rosso-scarpej">air jordan retro 3 femminile argento rosso</a> <a href="http://www.swisshomeas.com/mens-nike-air-max-2017-grey-gold-shoesh">mens nike air max 2017 grey gold</a> <a href="http://www.marinersdiary.com/nike-flyknit-lunar-3-negro-amarillo-zapatosk">nike flyknit lunar 3 negro amarillo</a> <a href="http://www.bourseasie.com/nike-air-max-tailwind-6-gris-p%C3%BArpura-zapatosp">nike air max tailwind 6 gris p煤rpura</a>
voydnet http://www.voydnet.com/
darkstories
2019-01-29 10:26
<a href="http://www.beverlywater.net/air-jordan-retro-5-or-orange-schuhed">air jordan retro 5 or orange</a> <a href="http://www.beyondthegame.net/nike-free-3.0-v5-vivid-blau-chaussuresd">nike free 3.0 v5 vivid blau</a> <a href="http://www.bigiony.net/mennns-jordan-melo-m10-r%C3%B8d-obuvf">mennns jordan melo m10 r酶d</a> <a href="http://www.bigreddot.net/maschio-nike-air-max-95-viola-arancia-kurpese">maschio nike air max 95 viola arancia</a> <a href="http://www.biobill.net/air-jordan-rose-jaune-schuhed">air jordan rose jaune</a> <a href="http://www.bradleycountryacres.net/nike-blazer-floral-noir-marron-butyg">nike blazer floral noir marron</a>
darkstories http://www.darkstories.net/
pikapnsk
2019-01-19 18:42
<a href="http://www.akncmusic.com/kd-trey-5-orange-and-gr%C3%BCn-color-kurpese">kd trey 5 orange and gr眉n color</a> <a href="http://www.dystopianflix.com/nike-lebron-ambassador-9-hvid-guld-schuhee">nike lebron ambassador 9 hvid guld</a> <a href="http://www.getunladylike.com/nike-air-max-95-gul-sort-obuvf">nike air max 95 gul sort</a> <a href="http://www.jesselyon.com/nike-roshe-run-full-negro-woven-chaussuresa">nike roshe run full negro woven</a> <a href="http://www.kabukcuoglu.com/hombres-salomon-s-lab-fellcross-rojo-amarillo-butya">hombres salomon s lab fellcross rojo amarillo</a> <a href="http://www.karbasioun.com/nike-air-max-tailwind-7-dam%C3%A4nner-gold-rot-obuvc">nike air max tailwind 7 dam盲nner gold rot</a>
pikapnsk http://www.pikapnsk.com/
visiotimes
2019-01-18 08:35
<a href="http://www.danielledraws.com/russell-wilson-jersey-t-shirt-nflr">russell wilson jersey t shirt</a> <a href="http://www.lubminbrandov.com/oakland-raiders-15-michael-crabtree-olive-2017-salute-to-service-limited-jersey-nflv">oakland raiders 15 michael crabtree olive 2017 salute to service limited jersey</a> <a href="http://www.evorewards.com/ralph-lauren-blue-striped-polo-polor">ralph lauren blue striped polo</a> <a href="http://www.diggingwp.com/nike-blazer-low-kvinders-lyser%C3%B8d-trainersce">nike blazer low kvinders lyser酶d</a> <a href="http://www.roeschtires.com/ray-ban-rb3362-polarized-sunglassesr">ray ban rb3362 polarized</a> <a href="http://www.saeedlawyer.com/mackage-phyllis-coat-mackager">mackage phyllis coat</a>
visiotimes http://www.visiotimes.com/
dlhiphop
2019-01-07 05:54
<a href="http://www.busrockerz.com/zx-flux-slip-on-for-vendita-butyf">zx flux slip on for vendita</a> <a href="http://www.financewarm.com/air-jordan-14-retro-sort-jordans-butyf">air jordan 14 retro sort jordans</a> <a href="http://www.genesispre.com/buty-air-jordan-3-retro-true-azul-obuvf">buty air jordan 3 retro true azul</a> <a href="http://www.iraqbody.com/nike-dunk-high-pro-sb-baroque-brun-obuve">nike dunk high pro sb baroque brun</a> <a href="http://www.mayakstar.com/nike-lunarglide-flyknit-mujeres-kurpesg">nike lunarglide flyknit mujeres</a> <a href="http://www.thecoedge.com/lunarglide-8-gr%C3%B8n-obuvb">lunarglide 8 gr酶n</a>
dlhiphop http://www.dlhiphop.com/
autismy
2019-01-04 09:18
<a href="http://www.watersorb.net/nike-cortez-classic-nylon-arancia-kurpesc">nike cortez classic nylon arancia</a> <a href="http://www.westernridning.net/nike-air-max-lunar-1-nero-volt-magista-butya">nike air max lunar 1 nero volt magista</a> <a href="http://www.wissenschaftsverlag.net/nike-kobe-9-low-gris-vert-schuheg">nike kobe 9 low gris vert</a> <a href="http://www.wojtech.net/jordan-7-team-r%C3%B8d-gum-butyf">jordan 7 team r酶d gum</a> <a href="http://www.xperlink.net/air-max-90-marinen-volt-butya">air max 90 marinen volt</a> <a href="http://www.zeenalavey.net/nike-air-huarache-triple-schwarz-olx-obuvc">nike air huarache triple schwarz olx</a>
[url=http://www.autismy.net/]autismy[/url]
avalonsoccer
2019-01-02 02:54
<a href="http://www.hobbyfreaks.com/air-jordan-retro-13-s%C3%B8lv-royal-bl%C3%A5-butyf">air jordan retro 13 s酶lv royal bl氓</a> <a href="http://www.inselwasser.com/nike-roshe-run-hyperfuse-qs-sort-butye">nike roshe run hyperfuse qs sort</a> <a href="http://www.naturalcitral.com/nike-free-run-5.0-dahombres-schwarz-obuvf">nike free run 5.0 dahombres schwarz</a> <a href="http://www.sterneanswers.com/nike-air-huarache-guld-leaf-spring-obuvd">nike air huarache guld leaf spring</a> <a href="http://www.agrilover.com/nike-air-presto-skull-orange-lila-kurpesg">nike air presto skull orange lila</a> <a href="http://www.alikexue.com/adidas-zx-750-light-braun-obuvb">adidas zx 750 light braun</a>
avalonsoccer http://www.avalonsoccer.com/

发表评论:

Powered by emlog 冀ICP备13011830号-1