Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2010-11-27
9
1,178 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

828 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