Sorry, too much time is passed!
Thank for all reply, but at the end I've found the answer using expect but in different manner I had supposed...
To be clear, I have to run an expect script but only in my localhost, while I thought to run expect into remote server...
The following is my script, hoping helpful for someone:
1 Parametric Script
say remoteActions.sh
#!/usr/bin/expect --
set timeout -1
# ssh connection
spawn ssh $env(fsUser)@$env(fsHost)
expect "password:"
send "$env(fsPass)\r"
expect "sh"
# switch to apache user
send "su - apache\r"
expect "Password: "
send "$env(fsApachePass)\r"
expect -re ".*apache.*"
# create folder if not exists
send "\[ -d /tmp/$env(fsRemoteTmpDir) \] || mkdir -m 777 -p /tmp/$env(fsRemoteTmpDir)\r"
expect -re ".*apache.*"
# copy folder via rsync but exclude some dirs
send "rsync -a $env(fsExcludeFromRemoteDir) $env(fsRemoteDir) /tmp/$env(fsRemoteTmpDir)\r"
expect -re ".*apache.*"
# change permissions
send "chmod -R 777 /tmp/$env(fsRemoteTmpDir)\r"
expect -re ".*apache.*"
send "exit\r\n"
2 Real Script with configurations
#!/bin/bash
### File System
export fsUser='sshuser'
export fsHost='remoteip'
export fsPass='remotepass'
export fsApachePass='apache'
export fsRemoteDir='/var/www/webapp'
export fsExcludeFromRemoteDir='--exclude=img/ --exclude=upload/'
export fsRemoteTmpDir='/bak'
# copy remote folder into remote directory
./remoteActions.sh
3 My Trick
with this script I can access with my standard user to {fsRemoteTmpDir}, which now has 777 permissions, so I have still two actions to do:
- run
scp to copy from remote to local
- via
ssh command or with another expect script to delete the remote {fsRemoteTmpDir}
I don't post 1 and 2 because the heart of my question/answer is in remoteActions.sh.
Thank you guys!!!