Solved

how to automatically download files from unix to windows?

Posted on 2010-08-15
8
801 Views
Last Modified: 2012-08-13
we have 2 files A.txt and B.txt at /unix/folder,every week we have to load them to \windows\folder, and send them to users.

usually, I   ftp unixserver
                 cd  /unix/folder  
                 prompt
                 mget *.txt "\windows\folder\*.txt"

question :
1. how to code a bat file ,when  i want to download the A.txt and B.txt , just double click the bat file?
2. how to code a bat file to upload the a specific file to unix environment or download a specific file to windows? it means give the file name as parameter.

loginID: unixuser
password: unixpass


0
Comment
Question by:SayYou_SayMe
[X]
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
8 Comments
 
LVL 32

Expert Comment

by:DrDamnit
ID: 33441492
Please see the attached script. It may be a little more heavyweight than you need (this also compares md5 hashes), but we can remove that if you wish. I will post a second commend explaining how it works.
'Remote tools installation script

'Dim Vars
    dim WshShell
    Dim fso
    Dim hFile
    Dim WshEnv
    Dim args
    Dim rFileName 'Holds the remote file name.
    Dim ftpfile
    Dim stream
    Dim command
    Dim logstream
    Dim emailSubject
'Initialize Vars
    Set args = WScript.Arguments
    Set fso = CreateObject("Scripting.fileSystemObject")
	Set WshShell = WScript.CreateObject("Wscript.Shell")
	Set WshEnv = WshShell.Environment("SYSTEM")
	'Set ftpfile = fso.GetFile("ftpscript.txt")
	'Set stream = ftpfile.OpenAsTextStream(2) '1=Read, 2=Write, 8=Append
	Set stream = fso.CreateTextFile("ftpscript.txt",True)
	Set logstream = fso.CreateTextFile("log.txt",True)
'Create download ftp script.
	logstream.WriteLine("Backup download process beginning...")
	logstream.Write("Creating FTP Download script...")
	rFileName = "backup-" & formatmonth(Month(Now)) & "-" & formatday(Day(Now)) & "-" & Right(Year(Now),2) & ".tgz"
	stream.WriteLine("open www.yourdomain.com")
	stream.WriteLine("user")
	stream.WriteLine("pass")
	stream.WriteLine("cd backups")
	stream.WriteLine("BINARY")
	stream.WriteLine("GET " & rFileName)
	stream.WriteLine("GET md5.txt")
	stream.WriteLine("Quit")
	stream.Close
	logstream.WriteLine("[OK]")
	logstream.Write("Downloading today's backup...")
'Log in with FTP and download stuff
	WshShell.Run "C:\Windows\System32\ftp.exe -s:ftpscript.txt",,True
	logstream.WriteLine("[OK]")
'Run MD5 hash on downloaded file
	logstream.Write("Hashing downloaded file...")
	command = "%comspec% /c C:\Util\md5sum.exe " & rFileName & " > mymd5.txt"
	WshShell.Run command,0,True
	logstream.WriteLine("[OK]")
'Compare Hashes.
	logstream.Write("Comparing hashes...")
	Dim h1 'stream for hash1 file
	Dim h2 'stream for hash2 file
	Set h1 = fso.OpenTextFile("md5.txt")
	Set h2 = fso.OpenTextFile("mymd5.txt")

	'Get the original hash from hash1 file
	Dim buffer
	buffer = h1.ReadLine()
	Dim ary
	ary = Split(buffer," ")
	Dim hash1
	hash1 = ary(0)

	'Get the new hash from hash2 file
	buffer = h2.ReadLine()
	ary = Split(buffer," ")
	Dim hash2
	hash2 = ary(0)

	If hash1 = hash2 Then
		'they match!
		logstream.WriteLine("THEY MATCH")
		emailSubject = "DevServer SUCCESSFUL backup"
	Else
		'They don't match!
		logstream.WriteLine("MATCH FAILURE!")
		emailSubject = "BACKUP FAILED FOR DEVSERVER"
	End If

	logstream.WriteLine(hash1)
	logstream.WriteLine(hash2)

	'Move the files to the E drive.
	command = "%comspec% /c robocopy . F:\Backups\dev.yourdomain.com\ " & rFileName & " /move"
	WshShell.Run command,0,True

	logstream.WriteLine("Sending you this log file. Over and out.")
	logstream.Close

	command = "%comspec% /c C:\Util\blat.exe log.txt -to michael@yourdomain.com -subject """ & emailSubject & ""
	WshShell.Run command,0,True




Function formatday(strDay)
	If Int(strDay) < 10 Then
		formatday = "0" & strDay
	Else
		formatday = strDay
	End If
End Function
Function formatmonth(strMonth)
	If Int(strMonth) < 10 Then
		formatmonth = "0" & strMonth
	Else
		formatmonth = strMonth
	End If
End Function

Open in new window

0
 
LVL 32

Accepted Solution

by:
DrDamnit earned 250 total points
ID: 33441511
This vbs script creates an FTP script on the fly, then saves it then uses ftp to download a specific file (or list if you so desire), then downloads the md5 hashes, compares them, and emails you to let you know that it was done sucessfully or not.

Here's how it works.

1. I dim all the vars.
2. I create several scripting objects (filesystem object, shell object, etc...)
3. I create the download script. This just creates the commands, in order, for the FTP program to process. You can change, add, remove commands here to suit your needs).
4. It logs into the ftp server to download stuff.
5. It runs an MD5 hash to compare the downloaded one to the original. This is where you can start deleting lines if you so please. It is more than you're asking for.
6. Next it moves the downloaded files to a backup directory. For you, this could be your distribution directory.
7. Last, it uses blat to send a log file (that it has been creating all along) to tell me what went on.

The functions at the bottom need to stay there or you might break the script.

To use the email function, you need to have a directory called C:\Util where you store blat.exe (See: http://www.blat.net/)

If you want to do the MD5 deal, you need to get gnucore utils and put it in C:\Util (at this point you might as well add C:\Util to the path).

I have attached a zip with a script to download and install core utils for you. It requires wget (Download from here: http://sourceforge.net/projects/gnuwin32/files/wget/1.11.4-1/wget-1.11.4-1-bin.zip/download).

I am pretty sure you feel like you just drank from the firehose by now, but let me know if you're having any issues, and I'll walk you through it.
'Dim vars
        dim x,t,m,filename
        dim wsh
        dim fso
        dim d
        dim args
        dim oFolder
        dim oFile
'Create objects
        set fso = WScript.CreateObject("Scripting.fileSystemObject")
        Set t = WScript.CreateObject("WScript.Shell")
        Set args = WScript.Arguments
        set wsh = WScript.CreateObject("WScript.Shell")
'Make sure we're in util.
        t.CurrentDirectory="C:\Util\"
'Get the file
        wscript.echo "Downloading Core Utils ..."
        t.run "wget http://gnuwin32.sourceforge.net/downlinks/coreutils-bin-zip.php -O coreutils.zip",1,true
        t.run "unzip -o coreutils.zip",1,true

        wscript.echo "Downloading Dependencies..."
        t.run "wget http://gnuwin32.sourceforge.net/downlinks/coreutils-dep-zip.php -O coredeps.zip",1,true
        t.run "unzip -o coredeps.zip",1,true

        wscript.echo "Downloading Grep..."
        t.run "wget http://downloads.sourceforge.net/gnuwin32/grep-2.5.4-bin.zip -O grep.zip",1,true
        t.run "unzip -o grep.zip",1,true

        wscript.echo "Downloading Grep Dependencies..."
        t.run "wget http://downloads.sourceforge.net/gnuwin32/grep-2.5.4-dep.zip -O grepdeps.zip",1,true
        t.run "unzip -o grepdeps.zip",1,true

        'Fix the su.exe read only bullshit
        t.run "attrib -r .\bin\su.exe"

        wscript.echo "Installing binaries and dependencies..."
        set oFolder = fso.GetFolder(".\bin")
        for each oFile in oFolder.Files
                dim target
                target = t.CurrentDirectory & "\" &oFile.Name
                wscript.echo "Checking to see if " & target & " exists..."
                if fso.FileExists(target) then
                        'stuff
'                        wscript.echo "File Exists. Removing in favor of the new file."
'                        fso.DeleteFile target
                end if
                        wscript.echo "Installing: " & oFile.Name
                        fso.CopyFile oFile.Path,target,1
        next

        wscript.echo "Cleaning UP..."
        dim FolderNames
        FolderNames = "bin,contrib,man,manifest,share"
        dim DelFolders
        DelFolders = split(FolderNames,",")
        for each x in DelFolders
                wscript.echo "Removing folder: " & x
                fso.DeleteFolder(x),1
        next

        dim FileNames
        FileNames = "coreutils.zip,coredeps.zip,grep.zip,grepdeps.zip"
        dim killList
        killList = split(FileNames,",")
        for each x in killList
                wscript.echo "Removing downloaded file: " & x
                fso.DeleteFile(x),1
        next

        wscript.echo "Installation Complete."

Open in new window

0
 
LVL 32

Assisted Solution

by:DrDamnit
DrDamnit earned 250 total points
ID: 33441518
Oh... one last thing... once you get this working to solve the #1 part of your question, I'll post how to do the #2 part. It is SUPER easy once we get the #1 part setup.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:SayYou_SayMe
ID: 33441541
thanks DrDammnit,

but it is a little bit complicated for me,
is there any other easy way to handle this?

0
 
LVL 4

Assisted Solution

by:Frozenice
Frozenice earned 125 total points
ID: 33441547
you can try this..

create.. from notepad
autoftp.bat

with the following codes.
@ftp -i -s:"%~f0"&GOTO:EOF
open example.com
username
password
!:--- FTP commands below here ---
lcd c:\MyLocalDirectory
cd  public_html/MyRemoteDirectory
binary
mput "*.*"
disconnect
bye




and there you go.
0
 
LVL 11

Assisted Solution

by:Pieter Jordaan
Pieter Jordaan earned 125 total points
ID: 33441558
That is serious overkill

Create a file called ftpdownload.scr
and add the attached source code to it, changing the lines where needed.


Then call it using
ftp -s:ftpdownload.scr

more info: http://support.microsoft.com/kb/96269
ftp ftp.myserver.com
user
username
password
lcd c:\
bin
mget *

Open in new window

0
 

Author Comment

by:SayYou_SayMe
ID: 33441578
thanks guys

Frozenice-

is it correct? based on your solution
open example.com , what does it mean?

autoftp.bat
@ftp -i -s:"%~f0"&GOTO:EOF
open(what is it?)
unixuser
unixpass
lcd c:\windows\folder
cd /unix/folder
binary
mput "*.*"
disconnect
bye



I am really confuse  
0
 

Author Comment

by:SayYou_SayMe
ID: 33441891
thanks guys

it works now
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

617 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