zoukankan      html  css  js  c++  java
  • 使用expect实现ssh自动输入密码,从而自动登陆Linux

    以下来自网上的一些资料和自己的实验,用以下命令执行: 

    expect -f expect-ssh.exp <username> <password> <hostname or ip> 

    注意,在这个expect-ssh.exp中,connect函数主要负责登陆,代码的最后两行,两个send是登陆上去后,执行的命令,注意最后一定要执行一个exit,否则会导致expect执行完成后还留在远程主机上。 

    这里是expect-ssh.exp的源码: 
    proc usage {} {
        puts stderr 
    "usage: $::argv0 username password ipaddress"
        
    exit 1
    }

    proc 
    connect {pass} {
       expect {
           
    "(yes/no)?" {
               
    send "yes\n"
               expect 
    "*password:" {
                    
    send "$pass\n"
                    expect {
                        
    "*#" {
                            
    return 0
                        }
                    }
               }
           }
           
    "*password:" {
               
    send "$pass\n"
               expect {
                   
    "*#" {
                       
    return 0
                   }
               }
           }
       }
       
    return 1
    }

    if {$argc != 3} { usage }

    set username [lindex 
    $argv 0]
    set password [lindex 
    $argv 1]
    set hostip [lindex 
    $argv 2]

    spawn ssh ${username}@${hostip}

    if {[connect $password]} {
        
    exit 1
    }

    send "rm -f /root/testeric;rm -f pattern_file\n"
    send "exit\n"
    expect 
    eof
  • 相关阅读:
    通过 VB5 创建 ActiveX DLL 文件并用 ASP 调用一例
    Autocad VBA初级教程
    自学资料第一集
    Linux虚拟化:10个不得不爱的理由
    EXCEL VBA编程的一些小结
    FAQ 工作薄及工作表
    很重要的EXCEL使用技巧
    Excel VBA编程的常用代码
    VBA生成一个CorelDraw工具栏
    支付宝,网银在线,快钱 3大支付接口的集成与对比,统合实现
  • 原文地址:https://www.cnblogs.com/super119/p/1909977.html
Copyright © 2011-2022 走看看