Question

FTP to SCP shell script

Asked by: FranklinRaj22

Due to security reasons my company is planing to modify all scripts that uses FTP to SCP. I see some documents in the web about scp but not very helpful in my case. My problem is this. My username and pwd is the same in the souce and destinamtion hosts and /home/<user> is mounded on both. Having said that, how do i deal with this public and private key stuff b'coz in both the cases its the same files which are gonna get modified.  Existing code attached.

#!/bin/ksh
userid="user"
passwd='pwd'
ftp -in <<!
user $userid $passwd
open server1
cd /tmp
get scptest.txt
bye
!

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-11-11 at 04:19:20ID23894129
Tags

Unix script

Topics

Unix Systems Programming

,

Shell Scripting

Participating Experts
3
Points
250
Comments
33

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. PHP and SCP
    Hello. I am attempting to make a upload script using SCP. I have tried using the system() and exec() commands, however they do not work as they run the code on the server, which obviously fails. Here's the SCP syntax that I'm using: scp $path username@www.server.com:/home/us...
  2. SCP configuration
    When I log in remotely to my system using an scp client, I see a number of directories, including home, bin, sbin...etc. Security permissions are obviously enforced, so I cannot access files that I should not be able to. However, I would like to configure my system so that wh...
  3. ssh & scp
    Just basic questions... If I wanted to ssh into another machine from my linux box using bash shell, how would I do that (i.e. what would be the command)? Also, if I have a large .tgz file in my linux box and want to transfer it to another machine using scp, what would be th...
  4. scp syntax
    I need to copy a file from a linux server to a linux workstation. I can putty into each machine from my windows desktop. using the following command, I am then asked for a password and after entering the password scp ./file user@192.168.1.x:/home/user/file I get the follo...
  5. shell script for scp
    I'm trying to automate the way i copy files from one machine to the next. How can i create a shell script that will scp over the network say the scp command is scp /workplace/test.txt angelblaze@127.0.0.17:/workplace and the password is 12345

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: omarfaridPosted on 2008-11-11 at 04:27:35ID: 22929662

you need to set trust (no password) between the systems:

http://waelchatila.com/2005/06/06/1118124232757.html
http://www.cvrti.utah.edu/~dustman/no-more-pw-ssh/

OR

use expect:

http://www.experts-exchange.com/OS/Unix/Q_23459740.html

If you set trust between the systems, then run

scp username@server1/tmp/scptest.txt /path/on/your/server

for expect, please see link provided

 

by: woolmilkporcPosted on 2008-11-11 at 04:34:31ID: 22929708

There's no problem in using the keys  - Add your public key into your 'authorized_keys' and leave the keys where they are (all  files commonly in your~/.ssh directory.

And - take care to have correct permissions - 0600 for your private key and and the 'authorized keys' !

wmp

 

by: FranklinRaj22Posted on 2008-11-11 at 05:02:27ID: 22929885

woolmilkporc, i tried doing the following:
1. generated the keys for the same ID in both servers.
2. updated the same in 'authorized_keys'
3. chmod 600...
4. scp user@server1:/tmp/file .

It didnt promt for a password but the file is not transfered. Further, i think the .cshrc is called. Cud u give me an insight of what is happening.

 

by: woolmilkporcPosted on 2008-11-11 at 05:09:04ID: 22929913

Did you get an error message?

Try scp -v user@server1:/tmp/file .

to see a bit more.

I don't know whether .cshrc gets called, but I do think it wouldn't matter.

wmp

 

by: woolmilkporcPosted on 2008-11-11 at 05:14:20ID: 22929933


This is what generally happens for authentication (from ssh man page):


          The file $HOME/.ssh/authorized_keys lists the public keys
         that are permitted for logging in.  When the user logs in,
         the ssh program tells the server which key pair it would
         like to use for authentication.  The server checks if this
         key is permitted, and if so, sends the user (actually the
         ssh program running on behalf of the user) a challenge, a
         random number, encrypted by the user's public key.  The
         challenge can only be decrypted using the proper private
         key.  The user's client then decrypts the challenge using
         the private key, proving that he/she knows the private key
         but without disclosing it to the server.

 

by: FranklinRaj22Posted on 2008-11-11 at 05:27:13ID: 22930011

I tried scp-ing with -v . I do see some debug statements. No obvious errors. But not sure about the following lines shown below.

host1:/tmp>scp -v /tmp/scptest.txt user@server1:/home/user/anish/scptry
........
debug1: Entering interactive session.
debug1: Sending command: scp -v -t /home/user/anish/scptry
Welcome Ramesh... from cshrc This time is Tue Nov 11 08:14:44 EST 2008
host1:/tmp> debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
debug1: Transferred: stdin 0, stdout 0, stderr 0 bytes in 1.2 seconds
debug1: Bytes per second: stdin 0.0, stdout 0.0, stderr 0.0
debug1: Exit status 0

What is the Interactive session about in the middle? and 'Welcome Ramesh... from cshrc This time is Tue Nov 11 08:14:44 EST 2008' is from my .cshrc.

Long story short. Transfer still not working.

 

by: omarfaridPosted on 2008-11-11 at 05:31:15ID: 22930034

can you try sftp interactivity to see if you can login and get the file?

 

by: woolmilkporcPosted on 2008-11-11 at 05:46:56ID: 22930164

The 'interactive session ..' message is normal.

But there really seems to be a problem with your .cshrc.

I'am a bit astonished about the 'host1:/tmp> ' prompt suddenly appearing.

Could you please, just for testing, rename your .cshrc and try again?

If the copy then succeeds, please post your .cshrc. Let's look what strange things might be in there ...

wmp

 

by: FranklinRaj22Posted on 2008-11-11 at 06:02:27ID: 22930364

Renaming .cshrc and scp-ing did the trick. I got it transfered .Thanks a lot but give me a solutions to fix my .cshrc also. And why is my .cshrc called in between? My .cshrc is ....

echo "Welcome Ramesh... from cshrc This time is `date`"
source /home/algo/MRS_ENVIRONMENT/common.cshrc
set prompt="`hostname`:`pwd`> "
alias cd 'cd \!*;set prompt="`hostname`:`pwd`> "'

setenv ASPEED_ROOT /prod/alg/linux_vendor/aspeed
setenv ASPEED_WB_HOST xxxxxxxxx
setenv ASPEED_CONFIG /prod/alg/linux_vendor/aspeed/config/config.user.txt

 

by: omarfaridPosted on 2008-11-11 at 06:11:14ID: 22930450

what do you have in /home/algo/MRS_ENVIRONMENT/common.cshrc ?

 

by: FranklinRaj22Posted on 2008-11-11 at 06:26:08ID: 22930682

I do have one. I don think i will be able share the whole file. if you want me to check for a particular call or some thing specific, i can check and get back to you. But why is my .cshrc called?

 

by: omarfaridPosted on 2008-11-11 at 06:32:31ID: 22930771

when a csh is run (which is needed for login) it reads .login and .cshrc and execute commands found in them.

Can you mask info in the file and share rest of it?

 

by: woolmilkporcPosted on 2008-11-11 at 06:37:21ID: 22930821

I think .login is executed only in an interactive shell, in contrast to .cshrc. Could you place all commands which produce terminal output (echo ...) there instead of having them in .cshrc?

Sure wee need to know whether  /home/algo/MRS_ENVIRONMENT/common.cshrc

does produce terminal output, too.

 

by: FranklinRaj22Posted on 2008-11-11 at 07:06:06ID: 22931117

Yes you are right. It is because of the echos. I tried commending echos and transfer works fine for me. but i will not be able to do the same in production. i will have to come up with some creative mechanism to transfer with all the echos or bypass .cshrc call. Can you help me out with this ?

 

by: woolmilkporcPosted on 2008-11-11 at 07:08:13ID: 22931137

Did you already try to put the echos in a .login file and eliminate them in .cshrc?

 

by: FranklinRaj22Posted on 2008-11-11 at 07:14:02ID: 22931201

I didnt put the echos anywhere. i removeed it and the transfer worked. Now, the prod admin will have the same copy of .cshrc. I will not be able to ask em to change his .cshrc inorder to run my script.

 

by: omarfaridPosted on 2008-11-11 at 07:22:42ID: 22931312

did you try to use sftp ?

 

by: woolmilkporcPosted on 2008-11-11 at 07:27:22ID: 22931348

If you can't change anything at all in your environment, and if you must continue using csh (ksh works very much better for this case), and if your transfer must be ssh-based, I fear there will be no other solution as the one omarfarid is keeping to suggest - sftp

 

by: FranklinRaj22Posted on 2008-11-11 at 07:36:30ID: 22931432

When i try to sftp, i get the following message:
connecting to server1...
Received message too long 1466264675

But if i commend out the following lines(first two lines) in cshrc, i am able to sftp.
echo "Welcome Ramesh... from cshrc This time is `date`"
source /home/algo/MRS_ENVIRONMENT/common.cshrc



 

by: woolmilkporcPosted on 2008-11-11 at 07:41:07ID: 22931495

So, go and see your 'prod admin' and have a discussion with /her/him about .cshrc etc.

 

by: FranklinRaj22Posted on 2008-11-11 at 07:56:56ID: 22931666

ok. Let me have a discussion. But do inform me in case you can find a way out without altering .cshrc. Thanks.

 

by: omarfaridPosted on 2008-11-11 at 09:04:06ID: 22932361

I think I found a solution to this problem. Here, I do a test to see if .cshrc is working with terminal (which means an interactive / login session) or not.

tty | grep /dev > /dev/null
if ( $status == 0 ) then
echo hi
endif

I tested this with sftp and scp, and it is working fine.

here you just put your statements between the if and endif

 

by: omarfaridPosted on 2008-11-11 at 09:05:10ID: 22932372

Sorry, but I forgot to say put this in .cshrc file

 

by: woolmilkporcPosted on 2008-11-11 at 09:44:13ID: 22932719

I would have suggested a similar thing (e.g. testing $prompt), but since Franklinraj22 doesn't want to modify .cshrc ...

 

by: omarfaridPosted on 2008-11-11 at 09:48:45ID: 22932763

Hi woolmilkporc,

Thank you for the comment, the question in not whither we want to change or not, but it is what to change.

The test I am doing will not affect the application requirements, it is just a check to avoid the echo statements that is causing the problem.

Thanks,

 

by: FranklinRaj22Posted on 2008-11-11 at 22:05:54ID: 22937176

Thanks omarfarid. the solution you gave did work. But again as woolmilkporc said, it wud be better if i can find a way out without editing my .cshrc.
In the scenario i am in, if my solution incudes a change in my .cshrc then I will have to ask all the users to change their .cshrc files including the production admin's. I will end up seeking for too many permission and aprovals.

 

by: TintinPosted on 2008-11-11 at 22:49:25ID: 22937324

I'm note sure if it will work, but you can try

scp -n /tmp/scptest.txt user@server1:/home/user/anish/scptry

 

by: TintinPosted on 2008-11-11 at 23:04:42ID: 22937382

Sorry, ignore my last suggestion.

 

by: omarfaridPosted on 2008-11-12 at 03:43:05ID: 22938511

Are you going to talk to your application provider / developer to see if changes can be done in .cshrc? if yes, then you need to change all users' .cshrc files.

 

by: woolmilkporcPosted on 2008-11-12 at 04:53:03ID: 22938940

How many users really need to run scp?

In the question I read 'scripts that use FTP ' - normally such scripts are only run by a few 'utility' userids. Modify .cshrc of those users only, or better, move them to ksh.

wmp

 

by: FranklinRaj22Posted on 2008-11-12 at 07:37:20ID: 22940576

What I deal with is a overnight batch system. This batch runs hundreds of scripts and some of them uses ftp and this is to be changed to scp.
Now let me tell why i should change all .cshrc files. Each developer has a user ID and an environment similar to  production in the development server. This development environment can be resynced to any paricular date, make changes and full batch test can be conducted with the developer's userID. This full batch test results have to be send for approval for any task. Here is my need to change all .cshrc files of all the users. Else he/she will not be able to run the full batch test which happens for most of the tests.

 

by: woolmilkporcPosted on 2008-11-12 at 07:48:50ID: 22940712

OK,  so omarfarid's solution will remain the only one - change all .cshrc files.

 

by: omarfaridPosted on 2009-09-22 at 00:47:40ID: 25390388

Hi FranklinRaj22

If my solution remains the only one, how come it was not accepted as the solution or part of the solution !

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...