Visit Our UNIX and Linux User Community


Telnet Bash Script (Connection closed by foreign host.)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Telnet Bash Script (Connection closed by foreign host.)
# 1  
Old 08-02-2014
Telnet Bash Script (Connection closed by foreign host.)

Hello Everyone,

My following script is giving me problems, when the SIP trunk goes down and the telnet session is started and just when the command is about to complete the connection is closed then script restarts.

I have noticed that as soon the script types in "sys re" or "sys rebo" or "sys reb" the connection is closed.


Code:
#!/bin/bash
typeset -i interval=5
run=true
trunk=vonage
reboot=0
while [[ "$run" == "true" ]]; do
checktrunk=$(asterisk -rx "sip show peer $trunk" | grep -c "Status.*OK")
if [[ $checktrunk -eq 0 ]]; then
echo "$(date): SIP trunk registration failed."
sleep 2
{
sleep 2
echo admin
sleep 2
echo password
sleep 2
echo sys reboot
echo exit
} | telnet 192.168.1.1 
(( reboot +=1 ))
if (( reboot == 3 )); then
echo "$(date): Maximum 3 retries failed. Next retry after 10 mins."
sleep 600
reboot=0
fi
else
echo "$(date): SIP trunk registration OK."
fi
sleep 5
done
exit 1

Hope someone helps over this issue. Thanks!
# 2  
Old 08-02-2014
I remember having had a similar problem. This helped:
Code:
{echo open 192.168.1.1 
sleep 2
echo admin
sleep 2
echo password
sleep 2
echo sys reboot
echo exit } | telnet

# 3  
Old 08-02-2014
Quote:
Originally Posted by RudiC
I remember having had a similar problem. This helped:
Code:
{echo open 192.168.1.1 
sleep 2
echo admin
sleep 2
echo password
sleep 2
echo sys reboot
echo exit } | telnet

Hi RudiC, you again mate! Smilie

Lemme quickly try your given solution.

---------- Post updated at 11:24 AM ---------- Previous update was at 11:15 AM ----------

Smilie Smilie Smilie No luck Smilie

Code:
Sat Aug  2 17:22:52 GMT-1 2014: SIP trunk registration failed.
telnet> Trying 192.168.1.1...
Connected to 192.168.1.1 (192.168.1.1).
Escape character is '^]'.


Account:admin

Password: ***********

User login successful, expired time is "Unlimited".


Type ? for command help

World> show aConnection closed by foreign host.

# 4  
Old 08-02-2014
Try reducing the sleep periods to 1 sec, and add a sleep after the exit command.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 08-02-2014
Quote:
Originally Posted by RudiC
Try reducing the sleep periods to 1 sec, and add a sleep after the exit command.
Lovely, it works! Smilie

But I have no clue as to why would it work with sleep duration reduced.

Code:
echo open 192.168.1.1 
sleep 1
echo admin
sleep 1
echo pass
sleep 0
echo show adsl
echo exit
sleep 1


Last edited by Scott; 08-02-2014 at 02:01 PM.. Reason: ...
# 6  
Old 08-02-2014
Actually, I think it was the final sleep n that made it work, not the reduction from 2 to 1. The stdout of your brace expression { ... } was closed before all commands had executed on the router, making it drop the connection.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 08-02-2014
Quote:
Originally Posted by RudiC
Actually, I think it was the final sleep n that made it work, not the reduction from 2 to 1. The stdout of your brace expression { ... } was closed before all commands had executed on the router, making it drop the connection.

absolutely right, just tried it without the sleep in the end and it would not work. amazing!!

Thanks

Previous Thread | Next Thread
Test Your Knowledge in Computers #94
Difficulty: Easy
The mv command in Linux moves a file from one location to another, but only in the same directory.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Telnet in script Connection closed by foreign host

Hello, I'm trying to learn and automate some tasks via a script, but my first ever script failed with "connection closed by foreign host" error. I checked the other discussions but it didn't help. Could you please help? #!/bin/bash ( sleep 2 echo open x.x.x.x 23 sleep 2 echo user sleep 2... (1 Reply)
Discussion started by: Myrtle
1 Replies

2. Shell Programming and Scripting

Telnet error- Escape character is '^]' and connection closed by foreign host

In program, I want to telnet the ports present in telnet.txt file and capture screenshot of open port which is opened on new terminal. Problems: Escape character is '^]' and Connection closed by foreign host. Loop is not working properly. It only take one ports and closed the connection. ... (9 Replies)
Discussion started by: sk151993
9 Replies

3. Red Hat

Connection closed by host error in rhel 6 Linux

Hi All, I am new to Redhat Enterprise Linux 6 version. I am facing an issue with my lab server which is having RHEL6 lab1:root> uname -a Linux lab1 2.6.32-358.18.1.el6.x86_64 #1 SMP Fri Aug 2 17:04:38 EDT 2013 x86_64 x86_64 x86_64 GNU/Linux I am getting this error for every 18 minutes 30... (0 Replies)
Discussion started by: go2suresh1979
0 Replies

4. Solaris

"Connection closed by foreign host error" in Solaris 10

Version Details Solaris version : Oracle Solaris 10 8/11 s10x_u10wos_17b X86 $ uname -a SunOS tippy178 5.10 Generic_147441-01 i86pc i386 i86pcThe Issue +++++++++ I have two solaris 10 Virtual Machines created using Virtual Box. One of my applications running on Source was trying to connect... (9 Replies)
Discussion started by: kraljic
9 Replies

5. HP-UX

Connection closed by foreign host

I am trying to connect to my HP server from remote machine. It gets connected but once credential are provided the connection is closed. adroit:/home/seo/hitendra 32 ] telnet myserv1 Trying... Connected to myserv1. Escape character is '^]'. Local flow control on Telnet TERMINAL-SPEED... (4 Replies)
Discussion started by: hiten.r.chauhan
4 Replies

6. Shell Programming and Scripting

shell script/telnet - Remove/Control "Connection closed by foreign host"

How do I gain control of the "Connection closed by foreign host" message telnet yields when you connect to it in a shell script? I'm using the output: #!/usr/local/bin/bash count=$(ping -c 1 $1 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') if ; then echo "$1 PING "... (2 Replies)
Discussion started by: phpfreak
2 Replies

7. UNIX for Dummies Questions & Answers

ssh_exchange_identification: Connection closed by remote host Connection closed

Hi Everyone, Good day. Scenario: 2 unix servers -- A (SunOS) and B (AIX) I have an ftp script to sftp 30 files from A to B which happen almost instantaneously i.e 30 sftp's happen at the same time. Some of these sftp's fail with the following error: ssh_exchange_identification: Connection... (1 Reply)
Discussion started by: jeevan_fimare
1 Replies

8. Linux

ssh_ exchange-identification: Connection closed by remote host

Dear All, Recently our server has been giving the error: "ssh_ exchange-identification: Connection closed by remote host" The error causes the server to become in accessible via ssh and the services are stopped/hung. The server has to be restarted to make it working normal again. The... (3 Replies)
Discussion started by: vguleria
3 Replies

9. Shell Programming and Scripting

ssh_exchange_identification: Connection closed by remote host

hi i am trying to connect the frontend server using ssh i got the following error ssh_exchange_identification: Connection closed by remote host can anyone help please (1 Reply)
Discussion started by: Satyak
1 Replies

10. IP Networking

FTP - Connection Closed By Remote Host

Hi, I am having a problem with our AIX 4.3.3 Server accessing FTP. The error is " Connection Closed By Remote Host". Scenario: Since i put a default gateway on the server FTP connection is having a problem but when i remove the default gateway it will works fine.. Is there any way not... (1 Reply)
Discussion started by: mouglybean
1 Replies

Featured Tech Videos