1

I have a little program that gets the pids of 3 processes that are listening on different ports:

Admins-Mac-mini-2:~ gitlabuser$ for pn in 23 33 43; do lsof -nP -iTCP:47${pn} | grep LISTEN | awk '{print $2}'; done
44171
44176
44181
Admins-Mac-mini-2:~

I want to run this little loop on a different machine so I tried backslashing the single quotes around the awk print like so:

Admins-Mac-mini-2:~ gitlabuser$ ssh gitlabuser@10.18.66.99 'for pn in 23 33 43; do lsof -nP -iTCP:47${pn} | grep LISTEN | awk \'{print $2}\'; done'
> 

I also tried backslashing the $ :

Admins-Mac-mini-2:~ gitlabuser$ ssh gitlabuser@10.18.66.99 'for pn in 23 33 43; do lsof -nP -iTCP:47${pn} | grep LISTEN | awk \'{print \$2}\'; done'

>

It there a way to run that same command on both my local machine and the remote machine?

A

amadain
  • 111

1 Answers1

0

You can use below method

for i in remoteserverip
do
ssh -o 'StrictHostKeyChecking no' $i <<'EOF'
for pn in 23 33 43
do
lsof -nP -iTCP:47${pn} | grep LISTEN | awk '{print $2}'
done
EOF
done