This is kind of related to What defines the maximum size for a command single argument?. But it is about the shell itself.
I want to know what defines the maximum size or string length of an command used via ssh. Example would be $ ssh user@remote "echo very big command".
I found some scripts simply testing it via trail and error. But I assume there is a better way. But first I would like to understand what defines that maximum length of the string.
Here is an example on how I trigger errors about the max length:
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import Popen
>>> Popen(['ssh', 'user@localhost', 'echo', '{}'.format('T' * 500000)])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.9/subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.9/subprocess.py", line 1823, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 7] Argument list too long: 'ssh'
The limit on my system (Debian 11 on ARM64hf) starts round about 131067 characters (excluding the starting ssh, echo, etc). But getconf ARG_MAX is much higher 2097152.