python远程控制linux服务器(paramiko、fabric与pexpect)(四)

paramiko安装与使用说明

背景

工作中应用系统都是多实例部署的,经常会遇到在多台机器执行同样的操作命令;使用paramiko能减少重复的工作。

安装说明

公司电脑连不了外网,需离线安装;操作系统为64位,python版本为3.7.6;在pypi网站上找到合适的安装包,依次下载安装

  • pycparser-2.19.tar.gz
  • cffi-1.14.0-cp37-cp37m-win_amd64.whl
  • six-1.12.0-py2.py3-none-any.whl
  • bcrypt-3.1.7-cp37-cp37m-win_amd64.whl
  • PyNaCl-1.3.0-cp37-cp37m-win_amd64.whl
  • cryptography-2.9-cp37-cp37m-win_amd64.whl
  • ecdsa-0.15.tar.gz
  • pycrypto-2.6.1.tar.gz
  • paramiko-2.7.1.tar.gz

下载地址

pycparser-2.19.tar.gz下载地址

cffi-1.14.0-cp37-cp37m-win_amd64.whl下载地址

six-1.12.0-py2.py3-none-any.whl的下载地址

bcrypt-3.1.7-cp37-cp37m-win_amd64.whl的下载地址

PyNaCl-1.3.0-cp37-cp37m-win_amd64.whl的下载地址

cryptography-2.9-cp37-cp37m-win_amd64.whl的下载地址

ecdsa-0.15.tar.gz下载地址

pycrypto-2.6.1.tar.gz下载地址

paramiko-2.7.1.tar.gz下载地址

安装指南

  1. 需按顺序安装,同时安装时留意报错信息
  2. tar.gz文件的安装方法,解压后分别进入目录,执行python setup.py build; python setuo.py install
  3. whl文件的安装方法,直接在文件所在目录,执行 pip install 文件名

安装完验证

先执行python,在输入import paramiko,留意是否报错

C:\Users\27004>python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
>>>

示例详解

示例1脚本详解

  1. 调用check脚本,check脚本包含进程、端口、磁盘使用率、数据库网络状况、挂载nas是否正常等检查,同时会输出检查结果到check.log
  2. 搜索check.log文件,找出报错信息,打印出来,或者对接告警平台发出告警
#getpass是隐藏密码
import paramiko,getpass

def ssh_connect(host_ip,user_name,host_port,password):
    # 创建SSH对象
    ssh = paramiko.SSHClient()
    # 允许连接不在know_hosts文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    # 连接服务器
    try:
        ssh.connect(host_ip, host_port, user_name, password)
        return ssh
    except Exception as e:
        print("连接ip为{0}的服务器失败,报错内容如下{1}".format(host_ip,e))
    #finally:
    #    ssh.close()


def execute_command(ssh,str_command):
    # 执行命令并获取执行结果
    stdin, stdout, stderr = ssh.exec_command(str_command)
    execute_result = stdout.read()
    print("execute_result:",execute_result)
    return execute_result

def execute_check(ssh,str_command,host_ip):
    execute_result = execute_command(ssh,str_command)
    if execute_result == '':
        print('execute_result is nothing')
    else:
        print("check execute result is:",execute_result)

if __name__ == '__main__':
    host_ip = '192.168.10.152'
    user_name = 'sully'
    host_port ='22'
    password='sully'
    ssh = ssh_connect(host_ip,user_name,host_port,password)
    print('ssh:',ssh)
   # today_command = "echo `date '+%Y%m%d'`"
   # execute_command(ssh,today_command)
    check_command = 'sh /home/sully/bin/check'
    execute_command(ssh,check_command)
    #str_command = 'grep -Ei "warning|mesg|danger" /home/sully/logs/check.20200412.log'
    str_command = 'grep -Ei "mesg|danger" /home/sully/logs/check.20200412.log'
    execute_check(ssh,str_command,host_ip)
    #read_res_command = "cat /home/sully/logs/check.20200412.log"
    #execute_command(ssh,read_res_command)
    ssh.close()

示例2上传文件到远程服务器

#!/usr/bin/python
import paramiko
  
t = paramiko.Transport(("服务器IP",22))
t.connect(username = "用户名", password = "口令")
sftp = paramiko.SFTPClient.from_transport(t)
remotepath='/tmp/test.txt'
localpath='/tmp/test.txt'
sftp.put(localpath,remotepath)
t.close()

示例3从远程服务器下载文件

#!/usr/bin/python
import paramiko
  
t = paramiko.Transport(("服务器IP",22))
t.connect(username = "用户名", password = "口令")
sftp = paramiko.SFTPClient.from_transport(t)
remotepath='/tmp/test.txt'
localpath='/tmp/test.txt'
sftp.get(remotepath, localpath)
t.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值