closing a telnet session on error, in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting closing a telnet session on error, in a shell script
# 1  
Old 01-23-2009
closing a telnet session on error, in a shell script

I am using a shell script to telnet into a remote unix server and execute certain commands. But if any command being executed remotely, throws an error, the script just hangs. And the telnet session does not get closed.

I want to be able to close the session and complete the script execution in case of an error or otherwise.

Below is the script. Any help will be appreciated.
#!/bin/sh
( echo open host
sleep 1
echo user
sleep 5
echo pswd
sleep 4
sourceDir=$1 ; filename=$3 ; destDir=$2
echo "cd ${sourceDir}"
sleep 1
echo "cp -rf ${filename} ${destDir} && \
cd ${destDir} && \
$cmd ; exit"
) | telnet


I also want to be sure the previous command got executed before proceeding to execute the next command, which is why I'm using '&&'. This, not by using "sleep", but some other way of ensuring the earlier command did get executed successfully.

The above script cribs at the echo of the last command. Snippet of the error is

uctvd201:/opt/epds/EPDSDV01/epdata/SPONSOR/0000_071/test [EPDSDV01]# d && \ <
sh: : Execute permission denied.
uctvd201:/opt/epds/EPDSDV01/epdata/SPONSOR/0000_071/test [EPDSDV01]# ad && \ <
sh: : not found.
# 2  
Old 01-23-2009
Not sure how much help I am since scripting is not one of my strong points, but I do notice some things.

I don't personally use telnet...it's a security hole unless you only have it open on a private subnet...even then...ssh is better.

I notice your script uses the sleep command in places where it should be waiting for user input. There is different shell scripting command that waits for user input instead of the sleep command...I believe it's the "read" command.

Just from the error snippets you posted, whatever user you're logging in as doesn't have the proper permissions to do what you want. Unless you're logging in as root (not good through telnet)...you need to use sudo.

If you want to make sure the previous command was successful...you're going to need to use if - else statements.

I wish I was better at scripting because I know exactly how to fix your issue I just don't know the proper syntax. Hopefully I've given you enough clues that you can get it figured out on your own. You seem to have good enough knowledge to do it.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to break out of hung or waiting telnet session in shell script?

I have to put together telnet instructions for 100s of hosts for verifying basic connectivity and get output in a neat format. Problem- If a telnet is hung with message "Trying .... <hostname" due to firewall or routing issue the commands waits for a very long time before it times out and my... (2 Replies)
Discussion started by: desiphantom
2 Replies

2. Shell Programming and Scripting

Print a closing XML tag shell script

I have a shell script that does everything I need it to do. But, when I was testing it I realized it doesn't print the closing XML tag.... Does anyone know how to incorporate printing the XML tag with my script? I am using AWK any help would be appreciated. (4 Replies)
Discussion started by: risarose87
4 Replies

3. Shell Programming and Scripting

Help with Shell Script opening and closing a program

REALLY new to this stuff, sorry. So I want a shell script to open a program, wait 45 minutes, close it, and then do it all again. I want to do this because I am running an iMacros Script for a long period of time and if Firefox is not constantly restarted, memory leaks start to happen. Anyway... (6 Replies)
Discussion started by: plsbbg
6 Replies

4. UNIX for Dummies Questions & Answers

PAM closing SSH session

Hello guys, You are my last hope. I googled for hours and tried so many things but I can't find a way to fix my problem. So there we go: I open a SSH tunnel to my linux vps. My client sends every 5-minutes a keepalive package and I also set the "ClientAliveInterval" value to 300 in the... (8 Replies)
Discussion started by: iamanewb
8 Replies

5. Emergency UNIX and Linux Support

Nohup process getting killed after closing PUTTY session

I am running a process in nohup . nohup getkeys.ksh 132 > 132.out & When I close the putty terminal,The process is getting killed . default_signal_handler called for signal no: 1 Is there a way to keep the process running even If I close the terminal (2 Replies)
Discussion started by: prasperl
2 Replies

6. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: drbiloukos
7 Replies

7. Shell Programming and Scripting

Expect script to automate telnet session

Hi all, I am currently running a daemon which creates a virtual terminal for testing purposes. Essentially, if I were to interact with it manually, this is what I get. john@test1:~$telnet localhost 7777 Trying ::1... Connected to localhost. Escape character is '^]' mip6d> pl eth2... (6 Replies)
Discussion started by: abxccd
6 Replies

8. HP-UX

ssh session getting hung (smilar to hpux telnet session is getting hung after about 15 minutes)

Our network administrators implemented some sort of check to kill idle sessions and now burden is on us to run some sort of keep alive. Client based keep alive doesn't do a very good job. I have same issue with ssh. Does solution 2 provided above apply for ssh sessions also? (1 Reply)
Discussion started by: yoda9691
1 Replies

9. Solaris

running script after closing telnet

Hi Guys, I have a question, is there a way or an option where i can run a script continuesly after i close the telnet window?? because im running a cobol program which runs several hours and im thinking that ill run it after office hours so the run will be finish the next day.. the thing is i need... (2 Replies)
Discussion started by: shinjeck
2 Replies
Login or Register to Ask a Question