• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 603
  • Last Modified:

Expect Script Linux

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
Aanvik
Asked:
Aanvik
  • 6
  • 3
  • 2
1 Solution
 
gremwellCommented:
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
 
Duncan RoeSoftware DeveloperCommented:
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
 
gremwellCommented:
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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Duncan RoeSoftware DeveloperCommented:
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
 
AanvikAuthor Commented:
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
 
Duncan RoeSoftware DeveloperCommented:
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
 
Duncan RoeSoftware DeveloperCommented:
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
 
AanvikAuthor Commented:
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
 
Duncan RoeSoftware DeveloperCommented:
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
 
Duncan RoeSoftware DeveloperCommented:
Or just get rid of the "No idea" line while you're testing.
0
 
AanvikAuthor Commented:
Thank you For your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 6
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now