Skip to content Skip to sidebar Skip to footer

How To Split An Ssh Address + Path?

A Python 3 function receives an SSH address like user@132.243.32.14:/random/file/path. I want to access this file with the paramiko lib, which needs the username, IP address, and f

Solution 1:

str.partition and rpartition will do what you want:

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'))

gives:

('user', '132.243.32.14', '/random/file/path')
('', '132.243.32.14', '/random/file/path')

Solution 2:

use

not set(p).isdisjoint(set("0123456789$,")) where p is the SSH.

Post a Comment for "How To Split An Ssh Address + Path?"