Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bash: listen on tcp port

2,110 views
Skip to first unread message

Chuck

unread,
Jul 9, 2008, 12:08:26 PM7/9/08
to
Is there a way to get a bash script to listen for connections on a tcp
port? I've tried "< /dev/tcp/localhost/1521" but that just throws a
"connection refused" error. Thanks

Dave B

unread,
Jul 9, 2008, 1:04:29 PM7/9/08
to
Chuck wrote:

That tries to connect to port 1521 on localhost. If there's no program
listening there, you get "connection refused".
I don't think what you want to do can be achieved with bash alone (if that's
not correct, hopefully more knowledgeable users will correct me). I think
that doing what you want requires some external utility, like eg netcat.

--
echo 0|sed 's909=oO#3u)o19;s0#0ooo)].O0;s()(0bu}=(;s#}#.1m"?0^2{#;
s)")9v2@3%"9$);so%op]t(p$e#!o;sz(z^+.z;su+ur!z"au;sxzxd?_{h)cx;:b;
s/\(\(.\).\)\(\(..\)*\)\(\(.\).\)\(\(..\)*#.*\6.*\2.*\)/\5\3\1\7/;
tb'|awk '{while((i+=2)<=length($1)-18)a=a substr($1,i,1);print a}'

Maxwell Lol

unread,
Jul 9, 2008, 10:32:05 PM7/9/08
to
Chuck <chuckh19...@gmail.com> writes:

You can use the netcat program.

nc -l -p portnumber | script

Dan Stromberg

unread,
Jul 10, 2008, 12:16:21 AM7/10/08
to

inetd or xinetd are relatively easy ways of getting a bash script
listening on a port. With them, you can make your bash script run once
for each connection - and you don't need to deal with any socket API's -
just the usual stdin/stdout work fine.

Or another way, which is similar to netcat but written in a scripting
language itself, is my pnetcat program:

http://stromberg.dnsalias.org/~strombrg/pnetcat.html

ISTR pnetcat being faster than netcat on gigabit networks at one time
(perhaps it's still true, dunno), because although pnetcat is in a VHLL,
it allows you to specify block sizes and TCP window sizes. In fact, I
wrote it when I was doing some (optically switched) gigE performance
testing - it was able to get over of 900 Megabits/second on a CPU that
wasn't that blazing with modest tuning.

Burkhard Ott

unread,
Jul 10, 2008, 3:10:17 AM7/10/08
to

vi /etc/inetd.conf

12345 stream tcp nowait root /usr/sbin/tcpd /bin/bash -i

restart inetd

But that might be dangerous because ther is an interactive root shell
listen on purt 12345.

cheers

mop2

unread,
Jul 10, 2008, 9:20:49 AM7/10/08
to

Chuck

unread,
Jul 10, 2008, 2:16:38 PM7/10/08
to

I don't think that's available on Solaris 10. Sorry for not including
the platform on my OP.

Also I do not have access to inetd.conf as I am not the administrator on
the box in question.

Kenny McCormack

unread,
Jul 10, 2008, 2:33:30 PM7/10/08
to
In article <amsdk.1190$Ae3.188@trnddc05>,

Chuck <chuckh19...@gmail.com> wrote:
>Maxwell Lol wrote:
>> Chuck <chuckh19...@gmail.com> writes:
>>
>>> Is there a way to get a bash script to listen for connections on a tcp
>>> port? I've tried "< /dev/tcp/localhost/1521" but that just throws a
>>> "connection refused" error. Thanks
>>
>> You can use the netcat program.
>>
>> nc -l -p portnumber | script
>>
>
>I don't think that's available on Solaris 10. Sorry for not including
>the platform on my OP.

Does the box have an internet connection?

Maxwell Lol

unread,
Jul 10, 2008, 4:12:41 PM7/10/08
to
Chuck <chuckh19...@gmail.com> writes:

> Maxwell Lol wrote:
>> Chuck <chuckh19...@gmail.com> writes:
>>
>>> Is there a way to get a bash script to listen for connections on a tcp
>>> port? I've tried "< /dev/tcp/localhost/1521" but that just throws a
>>> "connection refused" error. Thanks
>>
>> You can use the netcat program.
>>
>> nc -l -p portnumber | script
>>
>
> I don't think that's available on Solaris 10. Sorry for not including
> the platform on my OP.

Why is that a problem?

It's open source and should compile on Solaris.
Downlaod from
http://osdn.dl.sourceforge.net/sourceforge/netcat/netcat-0.7.1.tar.gz

AFAIK You don't need special priviledges to run it as long as it's not using
a reserved port.

Download it, unzip it. Type

./configure CC=/usr/local/bin/gcc (or whatever)
make (or gmake)

and you are set. Unless you don't have any compilers on your machine....

Maxwell Lol

unread,
Jul 10, 2008, 4:13:30 PM7/10/08
to
gaz...@xmission.xmission.com (Kenny McCormack) writes:

>>> You can use the netcat program.
>>>
>>> nc -l -p portnumber | script
>>>
>>
>>I don't think that's available on Solaris 10. Sorry for not including
>>the platform on my OP.
>
> Does the box have an internet connection?

Or a USB drive, CDROM, .....

Chuck

unread,
Jul 11, 2008, 1:51:05 PM7/11/08
to

No but I can pull stuff down to my workstation and upload it with scp.

Chuck

unread,
Jul 11, 2008, 1:52:58 PM7/11/08
to
Maxwell Lol wrote:

>
> Download it, unzip it. Type
>
> ./configure CC=/usr/local/bin/gcc (or whatever)
> make (or gmake)
>
> and you are set. Unless you don't have any compilers on your machine....
>

I believe so. It's an Oracle DB server and IIRC the Oracle installer
requires cc and make to link it's binaries with only the options you select.

Kenny McCormack

unread,
Jul 11, 2008, 2:30:49 PM7/11/08
to
In article <d4Ndk.17$jS4.14@trnddc07>,

Does that work with the dixie-cups-and-strings method of communication?

Or does it use the internet like most versions of scp do?

Chuck

unread,
Jul 11, 2008, 3:32:28 PM7/11/08
to

Any network program - scp or otherwise - can only use the internet if
the host is connected to it.

I think you are confusing tcp/ip with the internet. TCP/IP is a suite of
protocols that can be used on any network. The internet is a network.

mik3...@gmail.com

unread,
Jul 12, 2008, 3:53:09 AM7/12/08
to
On Jul 11, 2:16 am, Chuck <chuckh1958_nos...@gmail.com> wrote:
> Maxwell Lol wrote:

Solaris has Perl, how about doing some socket programming :
http://www.tutorialspoint.com/perl/perl_socket.htm

franzi

unread,
Jul 12, 2008, 2:57:53 PM7/12/08
to

Solaris got dtrace and with bash you can useit very well

Chuck

unread,
Jul 14, 2008, 1:56:19 PM7/14/08
to
franzi wrote:

> Solaris got dtrace and with bash you can useit very well

How would I use it to set up a listener on a port that just does
something trivial like pass back "hello" if someone tries to connect to it?

0 new messages