Find the answer to your Linux question:
Results 1 to 5 of 5
Hello, I am not new to bash scripting but I'm new to advanced scripting. I am writing a rather large script (for me) and I'm taking it one step at ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2013
    Posts
    6

    Help with BASH script using EXPECT to TELNET


    Hello,

    I am not new to bash scripting but I'm new to advanced scripting. I am writing a rather large script (for me) and I'm taking it one step at a time. I am just learning to use EXPECT to start a telnet session from my script, run a command, and save the output to a variable. I then want to release control from EXPECT so I can do some word with AWK back in the BASH sector of my script. I am having trouble with two things: saving the output of my command to a variable that I can work with back in bash, and also for some reason the "q" that I'm sending to quit the telnet session does not seem to be causing the telnet session to quit. My script is below.
    Code:
    #! /bin/bash
    # This script will ask for testbed, terminal server and line numbers
    # and then get the serial numbers for that line number and save them
    # in a text file in ~/ICT/<testbed>-ICT
    echo "This script will ask for testbed, terminal server (hostname or IP),"
    echo "and line numbers and get the serial numbers and save them in a file in"
    echo "~/ICT/<testbed>-ICT"
    echo ""
    echo "Enter the testbed name: "
    read TESTBED
    export TESTBED
    echo ""
    echo "Enter the terminal server (hostname or IP): "
    read TERMSERVER
    export TERMSERVER
    echo ""
    #echo "Enter the line numbers: "
    #read LINE
    #echo ""
    echo $TERMSERVER: > ~/ICT/$TESTBED-ICT
    echo "Scanning $TERMSERVER 60$LINE and saving to ~/ICT/$TESTBED-ICT"
    echo "Scanning..."
    expect << EOFEXPECT
    set timeout 20
    spawn telnet $TERMSERVER
    expect "Password:"
    send "lab\r"
    expect ">"
    send "show inventory\r"
    expect ">"
    set showinv $expect_out(buffer)
    send "q\r"
    EOFEXPECT
    echo "Returned control to BASH"
    OUTPUT=$showinv
    echo $OUTPUT
    ----
    OUTPUT FROM RUNNING IT:
    Code:
    [fedora-nis-client ~]$ getsn
    This script will ask for testbed, terminal server (hostname or IP),
    and line numbers and get the serial numbers and save them in a file in
    ~/ICT/<testbed>-ICT
    
    Enter the testbed name:
    r1
    
    Enter the terminal server (hostname or IP):
    r1-cs
    
    Scanning r1-cs 60 and saving to ~/ICT/r1-ICT
    Scanning...
    spawn telnet r1-cs
    Trying 192.168.100.1...
    Connected to r1-cs.
    Escape character is '^]'.
    
    
    User Access Verification
    
    Password:
    r1-cs>show inventory
    NAME: "2621 chassis", DESCR: "2621 chassis"
    PID: C2611 - 2FE       , VID: 2.2, SN: FTX0945W0MY
    
    NAME: "Unknown", DESCR: "Unknown"
    PID:                   , VID:    , SN:
    
    
    r1-cs>Returned control to BASH
    Last edited by atreyu; 10-07-2013 at 08:53 PM. Reason: added CODE tags to aid in readability

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hello and welcome, jwcalifo!

    what i would normally do in a case like this is set up a separate script to do the expect work, one that takes arguments, and then have my bash script call it - easy to capture STDOUT that way. but if you really want to embed it, you could put the whole expect code in a bash function, then save the output of that function to a Bash variable. here's an example using sftp, but the mechanics are the same:

    Code:
    #!/bin/bash
    
    # the expect function (called later)
    exp_scr() {
    
      # these are the args retrieved from user input
      local arg1=$1
      local arg2=$2
      local arg3=$3
    
      # start the expect code
      expect <<EOF
    set USER $arg1
    set PASS $arg2
    set HOST $arg3
    
    set timeout -1
    
    spawn -noecho /usr/bin/sftp \$USER@\$HOST
    
    expect "password:"
    set igot \$expect_out(buffer)
    
    send "\$PASS\n";
    expect "sftp>"
    set igot \$expect_out(buffer)
    
    send "ls -al /tmp\n";
    expect "sftp>"
    set igot \$expect_out(buffer)
    
    send "quit\n";
    exit 0
    EOF
    # done with expect code
    
    }
    
    # get user input
    read -p "give me arg1: " arg1
    read -p "give me arg2: " arg2
    read -p "give me arg3: " arg3
    
    # call the expect function
    out=$(exp_scr $arg1 $arg2 $arg3)
    
    # show the output (and grep on it, etc.)
    printf "$out\n"
    Notice that, in the expect function, you have to escape the Expect-defined variables with a backslash, but the 3 Bash variables that you are passing to the function must not be escaped b/c you are using the actual values there.

  3. #3
    Just Joined!
    Join Date
    Oct 2013
    Posts
    6
    Hey atreyu, thank you for this! So let me see if I understand. The value of $out at the end will be the output of the command (in your case, "ls -l /tmp")? What is the "igot' variable? Is that something you came up with or is this a pre-defined keyword of some sort? Thanks!

    JW

    PS: Why did you set "igot" 3 times if we only need to capture the output of the final command? (In my case there is only one command to capture...)

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by jwcalifo View Post
    The value of $out at the end will be the output of the command (in your case, "ls -l /tmp")?
    yes

    What is the "igot' variable?
    it is an Expect variable name that I made up - the variable is not available to the outer Bash shell.

    Why did you set "igot" 3 times if we only need to capture the output of the final command?
    it is part of trouble-shooting code that i removed for brevity. basically, if anything goes wrong with the given expect command, then I would spit out the contents of the $igot variable.

  5. #5
    Just Joined!
    Join Date
    Oct 2013
    Posts
    6
    Cool, thank you very much. I will try this!

    JW

Posting Permissions

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