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

6
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.