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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script/telnet - Remove/Control "Connection closed by foreign host"
# 1  
Old 02-27-2010
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:
Code:
#!/usr/local/bin/bash

count=$(ping -c 1 $1 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ "$count" = "0" ]; then
    echo "$1 PING [FAILED]"
	else
	echo "$1 PING [OK]"
  fi
  
apache=`echo " " | telnet $1 80 | grep Connected | awk '{ print $1 }'`
  if [ "$apache" = "Connected" ]; then
    echo "$1 PORT 80 [OK]"
	else
    echo "$1 PORT 80 [FAILED]"
  fi
ssh=`echo " " | telnet $1 22 | grep Connected | awk '{ print $1 }'`
  if [ "$ssh" = "Connected" ]; then
    echo "$1 PORT 22 [OK]"
	else
    echo "$1 PORT 22 [FAILED]"
  fi
smtp=`echo " " | telnet $1 25 | grep Connected | awk '{ print $1 }'`
  if [ "$smtp" = "Connected" ]; then
    echo "$1 PORT 25 [OK]"
	else
    echo "$1 PORT 25 [FAILED]"
  fi
pop=`echo " " | telnet $1 110 | grep Connected | awk '{ print $1 }'`
  if [ "$pop" = "Connected" ]; then
    echo "$1 PORT 110 [OK]"
	else
    echo "$1 PORT 110 [FAILED]"
  fi


Quote:
*0227|19:45%./simpleservertest.sh host.com
host.com PING [OK]
Connection closed by foreign host.
host.com PORT 80 [OK]
Connection closed by foreign host.
host.com PORT 22 [OK]
Connection closed by foreign host.
host.com PORT 25 [OK]
Connection closed by foreign host.
host.com PORT 110 [OK]
Code:
apache=`echo " " | telnet $1 80 | grep Connected | awk '{ print $1 }'`
echo " " | telnet $1 80 | grep Connected | awk '{ print $1 }'
++ echo ' '
++ telnet host.com 80
++ grep Connected
++ awk '{ print $1 }'
Connection closed by foreign host.
+ apache=Connected
  if [ "$apache" = "Connected" ]; then
    echo "$1 PORT 80 [OK]"
        else
    echo "$1 PORT 80 [FAILED]"
  fi
+ '[' Connected = Connected ']'
+ echo 'host.com PORT 80 [OK]'
host.com PORT 80 [OK]

But DeBug doesn't help me to understand why? Smilie
# 2  
Old 02-27-2010
I don't think that "ping" and "telnet" are suitable tools for port scanning .
You might allow such access on your own servers on your own LAN.
I would expect all such access to be blocked on a web server (with the possible exception of "ping").
# 3  
Old 02-28-2010
Hi, phpfreak:

Telnet is probably spitting that out to standard error, which isn't caught by command substitutions and in your code isn't redirected nor piped. Try redirecting telnet's stderr to /dev/null.

Code:
telnet $1 80 2>/dev/null

If not for this script then for the future, it would likely benefit you to learn more about netcat (aka nc).

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

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. Shell Programming and Scripting

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

4. UNIX for Dummies Questions & Answers

What is the significance of sh -s in ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh?

Please can you help me understand the significance of providing arguments under sh -s in > ssh -qtt ${user}@${host} "sh -s "${version}"" < test.sh (4 Replies)
Discussion started by: Sree10
4 Replies

5. 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

6. 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

7. Solaris

"Connection closed by" error

OS: Solaris 8 Sparc server1:userA:/home/userA# ssh userB@server1 Connection closed by < IP address> Problem: From being a userA, I am unable to connect/ssh to the same server as when trying to use a different account. (userB) Do you know where can i start checking? Here are some... (3 Replies)
Discussion started by: hrist
3 Replies

8. UNIX for Advanced & Expert Users

"Connection closed by remote host" while doing ssh to a solaris box

Hi All, When i try to do ssh from a linux to solaris box its throughing "Connection closed by remote host". Please not that this error is not occuring every time i do ssh, it occurs at random timing( Mostly ssh is successful) - unfortunately my script which is doing ssh is falling at this time... (4 Replies)
Discussion started by: pkumar7
4 Replies

9. 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

10. Cybersecurity

"421 service not available, remote server has closed connection."?

"421 service not available, remote server has closed connection." I tried to do ftp to one of the unix server . ftp hostname password It get connects when i do any command it throws error ftp> ls "421 service not available, remote server has closed connection." (1 Reply)
Discussion started by: vishaldsh
1 Replies
Login or Register to Ask a Question