Solved

Simplify scp & unix2dos to a single command : Shell script Perl

Posted on 2010-11-27
9
1,177 Views
Last Modified: 2012-06-21
I currently need to do transfer files from Linux RHES 4.x box to a remote
Windows 2003 box (running OpenSsh server) & would like to simplify
the following multi-step commands to a single step (using pipe | & redirection
or possibly using Perl if possible).  I would like to avoid creating a temp
file & having to remove the file after that

file.txt is the local file in my Linux box which has every line/record
that ends with <LF> character only but at the remote Windows server
end, we need the file copied to the Windows to be terminated with
<LF> plus <CR>


Put without Append
================
cat file.txt | unix2dos > tmpfile; scp tmpfile remoteid@Wintelhost:/c/temp/file.txt; rm -f tmpfile

Put with Append
==============
scp remoteid@Wintellhost:/c/temp/file.txt tmpfile; dos2unix tmpfile; cat file.txt >> tmpfile; unix2dos tmpfile; scp tmpfile remoteid@Wintellhost:/c/temp/file.txt; rm -f tmpfile

0
Comment
Question by:sunhux
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 20

Accepted Solution

by:
edster9999 earned 167 total points
ID: 34222911
You can get rid of the temp file by piping the command directly to the other box.
SCP doesn't let you do this (as you have probably already worked out)
but you could use SSH to run a command on the other box and get that to write the file from the pipe.
I use this on Linux / unix machines.  I have never tested it in windows land but it theory it should work.

Something like  :

cat file.txt | unix2dos | ssh remoteid@Wintelhost 'cat > /c/temp/file.txt'




0
 
LVL 77

Assisted Solution

by:arnold
arnold earned 167 total points
ID: 34223154
since this is a script, what is the issue with the added steps that the script will perform?

If you have a cygwin environment on the windows side, you could establish an SSH tunnel and then pipe the filename, file contents through the pipe to an application on the remote side that will get the filename and pipe the data through while replacing the <lf> with <cr>.

0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 166 total points
ID: 34224136
When I see questions such as yours, I interpret as "how can I combine x number of steps/commands into 1 and still know that everything was successful without ever having to deal with error checking/handling?"

The answer is "You can't".

Do yourself a favor and write a full script.  Your first decision is 'do "I" want to use a shell script or a Perl script'?

If you use a Perl script, you there are at least 3 modules available that can properly transfer the file without first having to create a temp.
Net::FTP
Net::SFTP
Net::SSH2
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:sunhux
ID: 34224831
I can't implement cygwin environment as the Windows side belongs to a strict &
unyielding customer



The links below appear wonderful, do you have specific example using the Perl
tool below to implement Ascii & Append modes

Net::SFTP
Net::SSH2
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 34225609
Currently, I don't have any example scripts and I won't be able to do any testing until I get back to work in a couple days.

>> the Windows side belongs to a strict & unyielding customer

Is the customer going to allow you to install the required Perl modules?

What version of perl is on the target server and is it a 32bit or 64bit build?  Some of the modules may not be available for the 64bit platform.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 34225635
You can disregard my comment about needing to install the modules on the Windows server.  I forgot that you are going from Linux to Windows, not the other way around.
0
 
LVL 77

Expert Comment

by:arnold
ID: 34226398
IMHO, in order to combine the functionality of three individual steps that work into a single complex script seems like a strange and unnecessary.

When you scp files directly, what is the impact on the windows side?

Which ssh server are they running?
0
 

Author Comment

by:sunhux
ID: 34246007

Windows side is running Openssh server 3.8-1 :
some impacts of directly scp'ing directly : we may overwrite an existing file
at remote end : yes, good point that a multi-step script makes more sense
0
 

Author Closing Comment

by:sunhux
ID: 34343980
Thanks
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question