Ubuntu – Scripting (piping) commands into a telnet session

command-line-interfacepipetelnetUbuntu

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

Best Answer

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

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

Related Topic