Unable to close telnet session from script

Hello people, I am creating a script that will alert me in an ftp or telnet account on my system expires. FTP part is ok, but when I am entering the 3 script, it stucks. I can see that the script will not close the telnet seession. Can you please check and let me know what I am doing wrong? What if I send a CTRL-C in the script, will it work for the telnet sessin?

!/bin/sh
bill=bill@gmail.com
LOGS=/home/bill/scripts_logs
SCRIPTS=/home/bill/scripts

${SCRIPTS}/bill_ftp_check.sh > ${LOGS}/bill_ftp_check.log 2>&1
${SCRIPTS}/bill2_ftp_check.sh > ${LOGS}/bill2_ftp_check.log 2>&1
${SCRIPTS}/bill3_check.sh > ${LOGS}/bill3_check.log 2>&1
${SCRIPTS}/bill_check.sh > ${LOGS}/bill4.log 2>&1

grep failed ${LOGS}/bill_ftp_check.log
if [ $? -eq 0 ]
   then
   mailx -s "bill_ftp account expired" $bill < ${LOGS}/bill_ftp_check.log
fi

grep failed ${LOGS}/bill2_ftp_check.log
if [ $? -eq 0 ]
   then
   mailx -s "bill2_ftp account expired" $bill < ${LOGS}/bill2_ftp_check.log
fi

grep "Your password will expire" ${LOGS}/bill3_check.log
if [ $? -eq 0 ]
   then
   mailx -s "bill3 account is about to expire" $bill < ${LOGS}/bill3_check.log
fi

grep "Your password will expire" ${LOGS}/bill4_check.log
if [ $? -eq 0 ]
   then
   mailx -s "bill4 account is about to expire" $bill < ${LOGS}/bill4_check.log
fi

sleep 5

rm ${LOGS}/bill_ftp_check.log
rm ${LOGS}/bill2_ftp_check.log
rm ${LOGS}/bill2_check.log
rm ${LOGS}/bill4_check.log

exit

WHERE bill3_check.sh

#!/bin/sh

(echo open 10.0.0.10
sleep 2
echo bill3
sleep 2
echo billpass
sleep 2
echo close) | telnet

exit

Thank you in advance !

---------- Post updated at 04:57 PM ---------- Previous update was at 12:08 PM ----------

Anyone ?

:frowning:

Replace "close" with "exit" and it should work, given that no errors while occur.

After the open command, which is processed by telnet, you are connected to the remote computer. You send username and password which should bring you to a shell, which can be closed by the "exit" command.

1 Like

I tried exit close quit
Same result :frowning:

Can you post the complete output of what happens when you call your script? Something seems to go wrong during connect or login.

1 Like

See what's happenning if I ran telent script only:

grx666 gpd /home/bill3/scripts:./bill3_check.sh
telnet> Trying...
Connected to 10.0.0.1.
Escape character is '^]'.
Local flow control on
Telnet TERMINAL-SPEED option ON

HP-UX grx666 B.11.11 U 9000/800 (ta)

login: bil3
Password:
Last successful login for bil3: Tue Mar 1 11:17:57 EET-2EETDST 2011 on pts/ta
Last unsuccessful login for bil3: Mon Feb 14 14:47:21 EET-2EETDST 2011
Please wait...checking for disk quotas
(c)Copyright 1983-2000 Hewlett-Packard Co., All Rights Reserved.
(c)Copyright 1979, 1980, 1983, 1985-1993 The Regents of the Univ. of California
(c)Copyright 1980, 1984, 1986 Novell, Inc.
(c)Copyright 1986-1992 Sun Microsystems, Inc.
(c)Copyright 1985, 1986, 1988 Massachusetts Institute of Technology
(c)Copyright 1989-1993 The Open Software Foundation, Inc.
(c)Copyright 1986 Digital Equipment Corp.
(c)Copyright 1990 Motorola, Inc.
(c)Copyright 1990, 1991, 1992 Cornell University
(c)Copyright 1989-1991 The University of Maryland
(c)Copyright 1988 Carnegie Mellon University
(c)Copyright 1991-2000 Mentat Inc.
(c)Copyright 1996 Morning Star Technologies, Inc.
(c)Copyright 1996 Progressive Systems, Inc.
(c)Copyright 1991-2000 Isogon Corporation, All Rights Reserved.

                       RESTRICTED RIGHTS LEGEND

Use, duplication, or disclosure by the U.S. Government is subject to
restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
Technical Data and Computer Software clause in DFARS 252.227-7013.

                       Hewlett-Packard Company
                       3000 Hanover Street
                       Palo Alto, CA 94304 U.S.A.

Rights for non-DOD U.S. Government Departments and Agencies are as set
forth in FAR 52.227-19(c)(1,2).
HP-UX grx666 B.11.11 U 9000/800 3016405088 unlimited-user license SCM Prod
resize: unknown character, exiting.
$
(IT APPEARS SCRIPT HAS EXIT BUT I PRESS ENTER IT KEEPS CHANGING LINES)

(HERE I PRESSED CTRL-C)
telnet> Connection closed.
grx666 test /home/bill3/scripts:
(NOW IT HAS EXITED)

Ah, I think I found the source of the problem:

resize: unknown character, exiting.

Your login script runs the "resize" command. It finds out the terminal's geometry by sending an escape sequence to the terminal and parsing the response. Instead of the response it reads the "exit" command, which is not understood by "resize".

Can you comment out the resize command and try again?

1 Like

I ll do that as soon as I found what the resize does. (It's a prod system).

Thanks for your help ! :slight_smile:

---------- Post updated at 12:11 PM ---------- Previous update was at 11:52 AM ----------

Is there any way to to use this script and leave the resize in my .profile?

How can I send a CTRL-D inside the script ?

You can send a Ctrl-D with

printf "\4"

If you can't remove the resize command, you have to send some fake input to it. Try to start with

...
echo xxx
sleep 2
printf "\4" ) | telnet