How to split an SSH address + path?
<p>A Python 3 function receives an SSH address like <code>user@132.243.32.14:/random/file/path</code>. I want to access this file with the <code>paramiko</code> lib, which needs the username, IP address, and file path separately.</p>

<p>How can I split this address into these 3 parts, knowing that the input will sometimes omit the username ?</p>

<p><code>str.partition</code> and <code>rpartition</code> will do what you want:</p>

<pre><code>def ssh_splitter(ssh_connect_string):
    user_host, _, path = ssh_connect_string.partition(':')
    user, _, host = user_host.rpartition('@')
    return user, host, path

print(ssh_splitter('user@132.243.32.14:/random/file/path'))
print(ssh_splitter('132.243.32.14:/random/file/path'))
</code></pre>

<p>gives:</p>

<pre><code>('user', '132.243.32.14', '/random/file/path')
('', '132.243.32.14', '/random/file/path')
</code></pre>

python python-3.x parsing ssh ip-address