How to pass port no. to script? [Solved]

Using applications, configuring, problems
Post Reply
Message
Author
jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

How to pass port no. to script? [Solved]

#1 Post by jafadmin »

I wrote a script called "listenon" which calls nc and listens on a specified port and invokes another script called "listener" which sends a connect message to the client until killed. The problem is that I can't seem to pass the port number to the "listener" script.

I know I'm goofing something up. See below.

Code: Select all

#!/bin/sh
#
#  start nc and listen on port number from command line
#   uses the "listener" script to generate output.
#
PortNum=$1
printf "\n\tListening on port %d\n" "$PortNum"

#nc -l -p $PortNum -e listener						#This works
nc -l -p $PortNum -e "listener $PortNum"	#This doesn't

printf "\n\tClosing port %d\n\n" "$PortNum"

Thanks
Last edited by jafadmin on Sun 13 Apr 2014, 20:08, edited 1 time in total.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Re: Need more scripting help. This is driving me crazy

#2 Post by L18L »

jafadmin wrote:

Code: Select all

#nc -l -p $PortNum -e listener						#This works
nc -l -p $PortNum -e "listener $PortNum"	#This doesn't

Code: Select all

nc -l -p $PortNum -e listener $PortNum
What about this :?:

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#3 Post by Karl Godt »

Or this :

Code: Select all

nc -l -p "$PortNum" -e listener "$PortNum"

Note : "listener $PortNum" would get passed as first argument , thus makes the second missing .
This would create two unknown/error events : Unknown argument "listener 12345" and missing PortNum .

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#4 Post by jafadmin »

Huh. Both of those worked. I could swear I tried just exactly that though and it failed. I tried so many different ways it's hard to remember...

Thank you guys very much. Here's the listener:

Code: Select all

#!/bin/sh
#
#  A simple listener for use with netcat
#
# nc -l -p [PortNumber] -e listener $PortNumber
#
Counter1=1
printf "\n"
while :
do
    printf "\t\tYes, I am listening on port %d. [%d]\n" "$1" "$Counter1" 

    read -t 5 -s  -n 1 key
    
    Counter1=`expr $Counter1 + 1`

    if [[ $key = q ]]
    then
        break
    fi
done

printf "\n\t\tQuit was pressed. Exiting ...\n\n"


Post Reply