如何分割SSH地址和路径? [英] How to split an SSH address + path?

查看:51
本文介绍了如何分割SSH地址和路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3函数接收像user@132.243.32.14:/random/file/path这样的SSH地址.我想使用paramiko lib访问此文件,该lib需要单独的用户名,IP地址和文件路径.

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 ?

推荐答案

str.partitionrpartition将执行您想要的操作:

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

给予:

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

这篇关于如何分割SSH地址和路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆