Advertisement

04.25.2008 at 01:34PM PDT, ID: 23354892
[x]
Attachment Details

Using expect for scp - expect succes only

Asked by trinity12583 in TCL Scripting Language, Unix Networking, KornShell (ksh)

Tags: scp, expect

First of all, I'm an expect newbie.  I wrote an expect (tcl) script a while back to hide prompts for user passwords when a user is not configured for scp on the box I'm trying to scp to.  My original code is below.  However, as you can see my script doesn't check for every possible error that may occur when calling this script to copy multiple files (specifically, losing the connection).

Is there a way to expect any error scp may throw at me, and only exit 0 if the scp was successful?  Do I have to manually add an expect condition for every possible error or is there a way to check scp's return code in conjunction with prompts it may ask for?  

I just don't want to keep re-writing this script every time a new (network-related) error crops up that I didn't anticipate when writing the expect script.

Thanks!

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
# Check for the correct number of arguments
if {$argc < 3} {
   puts "Usage: expect scp.tcl <server name> <file name> <dest dir>"
   exit 1
} elseif {$argc > 3} {
      puts "Too many arguments"
      puts "Usage: expect scp.tcl <server name> <file name> <dest dir>"
      exit 1
}
 
# Get user name and parameters
set user [exec whoami]
set server [lindex $argv 0]
set file [lindex $argv 1]
set destDir [lindex $argv 2]
 
puts "\r\n"
puts "Copying $file to $destDir on $server"
 
# Start scp process
spawn -noecho scp $file $user@$server:$destDir
expect {
   "$user's Password:" {
      puts "\r\n"
      puts "WARNING: $user is not configured for scp on this server."
      puts "Please manually copy $file to $destDir on $server"
      exit 1
   } timeout {
     puts "WARNING: Timeout waiting for scp to complete"
     puts "Please manually copy $file to $destDir on $server"
     exit 1
   } eof {
     exit 0
   }
}
 
Keywords: Using expect for scp - expect succes…
 
Loading Advertisement...
 
[+][-]04.28.2008 at 01:13AM PDT, ID: 21452259

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 07:43AM PDT, ID: 21454451

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.29.2008 at 12:37AM PDT, ID: 21460242

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.29.2008 at 12:44AM PDT, ID: 21460264

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.29.2008 at 12:56AM PDT, ID: 21460305

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: TCL Scripting Language, Unix Networking, KornShell (ksh)
Tags: scp, expect
Sign Up Now!
Solution Provided By: JustUNIX
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628