문제

I am new to scripting. How can I write an Expect script to ssh into a device and prompt the user for password? We use a pin + RSA token code as the password so I can't store the password.

#!/usr/bin/expect -f

spawn ssh device1
도움이 되었습니까?

해결책 2

I have used this code before to achieve what you wish to achieve. You can take it as a reference point to write your own version of the code.

#!/usr/bin/env expect -f
set passw [lindex $argv 0]

#timeout is a predefined variable in expect which by default is set to 10 sec
set timeout 60 
spawn ssh $user@machine
while {1} {
  expect {

    eof                          {break}
    "The authenticity of host"   {send "yes\r"}
    "password:"                  {send "$password\r"}
    "*\]"                        {send "exit\r"}
  }
}
wait
#spawn_id is another default variable in expect. 
#It is good practice to close spawn_id handle created by spawn command
close $spawn_id

Source: Expect Wiki

다른 팁

You will need to use the interact command to type in the password yourself.

The below skeleton should get you on your way:

set prompt "$"

spawn ssh user@host

set timeout 10
expect {
    timeout {
        puts "Unable to connect"
        exit 1
    }

    #Authenticty Check
    "*(yes/no)?" {
        send "yes\r"
        exp_continue
    }

    "assword: " {
        #Hand control back to user
        interact -o "\r" exp_continue
    }

    "$prompt" {
        puts "Cool by the pool"
    }
}

The below code will ask the user to enter the password and will use it further in the script where a password will be asked. Just as $pass.

Grabbing password to be used in script further

stty -echo
send_user -- "Enter the password: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set pass $expect_out(1,string)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top