zoukankan      html  css  js  c++  java
  • shell1(分发系统,expect)

    expect讲解
    expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时候只要知道对方机器的账号和密码就可以通过expect脚本实现登录和远程命令。
    使用expect之前,需要先安装expect:
    yum install -y expect 

    1. 自动远程登录,并执行命令
    首先来看一个登录后不退出的脚本:

    vim 1.expect

    #! /usr/bin/expect
    set host "192.168.11.102"
    set passwd "123456"
    spawn ssh root@$host
    expect {
    "yes/no" { send "yes "; exp_continue}
    "assword:" { send "$passwd " }
    }
    interact

    chmod a+x 1.expect(加权限)

    ./1.expect(执行命令)

    退出用 logout

    2.登陆后,执行命令然后退出的脚本:

    vim 2.expect

    #!/usr/bin/expect
    set user "root"
    set passwd "123456"

    spawn ssh $user@192.168.11.18

    expect {
    "yes/no" { send "yes "; exp_continue}
    "password:" { send "$passwd " }
    }
    expect "]*"
    send "touch /tmp/12.txt "
    expect "]*"
    send "echo 1212 > /tmp/12.txt "
    expect "]*"
    send "exit "

    chmod a+x 2.expect

    ./2.expect(执行命令)

    3.传递参数

    vim 3.expect

    #!/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 "}
    "password:" { send "$passwd " }
    }
    expect "]*"
    send "$cm "
    expect "]*"
    send "exit "

    chmod a+x 3.expect

     ./2.expect root 192.168.11.18 “w”  (执行命令 ,使用其他命令要用双引号)

    4.自动同步文件

    vim 4.expect

    #!/usr/bin/expect
    set passwd "123456"
    spawn rsync -av root@192.168.11.18:/tmp/12.txt /tmp/
    expect {
    "yes/no" { send "yes "}
    "password:" { send "$passwd " }
    }
    expect eof

    chmod a+x 4.expect

    ./4.expect

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

    vim 5.expect

    #!/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 "}
    "password:" { send "$passwd " }
    }
    expect eof

    chmod a+x 5.expect

    执行: ./4.expect 192.168.11.18 /tmp/12.txt  (远程机器的ip)

  • 相关阅读:
    《信息安全技术》实验四 木马及远程控制技术
    2017-2018-1 20155211 《信息安全系统设计基础》第9周学习总结
    linux 下c语言的pwd 实现
    2017-2018-1 20155211 实验三 实时系统
    信息安全技术 实验三 数字证书应用
    2017-2018-1 20155211 《信息安全系统设计基础》第八周学习总结
    2017-2018-1 20155211 《信息安全系统设计基础》第7周学习总结
    2017-2018-1  20155235 20155211 实验二 固件程序设计
    20155211实验2 Windows口令破解
    恢复旋转排序数组&&旋转字符串
  • 原文地址:https://www.cnblogs.com/pxmlinux/p/5556708.html
Copyright © 2011-2022 走看看