Link to home
Start Free TrialLog in
Avatar of itortu
itortuFlag for United States of America

asked on

transfer file via sftp

If possible, I would like an example of how to transfer a file that resides on a windows box, location: c:\\data\myfile.xml to a linux server location: /opt/dev/destinationfolder/ via sftp.

If I need to add more information needed for this script, please let me know.

thank you very much.

ASKER CERTIFIED SOLUTION
Avatar of Justin Mathews
Justin Mathews

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of itortu

ASKER

i installed putty "A Windows installer for everything except PuTTYtel", but i do not know how to configure it.

i added the hostname and the port, but will i have to always do the log in? or when running it automatically, it will use the credentials passed on the string?
Avatar of itortu

ASKER

jmatix,

what else do i need to do on putty so it is configured and running always?

is there a chance that you can show me where in the script i add the line?

thank you.
Avatar of itortu

ASKER

for these strings:

`pscp -pw <password> c:\\data\\myfile.xml user\@server:/opt/dev/destinationfolder/`;

system('pscp -pw <password> c:/data/myfile.xml user@server:/opt/dev/destinationfolder/');

do i have to replace the word server with the actual server name? how do i include the port, is it necessary to include the port?


Avatar of Justin Mathews
Justin Mathews

To prevent hardcoding your password in the script you can use public/private keys authentication using Pagent. You can generate those keys using PuTTYgen. Remember to encrypt the private key with a password. Copy the public key into a file named known_hosts under .ssh directory in your home directory on the Linux server. Load the private key into Pagent. Make Pagent to run on Windows startup.

Create and configure a connection to the server in Putty and name it say 'myconn'. Make sure you select 'Agent Forwarding' for authentication. Then add this line after you create the XML file:

$root->printToFile ("C:\\Temp\\data.xml");
`C:\\<Path to Putty>\\pscp c:\\data\\myfile.xml myconn:/opt/dev/destinationfolder/`;

For more info on configuring Putty see the documentation at:

http://www.chiark.greenend.org.uk/~sgtatham/putty/faq.html
http://www.chiark.greenend.org.uk/~sgtatham/putty/docs.html
In the first method replace 'server' with your actual server name or ip address. No need to give port if default - ie. 22 for pscp. The second method I gave above is more preferable.
Avatar of itortu

ASKER

before i get into the use of public/ private keys, let me try it with a user and password specified just to test.

please let me know if this is how the string should look:

`C:\\<Program Files\PuTTY\putty.exe>\\pscp -pw <password> c:\\data.xml user\@myconn:/opt/dev/destinationfolder/`;


i did create the connection in the putty configuration.

where in the configuration interface I make Pagent to run on Windows start up?

I wait for you to confirm whether the string looks correct or not.

Thank you.


10-20-2010-10-56-16-AM.png
Avatar of itortu

ASKER

when i run the script it tell me unrecognized character \P passed

line 67:

`C:\\<Program Files\PuTTY\putty.exe>\\pscp -pw <password> c:\\data.xml user\@myconn:/opt/dev/apache-2.2.11/htdocs/wtxml/`;
`"C:\\Program Files\\PuTTY\\pscp.exe" -pw <password> c:\\data.xml <user>\@myconn:/opt/dev/destinationfolder/`;

Replace <password> with your password and <user> with your user id.

In the Connection > SSH > Auth tab make sure you checked 'Attempt authentication using Pageant'. Also check 'Allow agent forwarding'

To start Pageant on Windows startup, create a shortcut to Pageant in your Windows > Start menu > Startup folder.

Avatar of itortu

ASKER

it is not copying the file. i even tried to move it to a directory of my local apache installation.

`"C:\\Program Files\\PuTTY\\pscp.exe" c:\\data.xml \@localhost:81/wtxml/`;
Avatar of itortu

ASKER

i also added a new connection on putty to my localhost on port 81 so:

`"C:\\Program Files\\PuTTY\\pscp.exe" c:\\data.xml \@localhost:81/wtxml/`;

would look like:

`"C:\\Program Files\\PuTTY\\pscp.exe" c:\\data.xml \@local:/wtxml/`;

and made sure that:

In the Connection > SSH > Auth tab make sure you checked 'Attempt  authentication using Pageant'. Also check 'Allow agent forwarding'
Check if the server has SSH server running on it.
Avatar of itortu

ASKER

none all the processes for my user on that linux server have ssh in their names.

root has ssh, but i do not lob on to the server as root.

what would be the next step?

thank you.
Avatar of itortu

ASKER

the linux person also informed me that  we have just an ssh  client.
You cannot use ssh/scp to copy file to a server if the server does not have ssh server running. You should get the ssh server running on the server. If FTP server is running on the server and you can live with the reduced security of FTP then you can also use FTP to transfer the file.

Another option is to map the Linux server directory as a drive on your Windows PC using something like Samba.

 
Avatar of itortu

ASKER

there are some things that i do not understand very clearly. one is that why if the server does not have ssh, then, the protocol used to connect via filezilla is ftp?
Yes, FileZilla uses FTP.
Avatar of itortu

ASKER

could you help me try this using just ftp I think i can live with the reduced security for now.

thank you much.
create a text file by name ftp.scr and insert these lines into it. (Replace <user id> and <password> appropriately):

user
<user id>
<password>
cd /opt/dev/destinationfolder
put C:\data.xml
quit

Then at windows command line (replace <server name or ip>:

ftp -n -s:ftp.scr <server name or ip>
Avatar of itortu

ASKER

i followed the steps on previous comment.

user
<testuser>
<coyut>
cd /opt/dev/wtxml/
put C:\wtxml\data.xml
quit


then on command window:



10-20-2010-1-04-01-PM.png
Avatar of itortu

ASKER

can the file be transfer via http?

the windows server where the file is located would be

http://webtrend1:7099/xmlsource/

to destination

http://contrib-dev.energy.com:8081/wtxml


FTP failed because of these facts:

1. It could not connect to contrib-dev.alliant-energy.com. Check if the server name is spelled correct.

2. userid and password should be given without angle brackets (<>)

user
testuser
coyut
cd /opt/dev/wtxml/
put C:\wtxml\data.xml
quit
Avatar of itortu

ASKER

the server name is spelled correctly, but to access it needs port 8081
FTP server usually listens on port 21. Make sure FTP server is running on contrib-dev.alliant-energy.com.

For http transfer: Do you have WebDAV on the server? You can check by adding it as a network place in your windows explorer.

Right click on 'My Network Places' in explorer and select 'Open'
Click 'Add a network place'
Next
Choose another network location
Enter http://contrib-dev.energy.com:8081
Enter username and password

Avatar of itortu

ASKER

So far I was able to copy the file by creating a shell script:

#!/bin/sh

URL=http://webtrend1/wtxml/data.xml
OUT=/opt/contrib-dev/apache/htdocs/wtxml/data.xml

http_proxy=http://proxy.com
WGET="wget --no-cache --quiet"

$WGET -O $OUT $URL

if [ $? -ne 0 ]; then
  echo 1>&2 Unable to retrieve file $URL
  exit $?
fi