zoukankan      html  css  js  c++  java
  • shell expect实现ssh免交互执行命令的三种用法

    介绍:
    https://www.cnblogs.com/chenjo/p/12892894.html

    1.EOF 标准输出作为 expect 标准输入

    #!/bin/bash
    USER=root
    PASS=123.com
    IP=192.168.1.120
    expect << EOF
    set timeout 30
    spawn ssh $USER@$IP
    expect {
    "(yes/no)" {send "yes
    "; exp_continue}
    "password:" {send "$PASS
    "}
    }
    expect "$USER@*" {send "$1
    "}
    expect "$USER@*" {send "exit
    "}
    expect eof
    EOF
    

    方法 2:

    #!/bin/bash
    USER=root
    PASS=123.com
    IP=192.168.1.120
    expect -c "
    spawn ssh $USER@$IP
    expect {
    "(yes/no)" {send "yes
    "; exp_continue}
    "password:" {send "$PASS
    "; exp_continue}
    "$USER@*" {send "df -h
     exit
    "; exp_continue}
    }"
    

    方法 3:将 expect 脚本独立出来
    login.exp 登录文件:

    #!/usr/bin/expect
    set ip [lindex $argv 0]
    set user [lindex $argv 1]
    set passwd [lindex $argv 2]
    set cmd [lindex $argv 3]
    if { $argc != 4 } {
    puts "Usage: expect login.exp ip user passwd"
    exit 1
    }
    set timeout 30
    spawn ssh $user@$ip
    expect {
    "(yes/no)" {send "yes
    "; exp_continue}
    "password:" {send "$passwd
    "}
    }
    expect "$user@*" {send "$cmd
    "}
    expect "$user@*" {send "exit
    "}
    expect eof
    

    执行命令脚本:

    #!/bin/bash
    HOST_INFO=user_info
    for ip in $(awk '{print $1}' $HOST_INFO)
    do
    user=$(awk -v I="$ip" 'I==$1{print $2}' $HOST_INFO)
    pass=$(awk -v I="$ip" 'I==$1{print $3}' $HOST_INFO)
    expect login.exp $ip $user $pass $1
    done
    

    SSH 连接信息文件:
    cat user_info
    192.168.1.120 root 123456

  • 相关阅读:
    Meterpreter
    CHM木马
    浅析ARP协议及ARP攻击
    python绝技 — 使用PyGeoIP关联IP地址和物理位置
    python虚拟环境virtualenv的安装与使用
    python调用nmap探测局域网设备
    提权
    Nexpose
    docker安装使用
    一些渗透测试练习环境介绍
  • 原文地址:https://www.cnblogs.com/rockstics/p/14178159.html
Copyright © 2011-2022 走看看