Solved

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

Posted on 2010-11-27
9
1,173 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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 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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now