Results 1 to 6 of 6

Thread: Taking command line argument in expect script

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Taking command line argument in expect script

    Hi frnds,
    This is my first post.....
    I want to take IP address, username and password as command line arguments to a expect script to telnet. My script is :
    __________________________________________________ ____________________
    ##file name is : script.sh
    #!/bin/bash
    #!/usr/bin/expect -- ##/usr/bin/expect is the directory where expect was installed

    log_user 0 ##this command is used to hide the conversationn between the script and the other machine
    set address [lrange $argv 0 0] ##assign the first passed parameter while calling the script to $address
    set username [lrange $argv 1 1] ##assign the 2nd passed parameter while calling the script to $username
    set password [lrange $argv 2 2] ##assign the 3rd passed parameter while calling the script to $password
    spawn telnet ${address} ##start the telnet session to machine with IP=$address

    ##start conversation with the machine:
    expect "login:"
    send -- "$username\r"
    expect "Password:"
    send -- "$password\r"
    expect "$ "
    send --"exit\r"
    __________________________________________________ ____________________
    im running script as:
    expect script.sh 10.1.101.80 root password

    but it is showing the error :
    can't read "address": no such variable
    while executing
    "set address [lrange $argv 0 0]
    (file "script.sh" line 6)
    __________________________________________________ ____________________
    please help me.....I hv expect and Tcl packages installed on my machine (OS is REL-4)

  2. #2
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,515
    Thanks
    17
    Thanked 815 Times in 511 Posts
    Rep Power
    10

    Default

    Remove #!/bin/bash and try again!
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    I removed #!/bin/bash but still showing same error

  4. #4
    Junior Member
    Join Date
    Apr 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    # works as a comment only if it's the first character on the line, so basically your comments are messing you script.Move each and every one of them to their own line and it should work(or at least spit a different error).

  5. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Thanks for your help fcojocaru ...its working now.
    I hv one more issue:
    I want to store the logs for some commands which i am executing in expect script, to a txt file. I want to check whether the command was wuccessful or it was failed. Log file format:
    command <3> - successful
    command <8> - failed
    ...................................
    command <27> - failed


    How to do this ...plz help.

  6. #6
    Junior Member
    Join Date
    Apr 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    This is trickier; Check the manpage of expect to see how to make a simple decision structure ( if - then - else ) , and check specifically for the log_file directive. There is no simple way to check the $? like in bash , but rather check the output of the executed command ( telnet in your case ).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 31st March 2009, 11:54 AM
  2. Replies: 1
    Last Post: 16th March 2009, 08:46 PM
  3. expect script hangs
    By antzjos in forum Shell scripting
    Replies: 0
    Last Post: 22nd May 2008, 07:37 PM
  4. Expect and tcl - manymaint.exp script
    By Intelligen in forum Shell scripting
    Replies: 11
    Last Post: 21st November 2007, 03:48 PM
  5. Command line remote access
    By angelus_kit in forum Networking, Firewalls and Security
    Replies: 7
    Last Post: 5th September 2007, 07:30 PM

Posting Permissions

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