linux expect 通配符,expect远程工具

expect远程登录,登录后不退出

安装expect:yum -y install expect

1、执行远程登录:

expect;执行 ./expect.sh [这不是bash解释器,所以不能sh执行]

#!/usr/bin/expect

set host "192.168.1.14"

set passwd "123456"

spawn ssh root@$host

expect {

"yes/no" { send "yes\r";exp_continue }

"assword:" { send "$passwd\r" }

}

interact

解释:

1、set host "192.168.1.14" 设置变量:host

2、spawn ssh root@$host 执行的命令即shell命令

3、如下固定语法:

@1、"yes/no" { send "yes\r";exp_continue}为第一次登录时需要yes/no,这里的yes/no关键字是根据:

Are you sure you want to continue connecting (yes/no)? 登录时的提示语事先做出判断,如果检测到有此关键字出现,就执行后面的send语句;

@2、"assword:" { send "$passwd\r" }为检测到关键字"assword:"时就执行后面的send语句;

@3、send语句后面的 \r 表示回车的意思;

@4、{ } 花括号之间得有空格;

expect {

"yes/no" { send "yes\r";exp_continue}

"assword:" { send "$passwd\r" }

}

登录后执行命令,然后退出

1、远程登录后执行命令:

expect2

#!/usr/bin/expect

set user "root"

set passwd "123456"

set host "192.168.1.14"

spawn ssh $user@$host

expect {

"yes/no" { send "yes\r";exp_continue }

"password:" { send "$passwd\r" }

}

expect "]*"

send "touch /home/software/test333.txt\r"

expect "]*"

send "echo 123 > /home/software/test333.txt\r"

expect "]*"

send "exit\r"

解释:

@1、expect "]*" 表示在linux界面检测到关键字符[root@hdc_2 ~]#的 "]#" *号表示通配符全匹配,即在这[root@hdc_2 ~]#后面执行命令

传递参数

1、传递参数:

expect3

#!/usr/bin/expect

set user [ lindex $argv 0 ]

set host [ lindex $argv 1 ]

set passwd "123456"

set cm [ lindex $argv 2 ]

spawn ssh $user@$host

expect {

"yes/no" { send "yes\r" }

"password:" { send "$passwd\r" }

}

expect "]*"

send "$cm\r"

expect "]*"

send "exit\r"

解释:

@1、设置变量【[ ]方括号之间必要有空格 】

set user [ lindex $argv 0 ] ; lindex $argv 0表示第一个参数,类似shell的$1

set host [ lindex $argv 1 ]; lindex $argv 1表示第二个参数,类似shell的$2

@2、设置命令变量

set cm [ lindex $argv 2 ];cm表示命令参数,lindex $argv 2 表示第三个参数,类似shell的$3

@3、执行./expect3 root 192.168.1.14 "ls /home"

如果cm 为一个长串命令需要用双引号括起

传输文件

#、传输文件:【自动化运维很大一部分就是要在其它机器上传递文件】

#!/usr/bin/expect

set passwd "123456"

spawn scp -r /home/software/123.bak root@192.168.1.14:/home/software/

expect {

"yes/no" { send "yes\r" }

"password:" { send "$passwd\r" }

}

expect eof

解释:

@1、spawn scp -r /home/software/123.bak root@192.168.1.14:/home/software/

也可以使用rsync命令spawn rsync -av root@192.168.1.14:/home/software/test333.txt /home

@2、最后的结束标语必须要有:expect eof 【eof表示end over finish】

指定host和指定要同步的文件

1、指定host和指定要同步的文件:

#!/usr/bin/expect

set passwd "123456"

set host [ lindex $argv 0 ]

set file [ lindex $argv 1 ]

spawn rsync -av $file root@$host:$file

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect eof

解释:

@1、指定host和指定file,即把host和file都设置成变量;

@2、IP地址可以存之一个文件,for循环来遍历;

for ip in `cat ip.txt`;do echo $ip ; ./expect5 $ip /file ; done

expect构建文件分发系统

1、实现思路:

首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。

2、核心命令:【格式固定】

spawn rsync -av --files-from=list.txt / root@host:/

@1、list.txt保存的是需要分发的文件的绝对路径如:

cat list.txt

/home/software/123.txt

/home/456.txt

@2、分发时文件的路径在目标机器上的路径也要一致存在,即目标机器上路径/home/software/要存在,/home/要存在。

【注:如果不能确保目标机器上路径是否一致存在,核心命令可加参数-R:spawn rsync -avR --files-from=$file / root@$host:/】

3、文件分发系统的实现:

rsync.expect

#!/usr/bin/expect

set passwd "123456"

set host [lindex $argv 0]

set file [lindex $argv 1]

spawn rsync -av --files-from=$file / root@$host:/

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect eof

rsync.sh

#!/bin/bash

for ip in `cat ip.list`

do

echo $ip

./rsync.expect $ip list.txt

done

2、最后只需执行sh rsync.sh脚本即可。

自动批量执行脚本命令

cat exe.expect

#!/usr/bin/expect

set host [lindex $argv 0]

set passwd "123456"

set cm [lindex $argv 1]

spawn ssh root@$host

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect "]*"

send "$cm\r"

expect "]*"

send "exit\r"

cat exe.sh

#!/bin/bash

for ip in `cat ip.list`

do

echo $ip

./exe.expect $ip "w;free -m;ls /tmp"

done

最后执行:sh exe.sh脚本即可。

【注:如果./exe.expect $ip "w;free -m;ls /tmp" 第二个参数命令太复杂,可以先把命令写成脚本,分发到各个机器上,然后贴上执行脚本的命令即可】

2、修改如下:

cmd.sh

#!/bin/bash

cd /home/software

for i in `seq 1 10`

do

echo $i >> /home/software/cmd.txt

done

exe.expect2

#!/usr/bin/expect

set host [ lindex $argv 0 ]

set passwd "123456"

set cm [ lindex $argv 1 ]

set file [ lindex $argv 2 ]

spawn rsync -avR --files-from=$file / root@$host:/

expect {

"yes/no" { send "yes\r" }

"password:" { send "$passwd\r" }

}

expect eof

spawn ssh root@$host

expect {

"yes/no" { send "yes\r" }

"password:" { send "$passwd\r" }

}

expect "]*"

send "$cm\r"

expect "]*"

send "exit\r"

cat exe.sh2

#!/bin/bash

for ip in `cat ip.txt`

do

echo $ip

./exe.expect2 $ip "sh /home/software/cmd.sh" list.txt

done

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值