LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2008, 02:25 PM   #1
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Rep: Reputation: 15
Expect script help adding newest dates


All,

I have an expect script that moves files from one server to another which works perfectly. But now i need to make a change where it goes into a directory where the date changes daily, exp: filedir.latestdate, and looks for a file whos name changes daily as well, exp: uploadfile.latestdate.

Anyone know how i can script in expect to beable to determine the latest directory and can get the latest version of a file?

Below is an example of my script so far:

#!/usr/bin/expect

# Expect script to sftp files to a remote server.

spawn sftp user@hostname
expect "password:"
send "password\n"
expect "sftp>"
send "cd /local/tmp/ \r"
expect "sftp>"
send "put test.txt \r"
expect "sftp>"
send "exit\r"
 
Old 10-07-2008, 02:32 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by investmentbnker75 View Post
All,

I have an expect script that moves files from one server to another which works perfectly. But now i need to make a change where it goes into a directory where the date changes daily, exp: filedir.latestdate, and looks for a file whos name changes daily as well, exp: uploadfile.latestdate.

Anyone know how i can script in expect to beable to determine the latest directory and can get the latest version of a file?
I'm not at my system right now, but I believe if you look at the man page for date, there are output format options. I'd either put it in YYYYMMDD format (easy human-readable, and will make sorting files easy too). That would be good if you only had one file per day.

If you had more than one file, you could do YYYYMMDDhhmm. Assigning the date variable in your script should be easy.
 
Old 10-07-2008, 05:25 PM   #3
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
Thanks, i dont think that will know to define what the latest date is though
 
Old 10-07-2008, 06:15 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
What are the filenames like ie what is the date pattern?
If you can define your own, use YYYYMMDD, as it sorts naturally

ls|sort|head -1

If you only have relevant files in the dir and they are not messed around with after being created, then you can use

ls -t|head -1
 
Old 10-08-2008, 08:11 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by chrism01 View Post
What are the filenames like ie what is the date pattern?
If you can define your own, use YYYYMMDD, as it sorts naturally

ls|sort|head -1

If you only have relevant files in the dir and they are not messed around with after being created, then you can use

ls -t|head -1
That was my thought exactly, Chrism01.
 
Old 10-08-2008, 12:03 PM   #6
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
So this has changed a little bit to make it easier, so i need to be able to just find the latest version of a file instead of both the directory and the file.

The file looks like this with ls:

-rw-rw-r-- 1 userid userid 1564 Oct 7 10:01 server.log.2008-10-07

So in expect how do i tell sftp to just get the latest server.log according to the date?

Thanks!
 
Old 10-08-2008, 07:13 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I don't think you can ask sftp (or any ftp) to run a remote script to find the latest file. Its not what ftp is for.
If the files are rpoduced regularly eg you expect 1 per day, then you can either:

1. keep the latest file locally (or a note somewhere) and calc the next name for sftp if you get irregularly
or
2. just calc from the current date if you sftp every day

I've used (1.) several times in the past, its more flexible & you can redo if the last one was corrupt.

HTH
 
Old 10-08-2008, 09:33 PM   #8
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
Right, keeing it locally is what i want to do but i still have to copy it from the remote server so it will be local. So how do you automate that, grabbing the newest file?
 
Old 10-09-2008, 04:06 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Need answers:

1. are the files produced on an expected schedule?
2. do the files a have predictable datestamp in the filename?


Different solns work depending on the answers to those qns. See my previous post.
 
Old 10-10-2008, 09:10 PM   #10
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
Look at my post on 10/08 for how the date is listed in the file.
 
Old 10-11-2008, 10:24 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
So like I said, if you know the date format in the filename you just calc the (expected) date of the newest file and try to sftp it. Just keep the latest file or a note so you know next time what to expect.
Start from your current file, if any.
If you haven't got any files yet, either manually get the first one, or just pick a date in the past (as far back as you need to go) and then keep looping until you run out of files or reach today's date, whichever comes first.

If you want to calc dates purely in bash, see here

http://cfaj.freeshell.org/shell/ssr/...-Game.shtml#s6

although you could prob manage with
http://groups.google.com/group/gnu.b...1c273d1f5cd2ce

Last edited by chrism01; 10-11-2008 at 10:31 PM.
 
Old 10-12-2008, 06:25 PM   #12
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
OK so ive got it almost figured out, in bash script you can do:

date=date=`date "+%Y-%m-%d"`
rsync -avz -e ssh userid@remoteserver:/tmp.$date /home/userid

i tried to set the date variable in Expect but no go. Anyone know how to make this work in expect? This is what i have below:

set date=`date "+%Y-%m-%d"` date
spawn rsync -avz -e ssh userid@remoteserver:/tmp.$date /home/userid

On the flipside, if no one knows this answer, anyone know how i can pass the password from a shell script, no i cant generate keys to make it passwordless. Its not an option. Thanks!
 
Old 10-12-2008, 07:14 PM   #13
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Sorry about the confusion

Last edited by jlinkels; 10-12-2008 at 09:10 PM. Reason: Nonsense what I wrote
 
Old 10-12-2008, 09:55 PM   #14
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
I dont know what to really make of that post jilinkels, i can find links myself but those links dont even have anything to do with what i asked. So... (scratches head)
 
Old 10-12-2008, 11:21 PM   #15
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
I didn't post to show those links, I edited and removed to content of the post. Look at the reason for editing.

I made some suggestions about how to tackle this problem using Expect. After that I did some testing and found out that both SSH and FTP send a whole lot of stray characters which makes it next to impossible for Expect to recognize a usable string.

If I had let it here it would have confused you even more.

jlinkels
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Expect Script coolest Programming 7 02-16-2009 03:59 PM
Script to change all the file's dates with condition ... ? raf_iso Programming 3 03-05-2008 09:30 AM
getting dates in a script farkus888 Solaris / OpenSolaris 6 02-22-2007 04:29 AM
Adding users with PHP (pass php variables to Expect script) Jayla Programming 1 10-20-2006 10:44 AM
dumping mysql db script with dates markehb Linux - Software 2 04-23-2006 10:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 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