zoukankan      html  css  js  c++  java
  • linux交互执行命令,expect

    转载 http://donex.blog.51cto.com/2005970/834467

    原文比较乱,只能参考

    本地交互执行:

    1. 修改shell
    #!/usr/bin/expect
    set USER [lindex $argv 0]
    set SHELL [lindex $argv 1]
    set timeout 3
    spawn chsh $USER
    expect "*]:*" { send "$SHELL " }
    expect eof
    # ./chsh.sh user1 /bin/tcsh

    2. 修改密码
    #!/usr/bin/expect
    set USER [lindex $argv 0]
    set PASS "1q2w#E$R"
    set timeout 3
    spawn passwd $USER
    expect "*Password:*" { send "$PASS " }
    expect "*Password:*" { send "$PASS " }
    expect eof
    # ./pass.sh user1

    或把用户和密码都作为参数
    #!/usr/bin/expect
    set USER [lindex $argv 0]
    set PASS [lindex $argv 1]
    set timeout 3
    spawn passwd $USER
    expect "*Password:*" { send "$PASS " }
    expect "*Password:*" { send "$PASS " }
    expect eof
    # ./pass.sh ttt 1q2w#E$R
    # ./pass.sh ttt "1q2w#E$R"

    总结:expect 必须要匹配最后一个输出字符。



    远程交互ssh 登录:

    1. 设置变量,执行多命令。
    #!/usr/bin/expect
    set IP "10.85.138.42"
    set timeout 3
    spawn ssh ${IP}
    expect "*yes/no*" { send "yes " }
    expect "*assword*" { send "root "}
    expect "*#*" { send "ls " }
    expect "*#*" { send "touch /tanjiyong/newfile " }
    expect eof
    #./exp.sh


    2. 增加参数。
    #!/usr/bin/expect
    set IP [lindex $argv 0]
    set USER [lindex $argv 1]
    set timeout 3
    spawn ssh $USER@${IP}
    expect "*yes/no*" { send "yes " }
    expect "*assword*" { send "root "}
    expect "*#*" { send "ls " }
    expect "*#*" { send "touch /tanjiyong/newfile " }
    expect eof
    #./exp.sh 10.85.138.42 root


    3. ssh 登录,执行时间超过timeout时间,设定timeout为-1(无限制)。
    #!/usr/bin/expect
    set IP [lindex $argv 0]
    set USER [lindex $argv 1]
    set IP2 [lindex $argv 2]
    set timeout 3
    spawn ssh $USER@${IP}
    expect "*yes/no*" { send "yes " }
    expect "*assword*" { send "root "}
    expect "*#*" { send "ls " }
    expect "*#*" { send "ping $IP2 " }
    set timeout -1
    expect "*#*" { send "exit 1 " }
    expect eof


    4. ssh 登录,使用循环,在30台机器执行相同命令。
    #!/usr/bin/expect
    set USER root
    set PASS root
    for {set i 1} {$i<=30} {incr i} {
    spawn ssh -l $USER 125.1.1.$i
    expect "*yes/no*" { send "yes " }
    expect "*assword*" { send "$PASS "}
    expect "*#*" { send "find / -name hao.txt " }
    expect eof
    }
    #./exp.sh



    本地远程交互执行:

    1. spawn 执行scp
    #!/usr/bin/expect
    set PASS root
    set timeout 3
    spawn scp /etc/passwd root@10.85.138.42:/tanjiyong
    expect "*yes/no*" { send "yes " }
    expect "*Password:*" { send "$PASS " }
    set timeout -1
    expect "*#*" { send "exit 1 " }
    # ./scp.sh

    使用-- send
    #!/usr/bin/expect --
    set PASS root
    set USER root
    set IP 10.85.138.42
    set env(SHELL) /bin/bash
    set timeout 1
    spawn $env(SHELL)    #spawn /bin/bash
    expect "*#*" { send "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong " }
    expect "*yes/no*" { send "yes " }
    expect "*Password:*" { send "$PASS " }
    set timeout -1    #复制的时间较长,设置为timeout无限制
    expect "*#*" { send "exit 1 " }

    #!/usr/bin/expect
    set PASS root
    set USER root
    set IP 10.85.138.42
    set env(SHELL) /bin/bash
    set timeout 1
    spawn $env(SHELL)
    expect "*#*" { send -- "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong " }
    expect "*yes/no*" { send "yes " }
    expect "*Password:*" { send "$PASS " }
    set timeout -1    #复制的时间较长,设置为timeout无限制
    expect "*#*" { send "exit 1 " }


    #!/usr/bin/expect
    set timeout 3
    set env(SHELL) /bin/bash
    spawn $env(SHELL)
    expect -exact "# "
    send -- "scp $TAR_NAME ${USERNAME}@${DESTIP}{DESTDIR} "
    expect {
        "*yes/no*" { send "yes ";exp_continue }
        "assword: " { send "hw2009 " }
    }
    set timeout -1    #复制的时间较长,设置为timeout无限制
    expect "# "
    send "exit "
    expect eof


    2. expect 搜索块
    #!/usr/bin/expect
    set PASS root
    set timeout 3
    spawn scp /etc/passwd root@10.85.138.42:/tanjiyong
    expect {
        "*yes/no*" { send "yes ";exp_continue }
        "assword: " { send "$PASS " }
    }
    set timeout -1
    expect "*#*" { send "exit 1 " }
    # ./scp.sh


    3. 判断
    #!/usr/bin/expect
    set PASS [lindex $argv 0]
    set USER root
    set IP 10.85.138.42
    set env(SHELL) /bin/bash
    set timeout 1
    spawn $env(SHELL)
    expect "*#*" { send "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong " }
    expect {
        "*yes/no*" { send "yes " }
        "*Password:*" { send "$PASS " }
    }
    expect "*Password:*" { send_user " Passwd error! ";exit 1 }
    set timeout -1
    expect "# " { send "exit 1 " }
    expect eof
    # ./scp.sh

    4. rsync 备份使用。
    #!/usr/bin/expect --
    spawn ssh backup@10.85.138.212
    expect {
        "(yes/no)?" {
            send "yes "
        }
        "assword" {
            send "123456 "
        }
    }

    send "rsync -avz rsync@10.85.138.212:/home/html /opt "
    expect "total size"
    expect {
        "rsync error" {
            exit 1
        }
    }

    expect "# "
    send "exit "
    interact
    #expect eof


    脚本中使用:
    #!/bin/bash
    echo "Start..."
    cat << EOF > /expectfile
    #!/usr/bin/expect
    set PASS root
    set USER root
    set IP 10.85.138.42
    set env(SHELL) /bin/bash
    set timeout 1
    spawn $env(SHELL)    #必须加上,不然会被置换为空。
    expect "*#*" { send "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong " }
    expect "*yes/no*" { send "yes " }
    expect "*Password:*" { send "$PASS " }
    set timeout -1
    expect "# " { send "exit 1 " }
    expect eof
    EOF
    expect -f /expectfile
    echo "$?"
    echo "finished backup.."




    附:修改SSH Client为非ask模式
    vi /etc/ssh/ssh_config
    #     StrictHostKeyChecking ask
    StrictHostKeyChecking no

    参考:自动登录
    #!/usr/bin/expect
    if {$argc!=3} {     # 疑问:参数个数($#)
        send_user "Usage: $argv0 {Array IP} {Password} {CMD} "
        exit
    }

    set IP [lindex $argv 0]
    set Password [lindex $argv 1]
    set CMD [lindex $argv 2]
    spawn ssh admin@$IP
    expect {
        "assword:" {
            exec sleep 1
            send "${Password} "
        }
        "*continue connecting*" {
            exec sleep 1
            send "yes "
            expect    "*Password:" {
                exec sleep 1
                send "${Password} "
            }    
        }
    }

    expect {
        "*Password*" { send_user " 1assword error! "
            exit
        }
        "*already*" { send_user " 2:Repeated login! "
            exit
        }
        "OceanStor: admin>" { send "${CMD} " }
    }
    expect "*>"
    send "exit "
    expect "*closed*"
    exit


    脚本2
    #!/usr/bin/expect
    set ipaddress [lindex $argv 0]
    set passwd [lindex $argv 1]
    spawn ssh -p 22 root@$ipaddress
    expect {
        "want"    {send -- "yes "; exp_continue}
        "password:"     {send -- "$passwd"}
        "No route"    { exit }
    }
    set timeout 5
    send " "
    expect "*justin*"
    send "pwd "
    expect "*OK*"
    send "exit "
    expect eof


    相关:
    exit 1/exit    #停止执行,退出脚本
    expect eof #脚本结束
    expect -exact "# " #精确匹配
    expect "# "
    expect "*#*" #模糊匹配
    exp_continue #在同一个expect块里,做多次匹配。
    send #发送命令。
    send_user #打印终端信息。用法与send一致。

  • 相关阅读:
    试述软件的概念和特点?软件复用的含义?构件包括哪些?
    Spring Security基本用法
    java中跳出循环的方式
    cookie和session区别
    spring中类型注解下的bean的加载顺序
    常见的异常
    aop使用场景
    缓存类似于redis
    旧版redis使用
    获取rdis的几种方式
  • 原文地址:https://www.cnblogs.com/liuzhenwei/p/6179023.html
Copyright © 2011-2022 走看看