[EdLUG] SSH - ssh-copy-id problem | RPi | Ubuntu.

Justin B Rye justin.byam.rye at gmail.com
Tue Mar 30 15:05:28 UTC 2021


Robert McWilliam wrote:
> On Tue, 30 Mar 2021, at 13:09, Justin B Rye wrote:
>>> pstree -sup $(ps aux | grep sshd |  awk '{print $2}' | sort -n | head -n 1)
>>> 
>> 
>> As a general rule of thumb, any complicated pipeline like that is
>> really a job for pgrep (which you probably get in the same package as
>> ps).  The above simplifies down to
>> 
>>  pstree -sup $(pgrep sshd)
> 
> You've thrown away a bit too much of the pipeline there: pgrep will
> give multiple results if there are SSH sessions and only want one for
> pstree so need the head part (and possibly the sort - I think ps and
> pgrep will give processes in order of PID without having to sort but
> chucking sort in the pipeline was quicker than checking).

When you want the oldest, that's "pgrep -o sshd"; taking the lowest
number will mess up if the PIDs wrap around.  Mind you, I was
surprised by how hard it has become to do this - I hadn't previously
noticed that /proc/sys/kernel/pid_max has gone up from 32,768 on my
old stable desktop to 4,194,304 on my testbed machine!

[...]
> Yes, just explaining where to manually copy the PID from is probably
> easier, but where's the fun in that :) 

I hope you'll forgive me my addiction to shell golf then!
-- 
Justin B Rye
http://jbr.me.uk/



More information about the EdLUG mailing list