zoukankan      html  css  js  c++  java
  • UNIX/LINUX使用expect实现人机自己主动交互功能

    expect使用方法

    1. [#!/usr/bin/expect]

    这一行告诉操作系统脚本里的代码使用那一个shell来运行。这里的expect事实上和linux下的bash、windows下的cmd是一类东西。

    注意:这一行须要在脚本的第一行。

    1. [set timeout 30]

    基本上认识英文的都知道这是设置超时时间的,如今你仅仅要记住他的计时单位是:秒 。timeout -1 为永不超时

    1. [spawn ssh -l username 192.168.1.1]

    spawn是进入expect环境后才干够运行的expect内部命令。假设没有装expect或者直接在默认的SHELL下运行是找不到spawn命令的。所以不要用 “which spawn“之类的命令去找spawn命令。好比windows里的dir就是一个内部命令,这个命令由shell自带。你无法找到一个dir.com 或 dir.exe 的可运行文件。

    它基本的功能是给ssh运行进程加个壳,用来传递交互指令。

    1. [expect “password:”]

    这里的expect也是expect的一个内部命令,有点晕吧,expect的shell命令和内部命令是一样的,但不是一个功能。习惯就好了。这个命令的意思是推断上次输出结果里是否包括“password:”的字符串,假设有则马上返回。否则就等待一段时间后返回,这里等待时长就是前面设置的30秒

    1. [send “ispass ”]

    这里就是运行交互动作,与手工输入密码的动作等效。

    温馨提示: 命令字符串结尾别忘记加上“ ”,假设出现异常等待的状态能够核查一下。

    1. [interact]

    运行完毕后保持交互状态,把控制权交给控制台,这个时候就能够手工操作了。

    假设没有这一句登录完毕后会退出。而不是留在远程终端上。假设你仅仅是登录过去运行

    7.$argv 參数数组

    expect脚本能够接受从bash传递过来的參数.能够使用[lindex $argv n]获得,n从0開始,分别表示第一个,第二个,第三个….參数

    以下的expect脚本的样例

    运行这个文件./launch.exp 1 2 3

    屏幕上就会分别打印出參数

    send_user用来发送内容给用户。

    參数运用方面还有非常多技巧

    比方argcargsargvargv0 被初始化为脚本名字。

    除此之外。假设你在第一行(#!那行)使用-d (debug參数),能够在运行的时候输出一些非常实用的信息

    比方你会看见

    argv[0] = /usr/bin/expect argv[1] = -d argv[2] = ./launch.exp argv[3] = 1 argv[4] = 2 argv[5] = 3

    使用这些也能够完毕參数传递

    8.expect的命令行參数參考了c语言的。与bash shell有点不一样。

    当中,argcargv0为脚本名字本身,argv[lrangeargv 0 0]表示第1个參数。[lrange argv04]cargv不包括脚本名字本身。

    9.exp_continue的使用方法

    #!/usr/bin/expect -f

    set ipaddr “localhost”

    set passwd “iforgot”

    spawn ssh root@$ipaddr #spawn 意思是运行命令。expect内命令。shell中不存在

    expect {

    “yes/no” { send “yes ”; exp_continue}

    “password:” { send “$passwd ” }

    }

    expect “]# “

    send “touch a.txt ” #意思为发送命令

    send “exit ”

    expect eof

    exit

    exp_continue能够继续运行以下的匹配。简单了很多。另一点,让我认识到匹配不见得要匹配最后几个字符。

    10.拿来小样例

    设置变量 set PASSWD abcd123

    #!/usr/bin/expect -f
    
    # Expect script to supply root/admin password for remote ssh server
    
    # and execute command.
    
    # This script needs three argument to(s) connect to remote server:
    
    # password = Password of remote UNIX server, for root user.
    
    # ipaddr = IP Addreess of remote UNIX server, no hostname
    
    # scriptname = Path to remote script which will execute on remote server
    
    # If you username and passwd has not pass the rsa trust, your login will fail.
    
    # Usage For example:
    
    #  ./sshlogin.exp password 192.168.1.11 who
    
    # ------------------------------------------------------------------------
    
    # Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/>
    
    # This script is licensed under GNU GPL version 2.0 or above
    
    # -------------------------------------------------------------------------
    
    # This script is part of nixCraft shell script collection (NSSC)
    
    # Visit http://bash.cyberciti.biz/ for more information.
    
    # ----------------------------------------------------------------------
    
    # set Variables
    
    set password [lrange $argv 0 0]
    
    set ipaddr [lrange $argv 1 1]
    
    set scriptname [lrange $argv 2 2]
    
    set arg1 [lrange $argv 3 3]
    
    set timeout -1
    
    # now connect to remote UNIX box (ipaddr) with given script to execute
    
    spawn ssh yourusername@$ipaddr $scriptname $arg1
    
    match_max 100000
    
    # Look for passwod prompt
    
    expect "*?assword:*"
    
    # Send password aka $password
    
    send -- "$password
    "
    
    # send blank line (
    ) to make sure we get back to gui
    
    send -- "
    "
    
    expect eof
    
    
    
    ==============================================================================
    
    
    

    #!/usr/bin/expect

    # 设置超时时间为 60 秒

    set timeout 60

    # 设置要登录的主机 IP 地址

    set host 192.168.1.46

    # 设置以什么名字的用户登录

    set name root

    # 设置用户名的登陆password

    set password 123456

    #spawn 一个 ssh 登录进程

    spawn ssh hostlname

    # 等待响应,第一次登录往往会提示是否永久保存 RSA 到本机的 know hosts 列表中;等到回答后,在提示输出密码;之后就直接提示输入密码

    expect {

    "(yes/no)?" { 
    
        send "yes
    "
    
        expect "assword:"
    
        send "$pasword
    "
    
    } 
    
        "assword:" { 
    
        send "$password
    "
    
    } 
    

    }

    expect “#”

    # 以下測试是否登录到 $host

    send “uname ”

    expect “Linux”

    send_user “Now you can do some operation on this terminal ”

    # 这里使用了 interact 命令,使运行完程序后,用户能够在 $host 终端进行交互操作。

    Interact

    ==============================================================================

    用expect实现ssh自己主动登录对服务器进行批量管理

    1.实现ssh自己主动登录完毕任务的expect脚本

    #!/usr/bin/expect -f

    set ipaddress [lindex $argv 0]

    set passwd [lindex $argv 1]

    set timeout 30

    spawn ssh shellqun@$ipaddress

    expect {

    “yes/no” { send “yes ”;exp_continue }

    “password:” { send “$passwd ” }

    }

    expect “from

    send “mkdir -p ./tmp/testfile ”

    #send “exit ”

    expect “#” 命令运行完, 你要期待一个结果, 结果就是返回shell提示符了(是# 或者$)

    #最后一句第13行的解释:

    事实上写成 interact 的最大优点是登录后不会退出,而会一直保持会话连接,能够兴许手动处理其他任务,请依据实际情况自行选择了。

    2.调用login.exp完毕批量管理

    !/bin/bash

    for i in awk '{print $1}' passwd.txt

    do

    j=awk -v I="$i" '{if(I==$1)print $2}' passwd.txt

    expect /root/shell/login.exp ij

    done

    3.passwd.txt

    192.168.0.2 password2

    192.168.0.3 password3

    13.expect {

    “?

    assword:” {
    #此大括号内是逐条运行,不存在if关系
    send “$PASSWORD ”

    exp_continue

    }

    }

  • 相关阅读:
    Java 简单算法--打印乘法口诀(只使用一次循环)
    Java简单算法--求100以内素数
    ubuntu 16.04 chrome flash player 过期
    java 网络API访问 web 站点
    java scoket (UDP通信模型)简易聊天室
    leetcode1105 Filling Bookcase Shelves
    leetcode1140 Stone Game II
    leetcode1186 Maximum Subarray Sum with One Deletion
    leetcode31 Next Permutation
    leetcode834 Sum of Distances in Tree
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5287733.html
Copyright © 2011-2022 走看看