shellスクリプトはssh自動登録機能を共有することを実現します。


ファイル名:ssh_autoロゴ

#!/usr/bin/expect
##
# ssh
#
# @author zhiyuan <hzyhouzhiyuan gmail.com>
##
if {$argc<4} {
 puts "Error params: $argv"
 puts "Expect params :user passwd ip port [translate_id]"
 exit 1
}

set default_passcode " "

set user [lindex $argv 0]
set password [lindex $argv 1]
set ip [lindex $argv 2]
set port [lindex $argv 3]
set timeout 10

while 1 {
 spawn ssh -p $port $user@$ip
 # \r
 expect {
  "*yes/no" { send "yes\r";exp_continue}
  "*password:" { send "$password\r" }
 }
        # , 。
 expect {
  "*PASSCODE:" {
   send_user " :";
   expect_user -re "(.*)
"
   set random_passcode $expect_out(1,string)
   send "$default_passcode$random_passcode\r"
   expect {
    "Access Denied" { continue }
    "Enter:" { send "1\r" }
   }
   set translate_ip [lindex $argv 4]
   if { $translate_ip != "" } {
    expect "*):" { send "$translate_ip\r" }
   }
  }
  #"Last login:*" { }
 }
 break
}
# $,
#expect -re "*\$" { puts "test123"; send "source /etc/profile\r" }
#expect "*\$" { send "cd ~\r" }
send_user "login success!"
interact
上はsshの自動登録で、下のshellに合わせて使うことができて便利です。ファイル名:xxx_launcher

#!/bin/sh
##

#
# @author zhiyuan <[email protected]>
##
channel_user="user_namexxx"
channel_passwd="xxxx"
#
internal_ip1=xxx.xxx.xxx.xxx
#
unicom_ip1=xxx.xxx.xxx.xxx
#
telecom_ip1=xxx.xxx.xxx.xxx
case "$1" in
 ci)
  expect ssh_auto_login $channel_user $channel_passwd $internal_ip3 22

 cl)
  expect ssh_auto_login $channel_user $channel_passwd $unicom_ip1 22

 cd)
  expect ssh_auto_login $channel_user $channel_passwd $telecom_ip1 22

 149)
  expect ssh_auto_login channel_user channel_passwd xxx.xx.xxx.xxx 22

 49)
  expect ssh_auto_login $channel_user $channel_passwd $unicom_ip1 22 ipxxx.xxx.xx

 *)
  echo " :"
  echo "\tthere is not a server named [$1]"
  echo "\t 149:\t149"
  echo "\t 49:\t49"

esac
この時、あるサーバーにログインする時は、上のshellで登録したいサーバのパラメータをそのまま使えばいいです。launcher 49