Do you want to write an interactive script with some read commands or... ? What do you mean by input and output? For which tool/script is this input/output?
Please explain what you want to achieve, thanks.
You can it do the way you already did or check the server.xml of your Tomcat where the ports are defined in and grep it out of there. When having the port(s), you can just check additionally with
netstat -an| grep <port>
if it is up and running.
The way you have chosen, you do not know how many Tomcat installations/configurations are there, but just check which is up and try to get it's port.
It depends which way you want to go.
And zaxxon, what you told is correct only. I can get it from server.xml file.
But my problem is : we have to use this script in a number of servers. And the server.xml file may be manually edited. There will be http & https connections also. The part <Connector port="portNum" will edit manually. And there is a chance to change the alignment also.
[root@linux ~]# grep 8080 /tomcat6/apache-tomcat-6.0.18/conf/server.xml
Define a non-SSL HTTP/1.1 Connector on port 8080
<Connector port="8080" protocol="HTTP/1.1"
port="8080" protocol="HTTP/1.1"
So upto my knowledge , there is only one way , that to GET the PORT number from PROCESS id.
[root@linux ~]# ps -ef | grep tomcat
root 7976 1 0 Jul18 ? 00:22:11 /usr/java/jdk1.6.0_13/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/tomcat6/apache-tomcat-6.0.18/conf/logging.properties -verbose:gc -XX:+PrintGCDetails -XX:MaxPermSize=512M -Xms1024M -Xmx1024M -Djava.awt.headless=true -XX:+UseParallelOldGC -XX:+UseParallelGC -Djava.endorsed.dirs=/tomcat6/apache-tomcat-6.0.18/endorsed -classpath :/tomcat6/apache-tomcat-6.0.18/bin/bootstrap.jar -Dcatalina.base=/tomcat6/apache-tomcat-6.0.18 -Dcatalina.home=/tomcat6/apache-tomcat-6.0.18 -Djava.io.tmpdir=/tomcat6/apache-tomcat-6.0.18/temp org.apache.catalina.startup.Bootstrap start
root 28214 28183 0 10:02 pts/1 00:00:00 grep tomcat
[root@linux ~]# ps -ef | grep catalina | grep -v "grep catalina" | grep -v "catalina.out"|awk '{print $2}' | head -1
7976
[root@linux ~]# fuser 8080/tcp
8080/tcp: 7976 -->> I need vice-versa of this. That is , by giving process id , i should get port number. I tried by grep of fuser but i couldn't find.
Shamrock's idea is correct and good. The only difference is, that if you want an absolute list of ports of configured ports (not only those that are currently up and running), you will not get it by checking which processes are there, since it could be, that one of them is down for maintenance or similar.
For the problem of the format the server.xml could be in due to manual editing, I would try to run following via ssh from one box that has hopefully access to all others so you don't have to log in into each of them. For stuff like this I usually do something like the following (of course depending they are in a similar directory - else you have to think about how to find those server.xml in different locations):
$> cat server.list
server_1
server_2
server_3
$> while read S; do ssh -n -o "ConnectTimeout=3" -o "BatchMode yes" $S "cat ~tomcat/conf/server.xml" | sed -n 's/.*Connector port="\([^"]*\)" .*/\1/p'
On a box where 3 instances of Tomcat are running, I get something like:
8080
8443
8444
8445
8446
8447
8009
Which are up and running you could get the other way with ps and lsof.
The while loop will cycle through the lines of the input file, where on each line is the name or IP-address of a single host to be inspected. So 10 lines will be 10 ssh connects, one to each host.
If you deploy that script on each host, you'll have to ssh to each one though to get the information. There are different ways to achieve what you want - you could also run that script via cron on each box and have them connect to your central box delivering the info to it in intervals or once a day, whatever.
But to have passwordless communication with ssh between the hosts and your central collection box, you would have to create passwordless ssh-keys and exchange the public ssh keys to your needs.