• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

How to login to remote server automatically?

I need to setup a crontab on my server to login to a remote server & ftp me a file routinely without any human intervention.

I cannot use rsh, rcp, or rlogin because I cannot edit /etc/hosts on the remote server. Besides, I only have a basic user account on the remote server.

I am wondering if there is a way to run a script program written in C or sh on my server to manually send strings to login on remote server, perform tasks, and ftp a file from the remote server to my server.

I have been playing around with sprintf, system(command), etc., but no luck because the remote server generates a very long banner messages for login process.  Please help!
0
hkimhkim
Asked:
hkimhkim
  • 3
  • 2
  • 2
  • +3
1 Solution
 
mpassCommented:
You don't say what flavor of unix you are using, but if it supports a .netrc file, try using that. Otherwise, below is a script you can use to do what you want.

#!/bin/csh -f

set u=username
set p=password

while 1
ftp -in systemname << eof_ftp
      us $u $p
      bin
      get filename
      quit
eof_ftp
exit

Change the following:
username > your username on that system
password > your password on that system
systemname > the name of the system you are logging into
filename > name of the file you want to ftp

Tell me if you have any problems with this script and I'll help you resolve it. Hope this helps.


0
 
mpassCommented:
When I cut and pasted the above, it didn't keep the script layout in an orderly manner. This is the same script as above, it just looks nicer now.

#!/bin/csh -f

set u=username
set p=password

while 1
ftp -in systemname << eof_ftp
     us $u $p
     bin
     get filename
     quit
eof_ftp
exit



0
 
hkimhkimAuthor Commented:
This script only FTPs a file.

I need to perform the following tasks on the remote server:

1. Login on to the remote server
2. Choose an option from menu
3. Setup oracle parameter (. oraenv)
4. run sqlplus and SQL statement
5. wait for output
6. close sqlplus
7. ftp the output file
8. email to acknowledge completion of job.
9. log off

All these tasks need to be completed while running this script trigerred by crontab.

client system is LINUX Redhat 6.0 and the remote system is DYNIX.

Please help.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ahoffmannCommented:
1. to do a login programatically, you need expect
2. see 1.
3. this should be in your ~/.cshrc or ~/.profile
4. see 1. or use rsh
5. done automatically by a script
6. see 1.
7. see 1. and mpass' comment
8. see 4.
9. exit

So we end up in 2 suggestions to perform all your tasks:
   a. get expect and install on your client, then write a expect script to do all you need
   b. try to setup rsh on the server,
       you simply need to modify your ~/.rhosts, then you may try rsh from the client
       if rsh is enabled on the server, and you try
      rsh server-l username_on_server pwd
       it should return your home directory

If rsh works you simply need a script to do it all.
Tell us if rsh works, then I can suggest a sqlplus script too.
0
 
hkimhkimAuthor Commented:
I am not quite following your comment, "see 1" & "see4."  Where do you want me to see? Where is expect script?

As I explained earlier, rsh doesn't work.  If rsh works, this isn't even an issue...

Thank you for your further help!
0
 
dgrimesCommented:
I think he is saying that for your option 1 --> his option 1 is to use expect. For your option 2 --> See his option 1 (which is to use expect).

ahoffman is correct. The easiest way to do this is through expect. rsh will work if the account initiating the login also exists on the remote system and has the required permissions. But there are quite a few thing you are wanting to do. Expect is the tool I would use since you have no way to modify the remote system.

You may need to create an sqlplus script to create the report you want from Oracle unless there is a program you know you can execute on the remote system.
0
 
n0thingCommented:
The sexiest way is still expect scripts. So here are
the steps.
1- Get expect & tcl/tk source at http://expect.nist.gov
2- Compile & install tcl/tk then expect.
3- Write an expect script to do the above tasks.

This could be subdivided into 2 separate tasks.
Dynix host: Setup a crontab to run the SQL statements etc. It's easier to run from the localhost
and it won't break your expect scripts if the result is
not normal.
Linux host: Write the scripts to get the results then
send you e-mail.

Make sure you see #1. :-) Of course we could all do
that for a huge consulting fee. :-)
 


0
 
ahoffmannCommented:
dgrimes, thanks for explaining :)
expect is Tcl only (no TK), I also recommend http://www.scriptics.com/ for Tcl(/Tk), it then may  point to http://expect.nist.gov too ;-)
0
 
dgrimesCommented:
Did I submit my comment as an answer? I thought I submitted it as a comment. I believe ahoffman is the one who suggested using expect. I only concurred with his answer.

ahoffman, if you want the points please let me know and I will post a question for you to answer. Otherwise I am quite happy to credit for your answers ... and if you have any others you would like me to take credit for ... just let me know :)

dgrimes
0
 
ahoffmannCommented:
I go for the answer, not just the points.
Thanks anyway for the gentle offer,  dgrimes.
0
 
kbraswellCommented:
This worked for me and was very helpful. I spend hours searching the web for such an example.  Thanks for your expert advice!  I actually needed it to send but was able to modify it to work perfectly.  Here is the send version.

#!/bin/csh -f

set u=user
set p=password

while 1

ftp -in sitename <<eof_ftp
us $u $p
send /directory/file.txt file.txt
quit
eof_ftp
exit
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now