0

I have a requirement to create an automation script which will do the below stuff in given order :

  1. Copy some script & XML from source to destination.
  2. Run the script in the destination
  3. Copy the results (which are files) from the destination to source.

I am using "expect" to copy files from source to destination. Then I am trying to establish ssh connection towards the destination and execute script in the destination. Once we have result generated, I will again copy files using "expect" but this time from destination to source.

I was wondering if there is any better way of doing the same.

Code to copy the files using "expect" as I need to pass password for scp :

#!/usr/bin/expect -f

set from [lindex $argv 0]
set to [lindex $argv 1]
set pass [lindex $argv 2]
puts "$from, $to and $pass ."
# connect via scp
spawn sudo scp  -v -r $from $to
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send $pass\n
}
}
interact
sauumum
  • 1,638
  • 1
  • 19
  • 36
  • *Why* are you using `expect` for this at all? Showing your code would make this a more concrete, less vague question. – Charles Duffy Apr 24 '18 at 13:18
  • ...also, details matter -- if the script can use a FIFO rather than a concrete file as input, you might be able to not copy the file at all, and only stream it over the network. – Charles Duffy Apr 24 '18 at 13:20
  • ...if your question is "how do I copy a file over a network", for example, we already have plenty of Q&A entries showing that -- *without* use of `expect`. – Charles Duffy Apr 24 '18 at 13:20
  • If you want to always answer "yes" to questions from scp, tell it not to ask those questions in the first place; ie. `scp -o ScriptHostChecking=no`. If you want to pass a password, use `sshpass`, or -- much better -- use RSA key authentication. There's no point to `expect` here. – Charles Duffy Apr 24 '18 at 13:26
  • Arguably this is duplicative of [How to pass password to scp?](https://stackoverflow.com/questions/50096/how-to-pass-password-to-scp) – Charles Duffy Apr 24 '18 at 13:28
  • Also note that questions about how to improve working code are generally not welcome here -- those are on topic at our sister site [codereview.se], subject to the detailed guidance at https://codereview.stackexchange.com/help/on-topic. Per https://stackoverflow.com/help/on-topic, a StackOverflow question should revolve around a specific practical, answerable problem; "how can this be improved?" is not a problem. – Charles Duffy Apr 24 '18 at 13:30
  • Hello @CharlesDuffy, I need to use 'expect' as I need to pass the password while doing scp. As you can see in my question, I am doing "scp" twice (once from src to dest and then copying results from dest to source) and establishing "ssh" once to execute one script command, I am just checking if there is better way to do the same. I have added the code to copy the files. – sauumum Apr 24 '18 at 13:31
  • You do not need `expect`. `sshpass` will pass in the password for you. – Charles Duffy Apr 24 '18 at 13:32
  • 1
    What kind of "better way" are you looking for, though? Do you just want to use a single authenticated transport for all three channels? You can do that -- see the ControlMaster/ControlSocket system, as documented at https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing -- that way you only set up a single SSH connection (authenticating just once), and do all three operations over it. – Charles Duffy Apr 24 '18 at 13:33
  • My environment is RHEL and I am not supposed to install anything on it. I checked 'sshpass' but it is not available by deault. – sauumum Apr 24 '18 at 13:33

1 Answers1

0

Your solution is ok. You could consider to use sshfs to mount remote directory on src machine and use command like this file was on src machine.

example:

sudo sshfs -o allow_other,nonempty user@10.1.2.3:/project /project
Bartłomiej Bartnicki
  • 1,067
  • 2
  • 14
  • 30