Get opened port with given PID?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get opened port with given PID?
# 8  
Old 07-21-2011
Sadly I have problems with my edit function so here an addition to make the code work. I forgot to end the line with:
Code:
....ctor port="\([^"]*\)" .*/\1/p' ; done < server.list

# 9  
Old 07-21-2011
thanks zaxxon for this idea..

$> cat server.list
192.168.1.10

but one error
Code:
[me@linux ~]$ while read S; do ssh -n -o "ConnectTimeout=3" -o "BatchMode yes" $S "cat /tomcat6/apache-tomcat-6.0.26/conf/server.xml" | sed -n 's/.*Connector port="\([^"]*\)" .*/\1/p'  ; done < server.list
Permission denied (publickey,gssapi-with-mic,password).

can i tell one another idea..

Quote:
cat `echo $CATALINA_HOME/logs/catalina.out` | grep -B10 "INFO: Server startup in"
gives the output as
INFO: Deploying web application directory docs
Jul 21, 2011 9:56:27 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
Jul 21, 2011 9:56:27 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jul 21, 2011 9:56:27 AM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jul 21, 2011 9:56:27 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/25 config=null
Jul 21, 2011 9:56:27 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 18838 ms
--
INFO: Deploying web application directory docs
Jul 21, 2011 12:12:16 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
Jul 21, 2011 12:12:16 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jul 21, 2011 12:12:16 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jul 21, 2011 12:12:16 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/31 config=null
Jul 21, 2011 12:12:16 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 17808 ms
--
INFO: Deploying web application directory docs
Jul 21, 2011 12:38:59 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
Jul 21, 2011 12:38:59 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jul 21, 2011 12:38:59 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jul 21, 2011 12:38:59 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/23 config=null
Jul 21, 2011 12:38:59 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 21409 ms

Code:
grep "`date +"%b %d"`"

will give the date started..

but i don't know how to code exactly this..

---------- Post updated at 01:44 PM ---------- Previous update was at 01:23 PM ----------

Quote:
Originally Posted by zaxxon
Sadly I have problems with my edit function so here an addition to make the code work. I forgot to end the line with:
Code:
....ctor port="\([^"]*\)" .*/\1/p' ; done < server.list

zaxxon ,

In this script i have to ssh each time the script is running. am i right ?

here actually one problem is there.

i am sorry that i did't give the complete spec.

The thing is:

1. i will create a shell script to get port number of tomcat , running or not , some other server info..etc and append the output to a file_details

2. I will put this script in each of our servers (say 10 servers)

3. And i will get the info from file_details whenever i need.

Last edited by linuxadmin; 07-21-2011 at 05:10 AM..
# 10  
Old 07-21-2011
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.
This User Gave Thanks to zaxxon For This Post:
# 11  
Old 07-21-2011
thanks zaxxon for your fast reply..

But what about this..


Quote:
Originally Posted by linuxadmin

Code:
cat `echo $CATALINA_HOME/logs/catalina.out` | grep -B10 "INFO: Server startup in"

gives the output as
INFO: Deploying web application directory docs
Jul 21, 2011 9:56:27 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
Jul 21, 2011 9:56:27 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jul 21, 2011 9:56:27 AM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jul 21, 2011 9:56:27 AM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/25 config=null
Jul 21, 2011 9:56:27 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 18838 ms
--
INFO: Deploying web application directory docs
Jul 21, 2011 12:12:16 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
Jul 21, 2011 12:12:16 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jul 21, 2011 12:12:16 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jul 21, 2011 12:12:16 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/31 config=null
Jul 21, 2011 12:12:16 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 17808 ms
--
INFO: Deploying web application directory docs
Jul 21, 2011 12:38:59 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
Jul 21, 2011 12:38:59 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jul 21, 2011 12:38:59 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jul 21, 2011 12:38:59 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/23 config=null
Jul 21, 2011 12:38:59 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 21409 ms

Code:
grep "`date +"%b %d"`"

will give the date started..

but i don't know how to code exactly this..
# 12  
Old 07-21-2011
Like this?
Code:
awk '/^INFO: Starting/ {sub(/\/1.1/,"",$4); a=$1 FS $2 FS $3 FS $4; split($NF,arr,"-"); b=arr[2]; getline; sub(/,$/,"",$2); print $1,$2,a,b}' infile
Jul 21 INFO: Starting Coyote HTTP 8080
Jul 21 INFO: Starting Coyote HTTP 8080
Jul 21 INFO: Starting Coyote HTTP 8080

# 13  
Old 07-21-2011
Or without a split, just deleting .*-:
Code:
awk '/^INFO: Starting/ {sub(/\/1.1/,"",$4); a=$1 FS $2 FS $3 FS $4; sub(/.*-/,"",$NF); b=$NF; getline; sub(/,$/,"",$2); print $1,$2,a,b}' infile

This User Gave Thanks to zaxxon For This Post:
# 14  
Old 07-21-2011
Thanks a lot.. for this Great work..

Thanks...


i made it like this..
Code:
cat `echo $CATALINA_HOME/logs/catalina.out` | grep -B10 "INFO: Server startup in" | awk '/^INFO: Starting/ {sub(/\/1.1/,"",$4); a=$1 FS $2 FS $3 FS $4; split($NF,arr,"-"); b=arr[2]; getline; sub(/,$/,"",$2); print $1,$2,a,b}' | tail -1 | awk '{print $7}' | uniq

output:
8080

---------- Post updated at 04:35 PM ---------- Previous update was at 04:21 PM ----------

got one more.. which gives all ports for tomcat , server listens..

Code:
netstat -anp | grep `ps -ef | grep catalina | grep -v "grep catalina" | grep -v "catalina.out" | awk '{print $2}' | head -1`  | grep LISTEN | awk '{print $4}'  | awk -F: '{print $NF}'

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Solaris

How to find port number wwn of particular port on dual port HBA,?

please find the below o/p for your reference bash-3.00# fcinfo hba-port HBA Port WWN: 21000024ff295a34 OS Device Name: /dev/cfg/c2 Manufacturer: QLogic Corp. Model: 375-3356-02 Firmware Version: 05.03.02 FCode/BIOS Version: BIOS: 2.02; fcode: 2.01;... (3 Replies)
Discussion started by: sb200
3 Replies

2. UNIX for Advanced & Expert Users

Find PID using a Port?

Hi, I do not have root user credentials nor do I have the functional id of the process that uses port 80. How can I find the pid of the process using the port number 80 ? Operating System: Linux (6 Replies)
Discussion started by: mohtashims
6 Replies

3. HP-UX

Find port for Pid

Hi, Is this the most appropriate way of finding the listen port number given the pid is "16659" ? lsof -Pan -i tcp -i udp | grep 16659 | grep -i "listen"If so, how can I extract "7001" and assign it to a variable say myport=7001 from the below output which happens to be actual port number? ... (1 Reply)
Discussion started by: mohtashims
1 Replies

4. UNIX for Dummies Questions & Answers

Find PID for a port

Hi, I need to find the PID for a given port on the below system. HP-UX mymachine B.11.31 U ia64 3223107173 unlimited-user license How can I ? (4 Replies)
Discussion started by: mohtashims
4 Replies

5. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

6. UNIX for Dummies Questions & Answers

pid from port number in AIX

Hello guys, How to shut down a port number in AIX. May be first I need to find out what is the process ID of that process that listens to this particular port.. Is there any command to find a process ID from the port number other than "lsof". thanks (1 Reply)
Discussion started by: solaix14
1 Replies

7. HP-UX

To find pid from port number

Hi, I am working on HP-UX Release 11i. I want to find the process id (PID) of the process running on a particular port. lsof command fuser does not work on this system. Please suggest some alternative. Thanks (6 Replies)
Discussion started by: gmat
6 Replies

8. UNIX for Dummies Questions & Answers

Session PID & socket connection pid

1. If I use an software application(which connects to the database in the server) in my local pc, how many PID should be registered? Would there be PID for the session and another PID for socket connection? 2. I noticed (through netstat) that when I logged in using the my software application,... (1 Reply)
Discussion started by: pcx26
1 Replies
Login or Register to Ask a Question