Thursday 23 February 2012

Solaris - Automated FTP job

I had a request come in to transfer a file from a Solaris box to a Windows VM. It needed to copy at 6am and only run on Tuesday to Saturday - being new to Solaris I thought I'd log this one for future reference......

Basic steps
1. create a user account on the receiving (Windows) box
2. create a user account on the sending (Solaris) box
3. create a .netrc file
4. create a batch file
5. edit the crontab file (Solaris account)
6. store passwords in a secure location

Step 1
Create the Windows user account and give it permissions to required area - in this case a FTP site was setup on a IIS box with the account having rights to the site.

Step 2
I created a user account, ftpjob, on the Solaris box and made sure the account had rights on the relevant area.
 ls -al (will show you permissions)
In this case the account created belonged to the group that had ownership along the full path.
It's home area was /export/home/ftpjob

Step 3
create the .netrc file in the root of the accounts home area (which allows for automating of the job).
 touch .netrc

Change the file permissions
 chmod 600 .netrc

I added the following text to the file:
 machine <name or ip> login <windows user account> password <windows user account> macdef init
 bin
 lcd /<solaris-local-file-path>
 cd /<non-root-path-on-windows-box>
 put <file-name>
 quit
<press enter twice then save file :wq!>

Step 4
Create a file, I'll call it ftpbatch with the following text in the home area
#!/usr/bin/ksh
 # Comments.....

 # links to the .netrc file in the ftpuser home area
 echo "`date` : Start script"
 ftp <IPAddress> <Port number if different from default>
 echo "`date` : Finish script"


Make sure you set the correct permissions on the file:
 chmod 755 ftpbatch
If you don't want anyone else to read the contents set the permissions as:
 chmod 700 ftpbatch

Step 5
To schedule the task crontab needs to used - by default I've found that a text editor is not specified in the user environment. I enabled this by editing the .profile file of ftpuser (in the accounts home directory).
To obtain the vi path type:
which vi
Output was:
/usr/bin/vi

Either make a temporary change so crontab uses vi or permenately add the entry in the .profile  export EDITOR=/usr/bin/vi

If you add the entry into the .profile then run: . .profilewhich will reload the environmental settings.

You can now edit the crontab settings by running: crontab -e

The request is for the job to run at 0600hrs every week (Tuesday - Saturday) and the crontab has edited as follows:
 # Comments 
00 6 * * 2-6 /export/home/ftpuser/ftpbatch >> /tmp/error.log 2>&1

The double >> will append an entry to the log file

Step 6
Store the passwords in a safe location

No comments:

Post a Comment