LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-09-2008, 01:01 PM   #1
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Rep: Reputation: 39
transfer file using expect and sftp


Hi,

I was willing to use sftp for tranferring files from one server to another remotely using crontab. For that I am using expect instead of creating certificates for ssh. Now problem is with expect when I get the files to my local machine then I have to run some commands there on that files like moving it/renaming it. Now I cant use those shell commands on that expect prompt, not sure though. So have to use another script for those. Is there any way possible to put those commands in same script so that all happens at the same time.

first script to fetch files from remote server:
PHP Code:
#!/use/bin/expect
spawn sftp user@xx.xx.xx.xxx
expect 
"password:"
send "password\n"
expect "sftp>"
send "lcd /var/scripts \r"
expect "sftp>"
send "mget test.txt \r"
expect "sftp>"
send "quit \r" 
Another script to change & move that file:
PHP Code:
#!/bin/sh
cp /var/scrips /usr/local/apache
chown daemon
.daemon /usr/local/apache 
 
Old 03-09-2008, 01:21 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
wrapping expedct around this is a pretty horrible point to end up at. i'd suggest using sshfs to just mount it as if local over ssh and then just copy however you see fit.

but rsa key authentication is so so so much simpler than learning tcl for expect. what's your motivation for doing this? i'm totally lost.

As to what you're having problems with i'm really unclear, can you define a use case to show how you get stuck?
 
Old 03-10-2008, 02:05 AM   #3
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Original Poster
Rep: Reputation: 39
Hi,
As these are all local servers thats why I didn't thought of ssh way. Simply run expect to automatically provide password at periodic times using crontab to get files from remote server. Problem is, I was using two scripts; one for sftp to get the files, second one for copy and change ownership of those files which I want to be combined, which can done
as below

PHP Code:
#!/usr/bin/expect
spawn sftp user@xx.xx.xx.xxx
expect 
"password:"
send "password\n"
expect "sftp>"
send "lcd /var \r"
expect "sftp>"
send "get backup.tar.gz \r"
expect "sftp>"
send -- "!\r\n"
expect -exact "^\[root@localhost ~\]#"
send -- "mv /var/backup.tar.gz /var/backup2.tar.gz \r"
expect -exact "^\[root@localhost ~\]#"
send -- "exit\r"
expect "sftp>"
send "exit\r" 
Thanks.
 
Old 03-10-2008, 03:14 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
But you can achieve the same result password-less with an ssh-key-pair (as
Chris pointed out).
Code:
scp user@xx.xx.xx.xxx:/var/backup.tar.gz /var
ssh user@xx.xx.xx.xxx "cd /var; mv var/backup.tar.gz /var/backup2.tar.gz"
Much clearer and cleaner as far as I'm concerned (I hope I did
interpret your script correctly).


Cheers,
Tink
 
Old 03-10-2008, 04:19 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
It's the use of the word "simply" that really concerns me. Expect is great, but this is not the place for it.
 
Old 03-10-2008, 06:38 AM   #6
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
I'm surprised no one has mentioned the security issues around exposing a password in a script.

Setting up an ssh key pair allows you to do what you want securely with just a few lines. I've done this when I'm running backup scripts. The backup script has to run as root, but I don't want root logins even with key pairs. So I have the script on one machine accessing another machine with a command embedded in ssh embedded in an su: `su - backup -c "ssh server2 \"mt status\""` for example. The user "backup" has key pairs set up, and it just works. Yours would seem to just require the simple `scp user@server1.x.x.x:/var/scripts/tests.txt /usr/local/apache` followed by the chown.

I've only used expect where I'm dealing with a menu driven system like a library catalog system (before they all went web), and there really wasn't an alternative there.

If you need guidance on setting up the key pair, see http://sial.org/howto/openssh/publickey-auth/
 
Old 04-20-2008, 08:18 AM   #7
tanveer
Member
 
Registered: Feb 2004
Location: e@rth
Distribution: RHEL-3/4/5,Gloria,opensolaris
Posts: 525

Original Poster
Rep: Reputation: 39
Thanks a lot you all.
 
Old 05-16-2008, 10:29 AM   #8
stangsandplows
LQ Newbie
 
Registered: May 2008
Posts: 1

Rep: Reputation: 0
Another approch

I have been fighting the same issue, I had my scripts working perfect with expect and interact, until I went to cron. My problem is that I have to mget files and then rename each one to a new location. This would be a pain in an expect script. So here is what I did, maybe it would work for you.

I enabled rexec and issued a remote command to execute the script. expect/interact works through rexec. You can even setup a cron job to execute the remote command.

Thought it might help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
autmate file transfer using expect newbie_adm Linux - Newbie 1 07-10-2007 02:37 PM
Can transfer only 2MB using sftp nIMBVS Debian 1 11-26-2005 03:26 PM
transfer freezes for 600MB zip file via sftp kpachopoulos Linux - Networking 1 11-24-2005 12:50 PM
sftp: how to transfer the whole directory? kpachopoulos Linux - General 1 11-06-2005 08:44 AM
Poor file transfer speed with sftp six6 Fedora 3 07-29-2004 12:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration