How to split an SSH address + path?

核能气质少年 提交于 2020-12-12 05:22:56

问题


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 file path separately.

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


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



回答2:


use

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



来源:https://stackoverflow.com/questions/39497199/how-to-split-an-ssh-address-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!