问题
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