美文网首页
Ubuntu使用Spawn和expect实现ssh自动登陆

Ubuntu使用Spawn和expect实现ssh自动登陆

作者: DiligentLeo | 来源:发表于2018-02-07 16:47 被阅读194次

    1.安装expect

    • 使用以下代码检测expect是否已经安装
     ls /usr/bin | grep expect 
    
    如果显示为空,则使用以下命令安装
    
    
    sudo apt-get install tcl tk expect
    

    2. 制作自动登陆脚本

    touch www.test.com
    chmod +x www.test.com
    vi www.test.com
    
    • 脚本中写入以下内容,即,按i键进入写入模式,
    #!/usr/bin/expect
    spawn ssh yourusername@www.test.com
    expect "*password:"
    send "yourpassword\r"
    expect "*#"
    interact
    
    按esc退出插入模式,然后输入:wq,保存并退出。
    
    

    3.使用脚本自动登陆

    • 注意,需要先手动ssh一下目标机保存密匙,因为我们脚本里并没有考虑这一步,不然会报错。
    • 使用以下命令登陆远程机器
    cd ~ 
    ./www.test.com
    

    相关文章

      网友评论

          本文标题:Ubuntu使用Spawn和expect实现ssh自动登陆

          本文链接:https://www.haomeiwen.com/subject/lugkixtx.html