Ssh argument length

From EggeWiki
Revision as of 21:20, 17 January 2011 by Brianegge (talk | contribs)

On Solaris 10, you can create command up to about 1MB in length.

<geshi lang="bash"> $ getconf ARG_MAX 1048320 </geshi>

However, what is the max length of a command which you can execute via ssh? To answer this, I wrote a basic script to test this.

<geshi lang="bash">

  1. !/bin/bash
  1. usage: <host> <arg size guess>

host=${1-localhost} mid=${2-1048320} r=$((mid/2))

while [ $r -gt 0 ]; do

 printf "Checking length $mid    \r"
 # this command only works if you have base64 and GNU versions of head
 #l=$(ssh $host printf "$(yes | base64 -w 0 | head -c$mid)" | wc -c) 
 # The follow command should work on older Solaris hosts - Thanks Alexis
 l=$(ssh $host printf "$(cat /dev/urandom | tr -cd 'a-f0-9' | dd count=1 bs=$mid)" | wc -c)
 if [ $l -eq $mid ]; then
   mid=$((mid+r))
 else
   mid=$((mid-r))
 fi
 r=$((r/2))

done

echo echo "Maximum ssh argument length between $(hostname) and $host is $mid" </geshi>

The results of the script look like:

~/bin/check-sshlength.sh everest
Received disconnect from 10.0.0.92: 2: Bad packet length 1000044.
Received disconnect from 10.0.0.92: 2: Bad packet length 500044.
Received disconnect from 10.0.0.92: 2: Bad packet length 375036.
Received disconnect from 10.0.0.92: 2: Bad packet length 312540.
Received disconnect from 10.0.0.92: 2: Bad packet length 281292.
Received disconnect from 10.0.0.92: 2: Bad packet length 265660.
Received disconnect from 10.0.0.92: 2: Bad packet length 263708.
Received disconnect from 10.0.0.92: 2: Bad packet length 262732.
Received disconnect from 10.0.0.92: 2: Bad packet length 262252.
Received disconnect from 10.0.0.92: 2: Bad packet length 262188.
Received disconnect from 10.0.0.92: 2: Bad packet length 262156.
Received disconnect from 10.0.0.92: 2: Bad packet length 262156.

Maximum ssh argument length between everest and everest is 262111

This makes sense as this jives with the maximum packet length of an ssh session. http://www.free.lp.se/fish/rfc.txt

If you need more than is, one could pipe the command into stdin and then execute it, or create a script on the remote side and execute that script.

If you default shell is csh, you will get output like

$ ~/bin/check-sshlength.sh everest 
Received disconnect from 10.0.0.92: 2: Bad packet length 1000044.
Received disconnect from 10.0.0.92: 2: Bad packet length 500044.
Word too longth 250000    
Word too longth 125000    
Word too longth 62500    
Word too longth 31250    
Word too longth 15625    
Word too longth 7813    
Word too longth 3907    
Word too longth 1954    
Word too longth 1466    
Word too longth 1222    
Word too longth 1100    
Word too longth 1039    
Word too longth 1024    
Word too longth 1020    

Maximum ssh argument length between everest and everest is 1019

See Top Ten Reasons not to use the C shell