Sunday, October 24, 2010

How to login into the server using bash script?

Here is the script :

===
#!/bin/bash

HOST="remote-hostname"
USER="remote-user"
PASS="remore-user-password"
CMD=$@

VAR=$(expect -c "
spawn ssh -o StrictHostKeyChecking=no $USER@$HOST $CMD
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
expect eof
")
echo "==============="
echo "$VAR"
===

Try :)

4 comments:

  1. it shows command not found

    ReplyDelete
  2. You need to assign the values of the following variables like :

    HOST
    USER
    PASS

    Then try to execute and let me know the results.

    ReplyDelete
  3. it logs in and then logs out.. uve just put $VAR in an echo command. so it jus displays rite? how to be in that shell?

    ReplyDelete
  4. Better & more secure way to do that would be 1stly to setup an
    ssh key with no passphase.

    then instead of using the spawn & expect commands just use plain old ssh
    to pass the command to the remote server

    # ssh user@srv.domain.com 'command to run on remote server'

    What ever is inside the ' ' (single quotation marks) will be executed
    on the remote server

    ReplyDelete