美文网首页PHP学习笔记
Shell学习笔记-expect 传参

Shell学习笔记-expect 传参

作者: 赵客缦胡缨v吴钩霜雪明 | 来源:发表于2022-04-28 14:50 被阅读0次

bash是通过$0 ... $n接收参数

expect是通过set <变量名称> [lindex $argv <param index>]

例如:set username [lindex $argv 0]

#!/usr/bin/expect  
set timeout 10  
set username [lindex $argv 0]  
set password [lindex $argv 1]  
set hostname [lindex $argv 2]  
spawn ssh $username@$hostname  
expect "yes/no"  
send "yes\r"  
expect "password:" 
send "$password\r"
expect eof

执行脚本./ssh.exp root pasword hostname1

一个比较粗糙的Linux expect通过telnet console 口配置网络设备

#!/bin/bash
PORT=$1
cmd="""sys\r
interface g1/1\r
 ip add 1.1.1.1 24\r"""
# 执行 expect
# 每个 expect 的判断间隔为 5 秒,确保命令可以正常退出
# expect 可以通过发送 ascii 码来执行键盘组合

expect <<END
    set timeout 5
    spawn telnet 10.0.0.1 1111
    expect "*]'" { send "\r" }
    expect "*assword:" { send "P@ssword\r" }
    expect {
        "*>" { send "screen disable\r" }
        "*]" { send "screen disable\r" }
    }
    expect {
        "*>" { send "$cmd" }
        "*]" { send "$cmd" }
    }
    expect "*]" { send "\03" }
    expect eof
END

一个自动登录脚本

#!/usr/bin/expect

set LOGIN_IP [lindex $argv 0]

spawn zssh administrator@$LOGIN_IP -p 2222


expect {
"*yes/no*"
{send "yes\r";exp_continue;}
"*password:"
{send "password\r;"}
}

expect "ac>"
send "loginto\r"

expect "*password:"
send "password\r"

expect "*:~#"
send "cd /usr/local/\r"
interact

相关文章

  • Shell学习笔记-expect 传参

    bash是通过$0 ... $n接收参数 expect是通过set <变量名称> [lindex $argv ]...

  • Shell学习笔记-expect

    expect 解释器 expect是一个能实现自动和交互式任务的解释器,它也能解释常见的shell语法命令,其特色...

  • shell中脚本传参

    shell中脚本传参 shell脚本传参有下面两种方式 1. $0,$1,$2.. 采用$0,$1,$2..等方式...

  • Shell学习笔记-expect 自动交互脚本

    启用选项 -c:执行脚本前先执行的命令,可多次使用。 -d:debug模式,可以在运行时输出一些诊断信息,与在脚本...

  • shell脚本切换用户执行并执行后续操作

    实现shell脚本切换用户并执行后续操作,需要使用expect脚本(或shell调用expect),如下为一个简单...

  • shell脚本-传参

    #!/bin/bashecho $0echo $1echo $2 比如这个脚本叫01.sh 传入参数的方式特简单:...

  • SHELL 笔记

    shell笔记 判断语句 运算 选择语句 循环语句 将shell字句执行结果复制给变量 shell简单传参 编号变...

  • shell编程基础2

    一、shell脚本传参 4.1.shell文件中输入 su test.sh one two three打印出对应输...

  • expect

    参考: Expect Command And How To Automate Shell Scripts Like...

  • shell面试题

    脚本传参[http://it.agr-api.com/shell/param_deliver.html]文件操作[...

网友评论

    本文标题:Shell学习笔记-expect 传参

    本文链接:https://www.haomeiwen.com/subject/mukdyrtx.html