Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

i use a cronjob to execute a filetransfer to a host and then telnet into the host and pipe some commands into it.

HOST=somehost
USER=postgres
PASSWD=blabla
ftp -n -v $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put schema_1.sql
put schema_2.sql
quit
END_SCRIPT

(sleep 20;
 echo "blabla2";
 sleep 5;
 echo "psql -d cb3db -f schema_1.sql";
 echo "psql -d cb3db -f schema_2.sql";
 echo "rm *.sql";
 echo "exit;"
 ) | telnet -l postgres somehost
echo "END OF REMOTE SCRIPT" ||:

it seems whatever I pipe/echo into the session gets executed parallel, it happens the exit is executed before the sql finished executing. I tried to put sleeps before the exit command, but thats rather a guessing game.

I know this approach is certainly not the most secure because of passwords,etc. So if there is a safer and securer way to do the job let me know.

Thanks for any hints

share|improve this question

3 Answers 3

"expect" is the usual way to script such interactions.

http://en.wikipedia.org/wiki/Expect

share|improve this answer

+1 for expect.

However, if you're like me, you'll find tcl (expect's native language) extremely painful.

If you dig down in to the wikipedia article, you'll find that there are perl and python (and certainly other) bindings for expect that let you do all the interactive things you need with much less code pain.

Best of luck.

share|improve this answer

Perl also has IPC::Run which I'm beginning to use after having used Expect (in Tcl and perl) quite a lot. My IPC::Run based code tends to be shorter and easier to read when I'm done.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.