Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

korn script and sendmail 3

Status
Not open for further replies.

tijerina

Vendor
Mar 4, 2003
132
US
How do I within a korn script, write an ftp process that will send a file to another server and on the sendmail server, how do I write a korn sccript to execute sendmail. I want the sendmail korn script to pick up the file that was recently ftp'd to it and then email out.

All help will be greatly appreciated.

I think for the ftp process, the beginning will be:

file_to_ftp=/dir/subdir/file_to_ftp

if [ -f $file_to_ftp ]
then
ftp target_server
and I am lost here with the username and password.
bin
put $file_to_ftp /dir/subdir/DESTINATION
exit
fi
exit

And for the sendmail script:

EMAILFILE=/Dir/Subdir/file_to_email

if [ -f $file_to_email ]
then
cat /Dir/Subdir/file_to_email | mail -s SUBJECT_OF_EMAIL email@address.com
fi

exit
 
For the ftp you can do the following. It is not very secure since the password is within the file, I would suggest expect for more error checking.

!#/bin/ksh
# my ftp file
ftp -n << EOF
user myuser mypassword
cd /some/directory
prompt off
ascii (or bin)
put myfile
-- or --
mput myfile*
EOF
echo &quot;Transfer Complete&quot;
------------------------------------


For the mail part, assuming it is ascii
#!/bin/ksh
# my email file
mailx -s &quot;my subject&quot; -r return@email.com addr1@email.com addr2@email.com < myemail.file



 
Hi,

Another ftp option you can use non-interactive which will use .netrc file in user home directory with permission only owner can rw. Example of .netrc :

machine ftp_server_ip login username password

then, for ftp itself:

ftp -i <<EOF
open ftp_server_ip
ascii
put filename
.
.
.
.
EOF

regards,
feroz
 
Another option would be to setup sendmail on the server itself. This would eliminate the need to ftp the file anywhere.

Of course, as this is an abstract, the FTP may be a valid requirement.
 
One more option is by using ssh (well scp) on the sendmail server to pull your file from the server, then use sendmail to mail it out, for non-interactive scp you will need to setup a null passphrase &quot;shared key&quot; between the two servers but this is more secure than ftp.

The basics of a script on the sendmail server could be as simple as:

(Note: this is only an example, syntax not tested, just to pass an idea)

#!/bin/ksh
scp me@myserver.com:/path/and/file_to_grab /tmp

if [ -e /tmp/file_to_grab ];
then
uuencode /tmp/file_to_grab attached_file_name | mail -s &quot;Your attached file is in this email&quot; someone@somewher.co.uk

else
mail -s &quot;Caution !! file was not avalible to send&quot; admin@yourdomain.com < path/to/your/log/file
fi

L.
 
tarn,
Do you have instructions for the setup of a null passphrase &quot;shared key&quot;? I have a script made with scp, but need to to run it non-interactively.
Thanks,
-Brent
 
I am trying all of the awesome ideas for this FTP script that I need and none seem to work.

kd7ict,

I do not have what you are mentioning. If you can help I will greatly appreciate this. Can you assist me with TARN's scp idea?


Thanks.

 
tijerina,
What part of tarn's scp script would you like help with?
-Brent
 
Ok its so simple that I still have to try it out first!
But its taken me an hour to set it up on my test box (gets me every time).

Be sure you have a local user account on each server (best with the same login name!!)
Then lets say that you have two servers A & B, you want to have non-interactive ssh/scp login from A to B.

On A in your home directory do:

ssh-keygen -t rsa <cr>
&quot;where <cr> is the enter key or carrage return&quot;

1) It will tell you &quot;Generating public/private key ....&quot;

2) Then prompt for a file name/location .. accept the deault.

3) Then it prompts for a passphrase just hit <cr> ....

4) It prompts for you to repeat passphrase again just give a <cr> ...

Thats the keys setup for your end ..
best test it locally first, cd into ~/.ssh/ and cat id_rsa.pub > authorized_keys2; chmod 600 authorized_keys2

now the test .. do: ssh -v youruser@your.box.com <cr>
you should see lots of std-out chatter (thats the -v &quot;verbose&quot; you can leave this off unless you are debugging) and you should be connected. If not there is probably an issue with either of your servers ssh_config or sshd_config files ( this is where it caught me out)..

If it has worked then ..

5) Now take a copy of the id_rsa.pub key from server A and place in the same users home/.ssh/ directory on server B now copy it into a file called authorized_keys2 in the same directory make sure it is chmod 600 (this is important for security and to allow it to work).

Now you should be able to: ssh youruser@serverB.com and not be prompted for a passphrase.

If you still have problems give me a shout and I'll document the /etc/ssh/ssh_config and /etc/ssh/sshd_config changes needed for me to get mine working.

Two more things:
1) if you want &quot;two-way&quot; non-interaction then repeate the same steps from server B !!Carefull not to overwrite your existing files when you move/cat the .pub files.

2) with two-way setup you can now take my original idea and run a script on server A that does two things,(1)-> builds the file that is destined for server B (to be mailed out) (2)-> calls ssh youruser@serverB run_getfile_and_mail.sh (= my first idea)

As you may have guessed with ssh you can tag a command onto the login command that will run inline (basically a remote call but reasonably secure)a command on the remote server.

One last thing b4 I get a cup-of-T IMPORTANT: never but never should the id_rsa &quot;private key&quot; be any where other than your machine guard it with your life !! mistakes can be made like coping it over into /tmp on the remote server, once someone has that &quot;The World's Their Lobster&quot;

Good luck Laurie.
 
You are the best Tarn!!!

You took the time to Gerber feed this to me, thank you so much.

Chris
 
Tarn,

Thank you very much for detailing the non-interactive ssh setup. I set it up for two-way, simply had to append the contents of id_rsa.pub to the authorized_keys2 file.

I am writing kornshell scripts to use scp between two HACMP servers which check for differences between the configuration scripts of Oracle and some cron jobs. I could have done it using ftp and a .netrc file, but this is much better.

-Brent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top