3

Possible Duplicate:
Using expect to pass a password to ssh

I want to have ssh connection to a remote machine and instead of using ssh command along with the machine address and password, I just want to write a little function that executes the ssh command to the machine and enters the pass after the server asks it. I can write the ssh part but how can I make the script that enters also the pass when the host ask for it?

Community
  • 1
  • 1
erogol
  • 13,156
  • 33
  • 101
  • 155

2 Answers2

4

You may use expect script. You can pass arguments from cmd line. Sample code I write:

#!/usr/bin/expect

set timeout 100
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]

spawn ssh $username@$host $command
#puts $command
expect {
    "(yes/no)?"
        {
            send "yes\n"
            expect "*assword:" { send "$password\n"}
        }
    "*assword:"
        {
            send "$password\n"
        }
    }
louxiu
  • 2,830
  • 3
  • 28
  • 42
1

You can use Expect tool
It's exactly what you need:

EDIT

#!/usr/bin/expect
set timeout 60
set user "yourName"
set machine "nameOfYourMachine"
set password "yourPassword"
set command "command that you want execute via ssh"
spawn ssh $user@$machine 
while {1} {
expect {
      eof                          {break}
      "The authenticity of host"   {send "yes\r"}
      "password:"                  {send "$password\r"}
      "*\]"                        {send "exit\r"}
      "bash"                        {send "$command"}
}
}
wait
close $spawn_id

Just workaround it as you need

Laser
  • 6,652
  • 8
  • 54
  • 85
  • @Erogol whrite #!/usr/bin/expect as first line – Laser Dec 10 '12 at 13:49
  • still dont work and getting spawn: command not found while{1}{: command not found couldn't read file "{": no such file or directory No command 'eof' found, did you mean: Command 'eog' from package 'eog' (main) Command 'eox' from package 'keyboards-rg' (universe) eof: command not found *ssword:: command not found – erogol Dec 10 '12 at 13:56
  • @Erogol i edit my post check it now – Laser Dec 10 '12 at 14:05