Link to home
Start Free TrialLog in
Avatar of Aanvik
Aanvik

asked on

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?
Avatar of gremwell
gremwell

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"}
}
Avatar of Duncan Roe
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.
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.
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
Avatar of Aanvik

ASKER

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

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

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

Avatar of Aanvik

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or just get rid of the "No idea" line while you're testing.
Avatar of Aanvik

ASKER

Thank you For your help.