快捷搜索:   服务器  安全  linux 安全  MYSQL  dedecms

Linux 系统自动化管理: 远程登录(2)

    # 期待提示出入密码的响应

    expect "assword:"

    # 接收密码

    send "$password\n"

    expect ":~#"

    # 将刚刚拷贝的 rsa key 添加到用户的 home 目录下的 ./ssh/authorized_keys

    if { $name == "root"} {

    send "cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys\n"

    } else {

    send "cat /tmp/id_rsa.pub >> /home/$name/.ssh/authorized_keys\n"

    }

    expect ":~#"

    # 操作成功后,退回 SSH 客户端机器

    send "exit\n"

    expect "#"

    # 下面将测试能否自动登录,不用输入密码

    spawn  ssh $host – l$name

    expect  {

    "Welcome" {

    send_user "Auto login the server successfully!"

    }

    "assword:" {

    send_user "failed to login the server!"

    }

    }

    send "ls\n"

    expect ":~#"

    # 退出 $host

    send "exit\n"

    # 程序结束

    expect eof

    清单 7 基于 SSH key 交换的自动化登录脚本注释掉了主机名,用户名和密码,因此用户在运行脚本时需要手工输入主机名,用户名和密码。一旦 SSH Key 的交换完成,用户就可以直接运行 “ssh host-address – l username”实现自动化登录,而不再需要输入用户名密码。

顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论