I am creating a local simulator (not connected to the Internet) using an SSH connection. I started using sshd for a specific range of port numbers and NATing a number of devices for them. I need to find the current port number.
OS CentOS 5.5 OpenSSH 6.1
I did the following. It works for normal use (manual user). But when trying a rigorous testing (automated), it seems that sometimes he canβt find the port number.
#!/bin/bash WHOINFO=`who -m` USERNAME=`echo $WHOINFO | awk 'NR==1{print $1}'` PTSNUMBER=`echo $WHOINFO | awk 'NR==1{print $2}'` USERSTR=$USERNAME"@"$PTSNUMBER PID=`ps -eLf | grep $USERSTR | awk 'NR==1{print $3}'` if [ -z "$PID" ]; then exit fi PORTSTR=`netstat -natp | grep $PID | awk 'NR==1{print $4}'` PORTNUMBER=${PORTSTR//*:/} echo $PORTNUMBER
source share