≡ Menu

SSH login expect shell script to supply username and password

Expect is a Unix and Linux automation and testing tool. It works with interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and many others. It uses Unix pseudo terminals to wrap up subprocesses transparently, allowing the automation of arbitrary applications that are accessed over a terminal. A simple expect script to supply OpenSSH root/admin password for remote ssh server and execute the Unix / Linux / BSD commands. First, you need to install expect tool by following these instructions.

Other options

  • Use sshpass command provide the password and username for ssh based login using the mode referred to as “keyboard-interactive” password authentication, but in non-interactive mode.
  • OpenSSH offers RSA and DSA authentication to remote systems without supplying a password. keychain is a special bash script designed to make key-based authentication incredibly convenient and flexible.
{ 88 comments… add one }
  • Sabarish July 8, 2016, 10:17 am

    HI

    #!/bin/sh

    set password “xxxxXxx”

    if [ -n “${SOURCE_FILES}” ]

    then

    for f in ${SOURCE_FILES}

    do

    cp -prf ${f} ${DESTINATION}

    done

    spawn sftp ${USER}@${HOST}:${CHANGEDIRECTORY} << ${SFTPLOGS}/sftplogs_${LOG_DATE}.log

    expect “Password”

    send “$Passwordr”

    else

    echo “NO FILES TO MOVE FROM SOURCE”

    fi

    Is the above script okay?

  • LearningExpect July 3, 2014, 2:10 pm

    I have a problem with Expect. I am supposed to do the following task which involves running a command script in Solaris. This command then asks the user two passwords (one after another). I am using Expect to do this task. However, I am unable to get the desired output and the script is failing. It is unable to send the password values

    #!/usr/bin/expect
    set pri_user_password [lindex $argv 0]
    set sec_user_password [lindex $argv 1]

    spawn -f
    expect “Password for property local_service_password : ” {
    send “$pri_user_passwordr” }
    expect “Password for property remote_service_password: ” {
    send “$sec_user_passwordr” }

    Can you tell me what the problem might be?

  • sporo July 16, 2013, 12:15 pm

    Hi Guys, can someone help with this:

    set user “touch”;

    set password “1touch@1”;

    set gateway “10.34.5.6”;

    spawn /usr/bin/ssh -L 22:localhost:22 $user@$gateway

    But this doesn’t work and i seem to be getting no result but this:

    usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]

    [-D [bind_address:]port] [-e escape_char] [-F configfile]

    [-i identity_file] [-L [bind_address:]port:host:hostport]

    [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]

    [-R [bind_address:]port:host:hostport] [-S ctl_path]

    [-w tunnel:tunnel] [user@]hostname [command]

  • Raghava October 18, 2012, 1:54 pm

    Hi,
    I installed sshdpass tool in RHEL and trying to connect another RHEL machine where sshd is running. I am able to connect when I run $ sshpass -p” ssh root@IP but not connecting to that IP when I tried by placing the same line in a connect_to_remote.sh file and called it through sudo command. It thrown error.. connect_to_remote.sh: line 4: sshpass: command not found

    Please guide me the solution if any one know.
    Thanks,
    Raghav

    • Abhinav Chittora January 9, 2013, 1:38 am

      Hi Raghava,

      You should try full path to sshpass i.e. /usr/bin/sshpass. Since it may be the reason that the shell, which is running your script is not looking for the directory in which sshpass is located. Using absolute path is always prefered way to use any such command.

      Thanks,
      Abhinav Chittora

  • Michelle Kelly September 25, 2012, 5:39 pm

    I am new to autoexpect and I am having issues with the script that I have created. The process does not complete fully and I am not sure how to fix this. It will not move the database files into the appropriate location.

    Script:
    spawn /bin/bash
    [root@hp36ed01 bin]# ./installdb_unc
    logname: no login name
    ======================================================================

    — Universal Addressing Module Installation —

    The environment for Database Installation is currently set to:

    Distribution type: cd
    Mounted CD directory: /holding/UNC/db

    Press enter to continue.

    CDQ Platform – Universal Addressing Module Installation Menu

    1. US Subscription
    2. Canadian Subscription
    3. International Subscription

    99. Exit

    Enter the number of the product that you want to load
    the subscription database for and then press enter: 1

    US Postal Database Menu
    ======================================
    Database Installation

    1. Subscription Database
    2. Delivery Point Validation
    3. Residential Delivery Indicator
    4. Early Warning System
    5. LACSLink Database
    6. SuiteLink Database

    99. Exit

    Enter the number of the type of data you want to load
    and then press enter: 1

    The database load environment is currently set to:

    Database input file location: /holding/UNC/db
    Database output location: /opt/UNC

    Enter c to (c)ontinue
    or m to (m)odify database input/output locations
    or q to (q)uit

    ===> m

    Which do you want to change?

    Enter 1 to change the input file location
    or 2 to change the database output location
    or 3 to change both

    ===> 2

    Please enter full path where you would like to install
    the CODE-1 Plus database ==> /usr/UNC/db/test12012

    The new CODE-1 Plus database location will be: /usr/UNC/db/test12012
    Is this correct?

    Enter (y)es to continue.
    (n)o to try again.

    ===> y
    /opt/UNC/server/bin/setup is updated.
    /opt/UNC/server/bin/setup is updated.

    Do you wish to load the optional ELOT file?
    Enter (y)es
    (n)o

    ===> n
    *************************************
    CODE-1 PLUS DATABASE INSTALLATION

    Installed on Tue Sep 25 12:23:01 CDT 2012

    Creating CODE-1 Plus Database
    Which do you want to change?\r
    \r
    Enter 1 to change the input file location\r
    or 2 to change the database output location\r
    or 3 to change both\r
    \r
    ===> ”
    send — “2\r”
    expect -exact “2\r
    [H[2J\r
    Please enter full path where you would like to install\r
    the CODE-1 Plus database ==> ”
    send — “/usr/UNC/db/test12012\r”
    expect -exact “/usr/UNC/db/test12012\r
    \r
    The new CODE-1 Plus database location will be: /usr/UNC/db/test12012\r
    Is this correct?\r
    \r
    Enter (y)es to continue.\r
    (n)o to try again.\r
    \r
    ===> ”
    send — “y\r”
    expect -exact “y\r
    /opt/UNC/server/bin/setup is updated.\r
    /opt/UNC/server/bin/setup is updated.\r
    [H[2J\r
    Do you wish to load the optional ELOT file?\r
    Enter (y)es\r
    (n)o\r
    \r
    ===> ”
    send — “n\r”
    expect -exact “n\r
    [H[2J*************************************\r
    CODE-1 PLUS DATABASE INSTALLATION\r
    \r
    Installed on Tue Sep 25 12:21:28 CDT 2012\r
    \r
    Creating CODE-1 Plus Database\r
    \r
    [?47h[r[m[2J[H[?7h[?1;3;4;6l[?1h[m[H[2J[24;1H[H[2JPlease press Enter to continue…..\r

    send — “\r”
    expect -exact “\r
    \r
    \r
    Database loading. Please wait….\r
    \r
    [1;1H \r
    \r
    \r
    [24;1H\r
    [2J[?47l*************************************\r
    DATABASE FILES LOADED SUCCESSFULLY\r
    END CODE-1 PLUS DATABASE INSTALLATION\r
    *************************************\r
    Tue Sep 25 12:22:19 CDT 2012\r
    ]0;root@hp36ed01:/opt/UNC/server/bin\[root@hp36ed01 bin\]# ”
    send — “”
    expect eof

    I should see several files in the /usr/UNC/db/test12012 location. After the script runs I dont see the files. Any help will be appreciated. Thanks in advance!

  • Piviul September 11, 2012, 8:41 am

    …oops I’ve found that doesn’t work if used with scp like “./bin/pwd_cmd.exp password scp root@host:/path /ptah/to/dest” because I can’t write : in a argument… :(((

    Piviul

Security: Are you a robot or human?

Leave a Comment