To write a shell script that will remote login to machineB from machineA

Discussion in 'General Linux' started by Basudeba Mandal, Jun 29, 2013.

  1. Basudeba Mandal

    Basudeba Mandal New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    I wanna write a shell script which will do remote login to a machine,
    If I use ssh command to do that, It asks something like 'do you wanna continue (yes/no)' and then remote machine's password, so
    there we need to input yes and next input is remote machine's password. So my doubt is,
    how to give those two input(yes and remote machine's password) to the system? Can I use the below command

    echo 'yes abcd' | ssh --stdin user25@192.168.1.1

    where 'abcd' is the password of 'user25'(user25 is an user on remote machine 192.168.1.1')

    or I need to give input from file????

  2. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,198
    Likes Received:
    405
    Trophy Points:
    83
    Code:
    #!/usr/bin/expect -f
    # ./sshlogin.exp uptime
    # set Variables
    set user username
    set password yourpassword
    set ipaddr serveripaddress
    set arg1 [lrange $argv 0 0]
    set timeout -1
    # now connect to remote UNIX box (ipaddr) with given script to execute
    spawn ssh $user@$ipaddr $arg1
    match_max 100000 # Look for passwod prompt
    expect "*?assword:*"
    # Send password aka $password
    send -- "$password\r"
    # send blank line (\r) to make sure we get back to gui
    send -- "\r"
    expect eof
    Basudeba Mandal likes this.
  3. Basudeba Mandal

    Basudeba Mandal New Member

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Thanks for your help....
    Actually I am new to shell script. And a just beginner to expect script. So some doubts are coming to my mind.

    set arg1 [lrange $argv 0 0]
    what is this [lrange $argv 0 0]?
    why this setting is needed?

    spawn ssh $user@$ipaddr $arg1
    why $arg1, can we do without that?

    match_max 100000
    whats this match-max?what does it do?
  4. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,198
    Likes Received:
    405
    Trophy Points:
    83
  5. birdboyee

    birdboyee New Member

    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Ryan, can you help me with the following? We use a SecureCRT .vbs script to automate logging into multiple boxes, however, I'd like to have something run in Linux vs. our emulator. Below is my objective:

    Our current .vbs script will look at an IP list .txt file on our PC, then login to each IP within the .txt, and execute the following:

    Code:
    ssh router1@[IP Address]
    su - root  # We get prompted for PW.
    su - secadmin2  # We do not get prompted for PW.
    cd .ssh
    cp authorized_keys authorized_keys_bkup
    cat /app2/.ssh/Vkey >> authorized_keys
    exit
    exit
    cd .ssh
    cp Vkey Vkey.orig
    cat Vkey >> authorized_keys
    rm /app2/.ssh/Vkey
    exit
    Again, the script must prompt for root password (only once since all root PW's will be the same), and loop commands above for all IP's found on file. Can anyone help with this? Your assistance and cooperation is GREATLY appreciated!

Share This Page