LoginSignup
0
2

More than 5 years have passed since last update.

Expectでtelnetのログインを自動化する

Posted at

ssh先でtelnetを使ってログイン必要な環境があったので
expectを使って自動化してみた

ssh_telnet.exp
#!/usr/bin/expect

set Prompt "\[#$%>\]"
set SSHUSER "foo"
set SSHPW "hogehoge"
set TELUSER "bar"
set TELPW "fugafuga"
set timeout 5

spawn env LANG=C ssh ${SSHUSER}@ssh-ip

expect {
  "password:" {
    send "${SSHPW}\n"
  }
}

expect {
    -glob "${Prompt}" {
      send "telnet telnet-ip\n"
    }
}

expect {
  "login:" {
    send "${TELUSER}\r"
    exp_continue
  }

  "Password:" {
    send "${TELPW}\r"
    exp_continue
  }

  ">>" {
    interact
    exit 0
  }
}

ポイントは telent中のEnterは \n ではなく \r になる。
(ちょっとハマった)

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2