推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tqknight
V2EX  ›  Python

Python 学习 002——远程连接 paramiko

  •  
  •   tqknight · Nov 13, 2017 · 1444 views
    This topic created in 3105 days ago, the information mentioned may be changed or developed.
    #coding:utf-8
    
    import paramiko
    
    #创建一个连接的实例
    ssh = paramiko.SSHClient()
    
    #我们需要设定一个白名单
    #know_hosts 访问受信任列表
    
    know_hosts = paramiko.AutoAddPolicy()
    
    ssh.set_missing_host_key_policy(know_hosts)
    
    #链接我们的远程服务器
    ssh.connect(
        hostname="192.168.1.249",
        port=22,
        username="root",
        password="111111"
    )
    stdin, stdout, stderr = ssh.exec_command("mkdir zzz")
    #关闭链接,释放内存
    ssh.close()
    

    ###上传下载文件

    #coding:utf-8
    
    import paramiko
    
    #创建一个连接的实例
    ssh = paramiko.SSHClient()
    
    #我们需要设定一个白名单
    #know_hosts 访问受信任列表
    
    know_hosts = paramiko.AutoAddPolicy()
    
    ssh.set_missing_host_key_policy(know_hosts)
    
    
    trans = paramiko.Transport(
        sock = ("192.168.1.249", 22)
    )
    
    trans.connect(
        username='root',
        password="111111"
    )
    
    sftp = paramiko.SFTPClient.from_transport(trans)
    
    sftp.put("D:\\zzz.txt","/root/zzz.txt")
    
    sftp.get("/root/zzz.txt", "D:\\zzz.txt")
    
    trans.close()
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1166 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 18:08 · PVG 02:08 · LAX 11:08 · JFK 14:08
    ♥ Do have faith in what you're doing.