KSH scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH scripting
# 1  
Old 10-02-2017
KSH scripting

Hi Guys,

I am trying to learn to script.
first I have 2 server, A and B.
A with IP 192.168.82.22. B with IP 192.168.82.44.
Both with login user admin and password admin.
server A will generate a file every day with name gg.log under /app/gg/20171002.log. I wish to write a script to copy the file zip it and send to server B under the folder ITlog with one day before.

Can I know how should I write the script?
# 2  
Old 10-02-2017
hello leecopper,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (csh, ksh, bash, Perl even?)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
  • Do you have authentication set up to copy the file manually, or some sort of disk share that they both see?
  • Does this need to be scheduled, and if so, what tools do you have available other than cron?
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all. I'm sure we can get this sorted.



Kind regards,
Robin
# 3  
Old 10-02-2017
Hi Robin,
Please see my reply.
Is this homework/assignment? There are specific forums for these.
This is my assignment.
What have you tried so far?
So far i have search on google and try to get some info.
What output/errors do you get?
I have not try any yet.
What OS and version are you using?
Aix 7.1
What are your preferred tools? (csh, ksh, bash, Perl even?)
Ksh
What logical process have you considered? (to help steer us to follow what you are trying to achieve)
i am trying to copy a file generated by the app and ftp it to another workstation to analyse the data.
Do you have authentication set up to copy the file manually, or some sort of disk share that they both see?
not too sure.
Does this need to be scheduled, and if so, what tools do you have available other than cron?
crontab to be use.
Most importantly, What have you tried so far?
just getting some concept to run this on our development server.
# 4  
Old 10-02-2017
Okay, so if you were to do this manually, step by step, what would you do? Would it be something like:-
  • Change to the directory of the source file
  • Start FTP/SFTP to the target server
  • Provide login credentials where requested
  • Within FTP/SFTP, change to the target directory
  • Issue a put to transmit the file
  • Quit FTP/SFTP
It could also be done the other way round, i.e connect from where you want to get the file to back to the source and get the file.
Are either of these a suitable structure? If so:-
  • Do you have a fixed filename to send/receive?
  • Do you have credentials?
  • Will you be using FTP, SFTP or something else?
  • How far can you get with your code before you get stuck?


Kind regards,
Robin
# 5  
Old 10-03-2017
Hi Robin,

so far I have refer some of the existing similar script and do some modify and try to use the script at below,

Code:
#!/bin/bash
#assuming the file1 and file2 will be generate everyday and it was already in the directory.
# This script is to transfer the file1 and file2 to admin workstation.
 
 
HOST=192.168.82.22
USER=admin
PASS=admin
 
date
cd /apps/gg/IT
 
gzip data_`date '+%d%m%y_%H'`00.txt
gzip log_`date '+%d%m%y_%H'`00.txt
 
File1=$(date +"data.gz")
File2=$(date +"log.gz")
data="/app/gg/IT/data"
log="/app/gg/IT/log"
data_use=$(date +"data_%Y*")
log_use=$(date +"log_%Y*")
 
ftp -n -v $HOST <<EOF
user "USER" "$PASS"
prompt
ascii on
mkdir "$File1"
cd "$File1"
mkdir "$File2"
cd "$File2"
lcd $data
mput $data_use
lcd $log
mput $log_use
quit
EOF

I have yet try it work or not.

Regards
Leecopper

Last edited by MadeInGermany; 10-03-2017 at 04:36 AM.. Reason: added code tags, removed leading spaces
# 6  
Old 10-03-2017
Okay, that is a decent start. I would suggest moving the credentials out of the script. If you can read the script, you can see the credentials and therefore access data that maybe should be secure. Additionally, if you have several scripts with these details in and you need to change them (e.g. you decide that having the password the same as the account is not actually very secure) then you have to change it in everyone of your scripts else you will get failures.

Without a -n flag, FTP will look for a file called .netrc in the home directory of the person running it. The format is defined in the man netrc page. You don't need to include all the fields.

This does some directory creation before putting all the files that match the string given. I'm not sure if this is what you want. Perhaps this would be neater:-
/home/rbatte1/.netrc
Code:
machine 192.168.82.22 login admin password admin
machine serverb login admin password M0r3_53cure!

The second line uses the short DNS name, which is more descriptive so may be better long term.

/home/rbatte1/my_ftp_script_example
Code:
#!/bin/ksh

host=serverb         # .... or the IP address if you must <<sigh>>
my_run_date=`date +%Y%m%d`

echo "Changing to source directory /app/gg/${my_run_data}.log"
cd  /app/gg/${my_run_data}.log || (echo "Failed to change directory to source area" ; exit 1)

echo "Checking file gg.log exists"
[ -f gg.log ] || (echo "There is no file to compress/send" ; exit 2 )

echo "Compressing gg.log to gg.${my_run_data}.log.Z"
compress -c gg.log gg.${my_run_data}.log.Z || (echo "Failed to compress the gg.log file" ; exit 3)

echo "Sending file"
ftp "${host}" << EOFTP 
  cd ITlog
  put gg.${my_run_data}.log.Z
EOFTP

echo "File sent.  Removing the temporary compressed file"
rm gg.${my_run_data}.log.Z

echo "Finished"
exit 0

Your description was a little vague, so I've guessed at what you mean for the various directories.


Does this get you going?
Robin
# 7  
Old 10-03-2017
Hi Robin,
There is a changes on the scenario, lets say I have a development server A running AIX 7.1 with ip 192.168.82.22 and a windows workstation with ip 192.168.82.44 and the ftp connection is opened.
for server A under the /app/gg/it there will be file generated every hour and another fie generated daily,what i need to do is to find the files lets say from may 2017 till yesterday and gzip it then ftp it over to the workstation assuming the ftp is ok. how should my scripts looks like?
Here I created a script but don't know its was right or wrong as I do not have a chance to test on the real development environment.

Code:
 
  1. #!/bin/bash
  1. cd /app/gg/it
  1. find /app/gg/it -name "Data_to_check_*" -type f -mtime +0 -exec gzip {} \;
  1. find /app/gg/it -name "log_to_check_*" -type f -mtime +0 -exec gzip {} \;
  1. HOST=192.168.82.44
  1. USER=admin
  1. PASS=abc123
  1. DIR1=$(date +"%Y_Data")
  1. DIR2=$(date +'%Y_log")
  1. DADIR="/app/gg/it/"
  1. LODIR="/app/gg/it/"
  1. DAFILE=$(date + "Data_%Y%m%d*.gz")
  1. LOFILE=$(date + "log_%Y%m%d*.gz")
  1. echo "backup file to admin workstation"
  1. ftp -n -v $HOST <<EOF
  1. user "$USER" "$PASS"
  1. prompt
  1. ascii on
  1. mkdir "$DIR1"
  1. cd "$DIR1"
  1. mkdir "$DIR2"
  1. cd "$DIR2"
  1. lcd $DADIR
  1. mput $DAFILE
  1. lcd $LODIR
  1. mput $LOFILE
  1. quit
  1. EOF


Last edited by leecopper; 10-06-2017 at 05:56 AM.. Reason: Converted to formatted numbered list
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh scripting

Hi All, Can you please let me know what I missed in the below function? Whenever I try to execute the script i am getting syntax error at line 101 : `fi' unexpected Function is function DELNWE5O { export ORACLE_HOME=/ora00/app/oracle/product/11.2.0/grid_1 export... (9 Replies)
Discussion started by: pvmanikandan
9 Replies

2. Shell Programming and Scripting

help with ksh shell scripting

I want to run a script that checks the env to see if I'm in a test or prod environment. From the command line I enter echo $host and it returns host name and I can tell by the name if I'm in test or prod. When I run the command from a script I get "not found" What's wrong with the script? if ... (2 Replies)
Discussion started by: Bperl1967
2 Replies

3. Shell Programming and Scripting

help in ksh scripting in aix

Hello gurus I am looking for a script : We need to generate a file list created by user id on a AIX box. Criteria 1: other than userid : dwimpid & aiadmin Criteria 2: Files older than 2 months ( it can be any user id ). File Path to Look: /project and /project1 Thx silu (7 Replies)
Discussion started by: silu
7 Replies

4. Shell Programming and Scripting

Help needed in ksh scripting

I got a task to do today, I want to write a script for the following 1) Archive log more than 2 days old 2) Number of days the current Archive logs present 3) Total number of archive logs 4) Size of each archive log 5) When was last successful backup happened? Can I get a general script... (8 Replies)
Discussion started by: Rambo
8 Replies

5. Shell Programming and Scripting

Help with ksh scripting in AIX

I am looking for a script which does the following Script will run daily. 1.It will get snapshot off all filesystems including nfs mounts, automounts and clearcase mounts. 2.Then it will compare new snapshot with the snapshot created in the previous run. 3.If filesystem exists in... (20 Replies)
Discussion started by: saidiya
20 Replies

6. Shell Programming and Scripting

Problem with Ksh scripting

Hi, Below is the code for my script #!/bin/ksh for file in "file1.txt" "file2.txt" "file3.txt" "file4.txt" do L="$(tail -1 $file)" NUM=${L%%|*} DAT=${L##*|} echo $NUM echo $DAT done Now here,suppose for file1.txt L will have data like 56|06-07-2010 So, it should be (7 Replies)
Discussion started by: fidelis
7 Replies

7. Shell Programming and Scripting

ksh scripting help needed.

I am trying to create a script to manipulate numerous file and at first I thought it would be a simple task but at this point i am ready to break my computer! I am new to unix scripting so hopefully someone out there thinks this is super easy and can help me out! A sample of the problem file is... (2 Replies)
Discussion started by: theqcup
2 Replies

8. Shell Programming and Scripting

ksh scripting help

I have the file as below server1 no dr aix 5300-05-03 9119-595 20-18888 yes ftp-eagle server2 no dr aix 5300-05-03 9119-595 20-18888 yes ftp-eagle server3 yes dr aix 5300-05-03 9119-595 20-18888 yes ftp-eagle server4 ... (1 Reply)
Discussion started by: praveenbvarrier
1 Replies

9. Shell Programming and Scripting

Need help with KSH scripting

Hi I need to insert a page break into a file based on matching a phrase in the file. :confused: I am doing this with a ksh script on a Unix box. Any help would be greatly appreciated. Thanks (5 Replies)
Discussion started by: scrappycc
5 Replies

10. Shell Programming and Scripting

KSH Scripting

Will a shell script written in shell for HP/UX run on Solaris? (1 Reply)
Discussion started by: dstaller
1 Replies
Login or Register to Ask a Question