?
Solved

Expect Script Linux

Posted on 2010-09-09
11
Medium Priority
?
562 Views
Last Modified: 2012-05-10
I have a simple question for  expect script...

I am automating SFTP and need some help... There could be multiple sftp location so in my expect script I need to check what return I am getting...and process is accordingly.
for example
expect {
"result1" {send "Command 1"}
"result2" {send "Command 2"}
else
               {send "bad command"}

Any help?
0
Comment
Question by:Aanvik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
11 Comments
 
LVL 3

Expert Comment

by:gremwell
ID: 33644237
The following script should do the trick:

expect {
      "result1\n"      {send "Command 1\n"}
      "result2\n"      {send "Command 2\n"}
      -re ".*\n"              {send "Bad command\n"}
}
0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 33645092
I agree with gremwell that it's a good idea to wait for all characters before sending anything, but I rather suspect you will be getting a CrLf sequence back so you should code

expect {
      result1\r\n      {exp_send "Command 1\r"}
      result2\r\n      {exp_send "Command 2\r"}
      \r\n                  {puts "Unrecognised result"}
}


If you are in any doubt about what is being returned to expect, insert the line exp_internal 1 before the expect line. That will clarify whether you receive CrLf or just Lf for instance

Notes (in order of appearance):-

1. Everything in tcl (or expect) is a string. You only need double-quotes if it contains spaces.
2. I recommend getting in the habit of using exp_send rather than send. That way your scripts will also work under expectk (where Tk owns the send command)
3. I always terminate send strings with Cr rather than Lf. Lf often works, but only because the underlying serial settings translate it to Cr
4. It's not an error to use a regular expression match of the characters before CrLf of an unexpected response, but it's not necessary either
5. Did you really want to send "bad command" back to the application on getting an unrecognised response? I assumed you didn't, so changed it to printing a message the user to see.
0
 
LVL 3

Expert Comment

by:gremwell
ID: 33645183
Regarding point 3: The way I understand OP, the goal is to wrap the expect script around SFTP tool. There is no serial hardware involved. In *nix world lines are typically terminated with LF. Your point 4 is valid indeed.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 35

Expert Comment

by:Duncan Roe
ID: 33645860
Text Files in the *nix world are typically terminated with Lf (often called Nl (newline) in this context (what the n in \n stands for)). Yes there is no serial hardware involved, but exp_send output goes to a pty which is the controlling terminal of the spawned application. Therefore it is appropriate to view that stream as serial data. By default, serial output lines end with CrLf, because the default stty setting includes onlcr. exp_internal 1 will verify this.
My point 3 was slightly off the mark regarding Cr on exp_send lines - the default stty setting includes icrnl which translates Cr to Lf on input. So Lf would work fine. But I think Cr on the (real or virtual) wire is the norm - many modems only respond to Cr (not to Lf) for instance
0
 
LVL 5

Author Comment

by:Aanvik
ID: 33651621
Hi
Thank you for your comments.. this is what I am looking for. Pls have a look at the code... depending upon the user the output could be different so I am trying this script... Is this correct way of doing it?

Thx
 
#!/usr/bin/expect
set timeout 50
spawn sftp fts@jlc031
expect {
    "fts@jlc031's password: " {send "fts\n"}
    "sftp> " {send "\n"}
    -re ".\n" {send "No idea\n"}
    }
send "put some file name\n"
send "bye\n";
interact

Open in new window

0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 33651653
Why have interact at the end? You have already sent bye.
You need to wait for the sftp> prompt after every send
You don't need to send a blank line if you get the prompt first up.
You have chosen to ignore my points 2 & 4, so be it:-



#!/usr/bin/expect set timeout 50 spawn sftp fts@jlc031 expect { "fts@jlc031's password: " {send "fts\n"} "sftp> " {send "\n"} -re ".\n" {send "No idea\n"} } send "put some file name\n" send "bye\n"; 

Open in new window

0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 33651685
Sorry about that - clicked Submit too soon. Had a problem with paste, try again:-
(no need for semicolon after send of bye)
(otherwise comments as in previous post)
(eof is special for when spawned program exits)
#!/usr/bin/expect
set timeout 50
spawn sftp fts@jlc031
expect {
    "fts@jlc031's password: " {send "fts\n"}
    "sftp> " {}
    -re ".\n" {send "No idea\n"}
    }
send "put some file name\n"
expect "sftp> "
send "bye\n"
expect eof

Open in new window

0
 
LVL 5

Author Comment

by:Aanvik
ID: 33651784
Hello,

Thank you for the code..

Can you please have a look at this screen shot..

For some reason its thinking "connecting to jlc031" is a response from sftp and it needs to do nothing and failing..

I have added this "connecting to jlc031" in code because some sftp it doesn't need a password and just print message ""connecting to jlc031"  and connects ..

I am showing both script and manual entry... Please help here...
sftp.JPG
0
 
LVL 35

Accepted Solution

by:
Duncan Roe earned 2000 total points
ID: 33651909
The "No idea" line is tripping you up. In fact it might be tripping even earlier - when the sftp command is echoed back to you
As I mentioned before, exp_internal 1 is your friend when debugging expect scripts.
The script below is a bit of a guess therefore
 
#!/usr/bin/expect
set timeout 50
spawn sftp fts@jlc031
expect \n
expect {
    "fts@jlc031's password: " {send "fts\n"}
    "sftp> " {}
    -re Connecting[^\n]*\n {}
    \n {send "No idea\n"}
    }
send "put some file name\n"
expect "sftp> "
send "bye\n"
expect eof

Open in new window

0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 33652468
Or just get rid of the "No idea" line while you're testing.
0
 
LVL 5

Author Closing Comment

by:Aanvik
ID: 33653747
Thank you For your help.
0

Featured Post

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question