linux - Seperate IP from port in CSV file using sed/awk -
i using following create csv output shown below. now, output csv want split ip address , port names in different columns , display process id in last column.
script is:-
netstat -anputw | awk '{if ($1 == "tcp") print $1,",",$4,",",$5,",",$6,",",$7}' > $home/mylog/connections_$hostname.csv netstat -anputw | awk '{if ($1 == "udp") print $1,",",$4,",",$5,",",",",$6}' >> $home/mylog/connections_$hostname.csv
output in csv like:-
tcp 127.0.0.1:25 0.0.0.0:* listen 1112/sendmail tcp 192.168.0.38:22 192.168.10.143:62998 established 3084987/sshd
now, wish split ip address , ports (comma seperated) , last column, trim text , display process id. lastly, first column csv should ip address of host name of script run(displayed each of rows). so, output below:-
192.168.0.22 tcp 127.0.0.1 25 0.0.0.0 * listen 1112 192.168.0.22 tcp 192.68.0.38 22 192.168.10.143 62998 established 3084987
any pointers how can achieve in csv output?
like previous question, you're approaching wrong. don't need bunch of separate commands , pipes. don't need multiple awk commands. can want in 1 simple awk command.
you didn't provide output netsat
see awk input looks rough guess need like:
netstat -anputw | awk ' begin {fs="[ :/]"; ofs=","} $1 == "tcp" {x=$8; y=$9} $1 == "udp" {x=""; y=$8} {print $4,$1,$5,$6,$7,x,y} ' > $home/mylog/connections_$hostname.csv
replace "x" , "y" more meaningful names if like, don't know values represent.
if updated question show sample input (i.e. output netstat) plus expected output given input can improve/verify script , more.
Comments
Post a Comment