-1

The situation is simple:

I can ssh to a remote server with ssh my_remote_user@remote_server, and would like copy some files, i.e. /var/www/docs/web.config.txt to local.

The problem is that /var/www/docs/web.config.txt has ownership like userA:userA. I can access as this user via

$ su - userA $ Password: userApwd

with this in mind, I can:

1 - copy /var/www/docs/web.config.txt to /tmp folder and change ownership to my_remote_user 2 - copy from remote to local from remote /tmp dir

MY QUESTION:

Via bash, python or other language can I automatize this steps?

NB: with expect it seems I can su - userA and then insert the password in remote server, but subsequent commands like cp /var/www/docs/web.config.txt doesn't occurs...

Thanks

Sim Sca
  • 413
  • 7
  • 18
  • Also, you can look on this question: https://stackoverflow.com/questions/305035/how-to-use-ssh-to-run-a-shell-script-on-a-remote-machine?rq=1 – A.N. May 09 '17 at 17:04

4 Answers4

0

You will need to use SCP commands if you are on Linux server, following is sample command.

scp user@host:directory/SourceFile TargetFile scp -r user@host:directory/SourceFolder TargetFolder

Shahzab Asif
  • 481
  • 5
  • 13
  • I know `scp`, but my problem is that in remote machine I need to change user in order to copy the files. – Sim Sca May 09 '17 at 17:12
0

You can use su with a parameter -c to run arbitrary command, i.e.:

su -c "scp /var/www/docs/web.config.txt your_user@client_machine:" userA
A.N.
  • 278
  • 2
  • 13
  • Sorry, my explanation could be not clear... I need to copy files from remote server to local, and in remote server I need to change the user. I can't invert , that is running scp in remote in order to copy to local. Furthemore my problem is how automatize the user changing... – Sim Sca May 09 '17 at 17:16
  • Using `su -c` on a remote server and, then run on a remote server (under `su` command). – A.N. May 10 '17 at 15:17
0

Can you just use scp and userA credentials to login to the remote server, i.e. instead of:

scp my_remote_user@remote_server:/remote/dir local_dir

use:

scp userA@remote_server:/remote/dir local_dir
Andriy Berestovskyy
  • 8,059
  • 3
  • 17
  • 33
0

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:

  1. run scp to copy from remote to local
  2. 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!!!

Sim Sca
  • 413
  • 7
  • 18