Find the answer to your Linux question:
Results 1 to 8 of 8
Hi All I want to expect command with for loop in shell script. I have list of server IP's in file and i want to auto login to all and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2012
    Posts
    5

    Expect Command with for loop in Shell script


    Hi All

    I want to expect command with for loop in shell script.
    I have list of server IP's in file and i want to auto login to all and run the scripts one by one on all servers till the lis is finished.

    Please suggest the script for the same.

    Thanks
    Samit

  2. #2
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    280
    What is:

    Expect Command?

    Give example please.

  3. #3
    Just Joined!
    Join Date
    Dec 2012
    Posts
    5
    Miven

    Below is the example of expect command
    #!/usr/bin/expect
    set timeout 20

    #set ip [lindex $argv 0]

    #set user [lindex $argv 1]

    #set password [lindex $argv 2]
    set user root
    set ip garlic
    set password l00nie
    spawn ssh "$user\@$ip"

    expect "Password:"

    send "$password\r";

    interact

    above is to expect password of single IP

    but i need it for multiple IP's


    For that i need to use for loop, which i am not able to do.

    Please suggest the expect with for loop

  4. #4
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    114
    I strongly suggest you use "ssh" instead since then you don't need "expect" at all. It's really much simpler and allows you to create easier to understand (and maintain IMO) scripts.

  5. #5
    Just Joined!
    Join Date
    Dec 2012
    Posts
    5
    Hi cnamejj

    thanks for suggestion. please send me some example

    thanks

  6. #6
    Just Joined!
    Join Date
    Jan 2010
    Posts
    40
    Quote Originally Posted by cnamejj View Post
    I strongly suggest you use "ssh" instead since then you don't need "expect" at all. It's really much simpler and allows you to create easier to understand (and maintain IMO) scripts.
    Some ssh server implementations don't support this. I had to write similar script to log into Ruckus ZoneDirect 1100 wireless controller.

    Quote Originally Posted by Samitbaghla View Post
    above is to expect password of single IP
    but i need it for multiple IP's
    For that i need to use for loop, which i am not able to do.
    Please suggest the expect with for loop
    I don't know the answer but a workaround -- use expect-script from inside of bash-script. Expect-script is able to get cmd-line params, AFAIK.

  7. #7
    Just Joined!
    Join Date
    Mar 2007
    Location
    Melbourne, Australia
    Posts
    29
    Quote Originally Posted by Samitbaghla View Post
    Hi cnamejj

    thanks for suggestion. please send me some example

    thanks
    Here's how I'd do it:

    Code:
    #!/bin/bash
    ips=`cat <<EOF
    ip1
    ip2
    ip3
    EOF`
    username="blahblah"
    password="password"
    for ip in $ips; do
    # use HEREDOC to embed your expect script as below
    /usr/bin/expect <<EOF
    spawn ssh $ip
    # implement all your expect related commands
    # between the EOF markers
    # reference any variable defined outside the EOF
    # markers as you usually do in bash - $varname
    EOF
    Read more about HEREDOC at Wikipedia.
    Hope that makes sense, otherwise ask after you've tried and can't get it to work...

    You can even avoid using username and password by configuring your ssh server to accept your public key!

    Cheers,
    tkb.

  8. #8
    Just Joined!
    Join Date
    Dec 2012
    Posts
    5
    Thanks All its done with some internal pre designed scripts

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •