【问题标题】:How to split an SSH address + path?如何拆分 SSH 地址 + 路径?
【发布时间】:2017-01-22 15:48:43
【问题描述】:

Python 3 函数接收 SSH 地址,例如 user@132.243.32.14:/random/file/path。我想用paramiko lib访问这个文件,需要分别输入用户名、IP地址和文件路径。

知道输入有时会省略用户名,我如何将这个地址分成这 3 部分?

【问题讨论】:

  • 为什么不使用SSHClient() 来执行此操作?
  • 怎么办?我试图将完整地址传递给SSHClient.connect(),但出现错误。不记得是哪个了,抱歉。

标签: python python-3.x parsing ssh ip-address


【解决方案1】:

使用

not set(p).isdisjoint(set("0123456789$,")) 其中 p 是 SSH。

【讨论】:

    【解决方案2】:

    str.partitionrpartition 会做你想做的事:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-10-06
      相关资源
      最近更新 更多