Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

ftp -n -v $HOST <<END_SCRIPT
quote USER $USER
put schema_1.sql
put schema_2.sql

(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

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

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

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


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.