zoukankan      html  css  js  c++  java
  • python堡垒机

    堡垒机

    windows下安装python3的paramiko模块后一些报错总结:

    • error: Unable to find vcvarsall.bat

    [官网对此问题的描述] : https://docs.python.org/3.5/using/windows.html#compiling-python-on-windows

    即官方推荐安装VS2015解决编译问题,或者安装MinGW解决,但都太麻烦

    这里引用安装PyCrypto 第三方版解决,如下:

    1. pip install --use-wheel --no-index --find-links=https://github.com/sfbahr/PyCrypto-Wheels/raw/master/pycrypto-2.6.1-cp35-none-win_amd64.whl pycrypto

    2. 然后再次安装paramiko,pip3 install paramiko
      又报错如下:

    “File "D:Program FilesPython35libsite-packagesCryptoRandomOSRNG t.py",line 28, in <module> import winrandom ImportError: No module named 'winrandom'”

    1. 修改python安装目录下D:Program FilesPython35Libsite-packagesCryptoRandomOSRNG t.py文件,将import winrandom改为from . import winrandom,问题解决

    开发堡垒机之前,先来学习Python的paramiko模块,该模块基于SSH用于连接远程服务器并执行相关操作


    ##### SSHClient

    用于连接远程服务器并执行基本命令

    • 基于用户名密码连接:
    import paramiko
    
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    # 允许连接不在know_hosts文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接服务器
    ssh.connect(hostname='192.168.1.82', port=22, username='ivan', password='123')
    
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command('df')
    # 获取命令结果
    result = stdout.read()
    
    # 关闭连接
    ssh.close()
    

    SSHClient 封装 Transport

    import paramiko
    
    transport = paramiko.Transport(('hostname', 22))
    transport.connect(username='ivan', password='123')
    
    ssh = paramiko.SSHClient()
    ssh._transport = transport
    
    stdin, stdout, stderr = ssh.exec_command('df')
    print stdout.read()
    
    transport.close()
    
    • 基于公钥密钥连接:
    import paramiko
    
    private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
    
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    # 允许连接不在know_hosts文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接服务器
    ssh.connect(hostname='192.168.1.82', port=22, username='ma', key=private_key)
    
    # 执行命令
    stdin, stdout, stderr = ssh.exec_command('df')
    # 获取命令结果
    result = stdout.read()
    
    # 关闭连接
    ssh.close()
    

    SSHClient 封装 Transport

    import paramiko
    
    private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
    
    transport = paramiko.Transport(('hostname', 22))
    transport.connect(username='ivan', pkey=private_key)
    
    ssh = paramiko.SSHClient()
    ssh._transport = transport
    
    stdin, stdout, stderr = ssh.exec_command('df')
    
    transport.close()
    
    SFTPClient

    用于连接远程服务器并执行上传下载

    • 基于用户名密码上传下载
    import paramiko
    
    transport = paramiko.Transport(('hostname',22))
    transport.connect(username='ivan',password='123')
    
    sftp=paramiko.SFTPClient.from_transport(transport)
    # 将F:del	est_sftp.log 上传至服务器 /tmp/test_sftp.log
    sftp.put('/tmp/location.py', '/tmp/test.py')
    # 将remove_path 下载到本地 local_path
    sftp.get('remove_path', 'local_path')
    
    transport.close()
    
    • 基于公钥密钥上传下载
    import paramiko
    
    private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')
    
    transport = paramiko.Transport(('hostname', 22))
    transport.connect(username='ivan', pkey=private_key )
    
    sftp = paramiko.SFTPClient.from_transport(transport)
    # 将location.py 上传至服务器 /tmp/test.py
    sftp.put('/tmp/location.py', '/tmp/test.py')
    # 将remove_path 下载到本地 local_path
    sftp.get('remove_path', 'local_path')
    
    transport.close()
    Demo
    
  • 相关阅读:
    Minimum Depth of Binary Tree leetcode java
    Maximum Depth of Binary Tree leetcode java
    Symmetric Tree leetcode java
    Same Tree leetcode java
    Binary Tree Postorder Traversal leetcode java
    Binary Tree Preorder Traversal leetcode java
    Binary Tree Inorder Traversal leetcode java
    Combinations leetcode java
    一键清除Centos iptables 防火墙所有规则
    阿里云centos7.7x64安装open,并配置ip转发和nat伪装
  • 原文地址:https://www.cnblogs.com/microfan/p/5299135.html
Copyright © 2011-2022 走看看