Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a PHP page which runs the following expect script to upload a file to another server on my network:

#!/usr/bin/expect -f

set password [lindex $argv 0]

spawn /usr/bin/scp /home/Dave/bin/test root@192.168.1.1:/etc/test

expect {
    -re ".*yes.*no.*" {
        exp_send "yes\r"
        exp_continue
    }
    -re ".*password.*" {
        exp_send "$password\r"
    }
}

expect eof

Before I do this however I need to backup the existing file on the server:

cp /etc/test /etc/test.bak

And after I upload the file I need to restart the service that reads the file:

/etc/init.d/testservice restart

It seems rather silly to SSH to the server three separate times. How can I do this all at once? Is there a PHP function that will take care of some or all of this for me?

Edit: Prompting the user for a password is a requirement.

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

You can do this by running a single SSH command, but arranging the proper mix of data transfer, command execution and control logic is hard to manage.

It's easier to establish an SSH connection, do your work with whatever mixture of ssh, scp, sftp, rsync, sshfs, … suits you, and finally close the SSH connection.

SSH offers a connection that other SSH processes can piggyback on: a master connection. Start a master connection and keep it running:

ssh -N -M

Pass the credentials as input with expect, since you need to provide a password. Then run other SSH commands as you please. When you no longer need the connection, run ssh -O exit.

To ensure that the SSH commands will be slaves to the existing master connection (and hence require no authentication), pass the same ControlPath option to all SSH invocations. Either pass it as a command line option (-o ControlPath=…, note lowercase) or define it in ~/.ssh/config. The value is a path where ssh will create or look for the socket that allows the slaves and the master to communicate; it needs to be in a writable directory on a filesystem that supports named sockets, and the name needs to be unique for each connection (so it should include the target host, port and user).

See also Using an already established SSH channel and SSH easily copy file to local system

share|improve this answer
add comment

I think your approach could be improved.

  1. I would set up your machines with public/private keys so that you can ssh/scp without having to login with a password.
  2. Write a script for the remote server to do the following : a) Copy file (if exists) to backup file, copy from your php server the file required, restart the service and return a status
  3. Then when your php runs it should run a script which simply starts the remote collection and returns the result to your PHP script.

So your remote pulls the file and restarts the service, your php triggers the remote script to run.

share|improve this answer
    
Prompting the user for a password is a requirement I can't get around unfortunately. I should have specified this earlier. As for pulling the file from the remote end, there would still have to be at least two SSH connections made right? –  Koveras Dec 31 '13 at 16:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.