V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2017-11-13 16:01:40 +08:00 · 1075 次点击
    这是一个创建于 2419 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #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()
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2514 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 11:37 · PVG 19:37 · LAX 04:37 · JFK 07:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.