您现在的位置: 365建站网 > 365文章 > Linux 实现自动登陆远程机器的方法

Linux 实现自动登陆远程机器的方法

文章来源:365jz.com     点击数:267    更新时间:2017-08-23 12:35   参与评论

在 Linux 下进行远程登陆的时候,总是要进行 ssh 输入账号密码,相对比较繁琐。而在工作中总会先从本地登陆到公司的中间机器(跳板机)然后才能登陆到线上的机器,每次操作更加繁琐。如果从 A -> B 可以直接进行建立相互的信任关系来解决面输入密码的问题。显示情况如果 A -> B -> C 三台机器,如果想从 A 直接到 C 只能通过 B 进行登录,这样就无法建立 A -> C 的信任关系(这种操作主要是为了保护线上机器不能随便登录)。该脚本就是解决这种有多个依赖的关系。

注意事项:

1. 使用实时 bash version >= 4.0,因为配置中需要使用关联数据

2. 如果需要全局使用直接修改 autologin 的名字,移动到 PATH 路径下即可 eg: mv autologin /usrl/local/bin/to(改为自己想要使用的名字)

脚本代码:

</>code

  1. #!/usr/local/bin/bash
  2. # @Version 0.3.1
  3. # @filename to
  4. # 修复等不需要要配置跳板机的时候执行命令,在配置跳板机位置默认填 no 即可
  5. # @Author pemakoa@gmail.com
  6. # Bash version >= 4.0 使用关联数组
  7. # Usage: host user passwd port jump_host command
  8. # 四种情况如下:
  9. # 1. 直接登录目标机器 如 A
  10. # 2. 需要中间机器登陆到目标机器 如 C, 其中 B 为中间机器,会先登录 B在从 B登陆到 C然后执行 command
  11. # 3. 直接登录目标机器并执行相应的命令 如 D
  12. declare -A _server_config
  13. _server_config['A']="a_host a_user a_passwd a_port"
  14. _server_config['B']="b_host b_user b_passwd b_port"
  15. _server_config['C']="c_host c_user c_passwd c_port B '(command eg) ls .'"
  16. _server_config['D']="d_host d_user d_passwd d_port no 'cd /home && ll'"
  17. _config_keys=(${!_server_config[@]})
  18. _length=${#_server_config[@]}
  19. _login_server=$1
  20. _config_status=false
  21. # 是否输入登陆机器
  22. if [ "$_login_server" == "" ];then
  23. echo -e "\033[40m\033[31m Please input login server, you can choose one follows list \033[0m"
  24. for i in "${_config_keys[@]}";do
  25. echo -e "\033[41;37m $i \033[0m "
  26. done
  27. exit
  28. fi
  29. # 检查登陆的机器是否配置
  30. for i in "${_config_keys[@]}";do
  31. if [ "$_login_server" == "$i" ];then
  32. _config_status=true
  33. fi
  34. done
  35. if [ "${_config_status}" == "false" ];then
  36. echo -ne "\033[40m\033[31m
  37. Not config server info ...
  38. Please config in _server_config like
  39. Host User Passwd Port Jump Command\033[0m"
  40. exit
  41. fi
  42. # 登陆 如果配置跳板机,先登陆跳板机在登陆到目标机器
  43. _host=$(echo ${_server_config["${_login_server}"]} | awk '{print $1}')
  44. _user=$(echo ${_server_config["${_login_server}"]} | awk '{print $2}')
  45. _passwd=$(echo ${_server_config["${_login_server}"]} | awk '{print $3}')
  46. _port=$(echo ${_server_config["${_login_server}"]} | awk '{print $4}')
  47. _jump=$(echo ${_server_config["${_login_server}"]} | awk '{print $5}')
  48. _command=$(echo ${_server_config["${_login_server}"]} | awk -F"'" '{print $2}')
  49. if [ "${_command}" != "" ]; then
  50. _command="expect \"*]*\"
  51. send \"${_command}\r\""
  52. fi
  53. if [ "${_jump}" != "" ] && [ "${_jump}" != "no" ]; then
  54. _jump_host=$(echo ${_server_config["${_jump}"]} | awk '{print $1}')
  55. _jump_user=$(echo ${_server_config["${_jump}"]} | awk '{print $2}')
  56. _jump_passwd=$(echo ${_server_config["${_jump}"]} | awk '{print $3}')
  57. _jump_port=$(echo ${_server_config["${_jump}"]} | awk '{print $4}')
  58. expect -c "
  59. set timeout 30
  60. spawn ssh -p${_jump_port} ${_jump_user}@${_jump_host}
  61. expect {
  62. \"yes/no\" { send \"yes\r\"; exp_continue }
  63. \"assword\" { send \"${_jump_passwd}\r\" }
  64. }
  65. expect \"*]*\"
  66. send \"ssh -p${_port} ${_user}@${_host}\r\"
  67. expect \"assword:\"
  68. send \"${_passwd}\r\"
  69. ${_command}
  70. interact"
  71. else
  72. expect -c "
  73. set timeout 30
  74. spawn ssh -p${_port} ${_user}@${_host}
  75. expect {
  76. \"yes/no\" {send \"yes\r\"; exp_continue }
  77. \"*assword:\" { send \"$_passwd\r\" }
  78. }
  79. ${_command}
  80. interact
  81. "
  82. fi

GitHub代码地址: https://github.com/pemako/LearnShell/blob/master/lib/autologin
 

另外的方法:Linux中使用expect脚本实现远程机器自动登录

首先创建一个expect脚本ssh_expect,文件内容如下:

</>code

  1. #!/usr/bin/expect -f
  2. set hostname [lindex $argv 0]
  3. set user [lindex $argv 1]
  4. set passwd [lindex $argv 2]
  5. set timeout 30
  6. set force_conservative 1
  7. if {$force_conservative} {
  8. set send_slow {128 .1}
  9. }
  10. spawn ssh $user@$hostname;
  11. expect {
  12. "*continue connecting (yes/no)?" { send -s "yes\r"; exp_continue }
  13. "*assword:" {
  14. send -s "$passwd\r";
  15. }
  16. }
  17. interact

然后定义一些命令别名,比如:

</>code

  1. alias h101='ssh_expect 192.168.0.101 <user> <password>'
  2. alias h102='ssh_expect 192.168.0.102 <user> <password>'

这些别名可以写到~/.bashrc文件中

然后执行 h101 就可以自动登录192.168.0.101机器了。



如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (267人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号