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.

itortuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Justin MathewsCommented:
If you have downloaded and installed Putty (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html), In your Perl script you can say:

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

OR

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itortuAuthor Commented:
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?
itortuAuthor Commented:
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.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

itortuAuthor Commented:
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?


Justin MathewsCommented:
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
Justin MathewsCommented:
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.
itortuAuthor Commented:
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
itortuAuthor Commented:
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/`;
Justin MathewsCommented:
`"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.

itortuAuthor Commented:
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/`;
itortuAuthor Commented:
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'
Justin MathewsCommented:
Check if the server has SSH server running on it.
itortuAuthor Commented:
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.
itortuAuthor Commented:
the linux person also informed me that  we have just an ssh  client.
Justin MathewsCommented:
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.

 
itortuAuthor Commented:
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?
Justin MathewsCommented:
Yes, FileZilla uses FTP.
itortuAuthor Commented:
could you help me try this using just ftp I think i can live with the reduced security for now.

thank you much.
Justin MathewsCommented:
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>
itortuAuthor Commented:
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
itortuAuthor Commented:
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


Justin MathewsCommented:
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
itortuAuthor Commented:
the server name is spelled correctly, but to access it needs port 8081
Justin MathewsCommented:
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

itortuAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.