iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >系统运维工程师的法宝:python pa
  • 412
分享到

系统运维工程师的法宝:python pa

法宝工程师系统 2023-01-31 03:01:30 412人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

系统运维工程师的法宝:python paramikoPython视频培训班安装:pip install Paramiko paramiko是用python语言写的一个模块,遵循ssh2协议,支持以加密和认证的方式,进行远程服务器的连接。使用

系统运维工程师的法宝:python paramiko

Python视频培训班

安装:pip install Paramiko
paramiko是用python语言写的一个模块,遵循ssh2协议,支持以加密和认证的方式,进行远程服务器的连接。
使用paramiko可以很好的解决以下问题:
需要使用windows客户端,
远程连接到linux服务器,查看上面的日志状态,批量配置远程服务器,文件上传,文件下载等


"paramiko" is a combination of the esperanto Words for "paranoid" and
"friend".  it's a module for python 2.5+ that implements the SSH2 protocol
for secure (encrypted and authenticated) connections to remote Machines.
unlike SSL (aka TLS), SSH2 protocol does not require hierarchical
certificates signed by a powerful central authority. you may know SSH2 as
the protocol that replaced telnet and rsh for secure access to remote
shells, but the protocol also includes the ability to open arbitrary
channels to remote services across the encrypted tunnel (this is how sftp
works, for example).

it is written entirely in python (no C or platfORM-dependent code) and is
released under the GNU LGPL (lesser GPL).

the package and its api is fairly well documented in the "doc/" folder
that should have come with this arcHive.


Requirements
------------

 - python 2.5 or better <Http://www.python.org/>
 - pycrypto 2.1 or better <https://www.dlitz.net/software/pycrypto/>

If you have setuptools, you can build and install paramiko and all its
dependencies with this command (as root)::

   easy_install ./


Portability
-----------

i code and test this library on Linux and MacOS X. for that reason, i'm
pretty sure that it works for all posix platforms, including MacOS. it
should also work on Windows, though i don't test it as frequently there.
if you run into Windows problems, send me a patch: portability is important
to me.

some python distributions don't include the utf-8 string encodings, for
reasons of space (misdirected as that is). if your distribution is
missing encodings, you'll see an error like this::

   LookupError: no codec search functions reGIStered: can't find encoding

this means you need to copy string encodings over from a working system.
(it probably only happens on embedded systems, not normal python
installs.) Valeriy Pogrebitskiy says the best place to look is
``.../lib/python*/encodings/__init__.py``.


Bugs & Support
--------------

Please file bug reports at https://GitHub.com/paramiko/paramiko/. There is currently no mailing list but we plan to create a new one ASAP.


Demo
----

several demo scripts come with paramiko to demonstrate how to use it.
probably the simplest demo of all is this::

   import paramiko, base64
   key = paramiko.RSAKey(data=base64.decodestring('AAA...'))
   client = paramiko.SSHClient()
   client.get_host_keys().add('ssh.example.com', 'ssh-rsa', key)
   client.connect('ssh.example.com', username='strongbad', password='thecheat')
   stdin, stdout, stderr = client.exec_command('ls')
   for line in stdout:
       print '... ' + line.strip('\n')
   client.close()

...which prints out the results of executing ``ls`` on a remote server.
(the host key 'AAA...' should of course be replaced by the actual base64
encoding of the host key.  if you skip host key verification, the
connection is not secure!)

the following example scripts (in demos/) get progressively more detailed:

:demo_simple.py:
   calls invoke_shell() and emulates a terminal/tty through which you can
   execute commands interactively on a remote server.  think of it as a
   poor man's ssh command-line client.

:demo.py:
   same as demo_simple.py, but allows you to authenticiate using a
   private key, attempts to use an SSH-agent if present, and uses the long
   form of some of the API calls.

:forward.py:
   command-line script to set up port-forwarding across an ssh transport.
   (requires python 2.3.)

:demo_sftp.py:
   opens an sftp session and does a few simple file operations.

:demo_server.py:
   an ssh server that listens on port 2200 and accepts a login for
   'robey' (password 'foo'), and pretends to be a BBS.  meant to be a
   very simple demo of writing an ssh server.

:demo_keygen.py:
   an key generator similar to openssh ssh-keygen(1) program with
   paramiko keys generation and progress functions.

Use
---

the demo scripts are probably the best example of how to use this package.
there is also a lot of documentation, generated with epydoc, in the doc/
folder.  point your browser there.  seriously, do it.  mad props to
epydoc, which actually motivated me to write more documentation than i
ever would have before.

there are also unit tests here::

   $ python ./test.py

which will verify that most of the core components are working correctly.

-、执行远程命令:
#!/usr/bin/python
#coding:utf-8
import paramiko
port =22
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("*.*.*.*",port,"username", "password")
stdin, stdout, stderr = ssh.exec_command("你的命令")
print stdout.readlines()
ssh.close()

二、上传文件到远程
#!/usr/bin/python
#coding:utf-8
import paramiko

port =22
t = paramiko.Transport(("IP",port))
t.connect(username = "username", password = "password")
sftp = paramiko.SFTPClient.from_transport(t)
remotepath='/tmp/test.txt'
localpath='/tmp/test.txt'
sftp.put(localpath,remotepath)
t.close()

三、从远程下载文件
#!/usr/bin/python
#coding:utf-8
import paramiko

port =22
t = paramiko.Transport(("IP",port))
t.connect(username = "username", password = "password")
sftp = paramiko.SFTPClient.from_transport(t)
remotepath='/tmp/test.txt'
localpath='/tmp/test.txt'
sftp.get(remotepath, localpath)
t.close()

四、执行多个命令
#!/usr/bin/python
#coding:utf-8

import sys
sys.stderr = open('/dev/null')       # Silence silly warnings from paramiko
import paramiko as pm
sys.stderr = sys.__stderr__
import os

class AllowAllKeys(pm.MissingHosTKEyPolicy):
   def missing_host_key(self, client, hostname, key):
       return

HOST = '127.0.0.1'
USER = ''
PASSWORD = ''

client = pm.SSHClient()
client.load_system_host_keys()
client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
client.set_missing_host_key_policy(AllowAllKeys())
client.connect(HOST, username=USER, password=PASSWORD)

channel = client.invoke_shell()
stdin = channel.makefile('wb')
stdout = channel.makefile('rb')

stdin.write('''
cd tmp
ls
exit
''')
print stdout.read()

stdout.close()
stdin.close()
client.close()

五、获取多个文件
#!/usr/bin/python
#coding:utf-8

import paramiko
import os

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('localhost',username='****')  

apath = '/var/log'
apattern = '"*.log"'
rawcommand = 'find {path} -name {pattern}'
command = rawcommand.format(path=apath, pattern=apattern)
stdin, stdout, stderr = ssh.exec_command(command)
filelist = stdout.read().splitlines()

ftp = ssh.open_sftp()
for afile in filelist:
   (head, filename) = os.path.split(afile)
   print(filename)
   ftp.get(afile, './'+filename)
ftp.close()
ssh.close()

本文由python视频培训班黄老师编写。

--结束END--

本文标题: 系统运维工程师的法宝:python pa

本文链接: https://www.lsjlt.com/news/187354.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 系统运维工程师的法宝:python pa
    系统运维工程师的法宝:python paramikopython视频培训班安装:pip install Paramiko paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接。使用...
    99+
    2023-01-31
    法宝 工程师 系统
  • Linux系统资深运维工程师的进阶秘籍
    2010年毕业,从事IT行业已经接近7个年头,一路走来有很多不足,不论是技术上的还是工作当中的待人接事等,但正是这些不足让我有了现在的进步,技术上从最初的做水晶头,综合布线到服务器上架,网络设备调试,服务架设等,从管理一台网络设备到管理上百...
    99+
    2023-06-05
  • Linux运维工程师常用的操作有哪些
    本篇内容主要讲解“Linux运维工程师常用的操作有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linux运维工程师常用的操作有哪些”吧!1.Shell命令行光标移到行首:Ctrl+a光标移...
    99+
    2023-06-27
  • Linux运维工程师要注意的哪些方面
    这篇文章主要介绍了Linux运维工程师要注意的哪些方面的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Linux运维工程师要注意的哪些方面文章都会有所收获,下面我们一起来看看吧。一、线上操作规范1.测试使用当初学...
    99+
    2023-06-27
  • 运维工程师需要掌握的7大武器
    随着互联网时代的快速发展,各个领域对于终端设备的稳定性、可操作性也提出了更高的要求,于是乎,一个看似神秘的岗位就这么诞生了,这就是----运维工程师。运维工程师 —— “Operations Engineer”,字面意思可理解为管理系统、服...
    99+
    2023-06-05
  • Linux运维工程师入门必备的技术有哪些
    这篇文章主要介绍“Linux运维工程师入门必备的技术有哪些”,在日常操作中,相信很多人在Linux运维工程师入门必备的技术有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Linux运维工程师入门必备的技术...
    99+
    2023-06-13
  • 工程项目运维管理系统的构建及应用
    随着现代信息技术的快速发展,工程项目运维管理系统在工程项目的运行和管理中扮演着越来越重要的角色。本文将对工程项目运维管理系统的构建、应用以及发展趋势进行详细说明。 一、工程项目运维管理系统的构建工程项目运维管理系统通常由多个模块组成,包括项...
    99+
    2023-12-11
    管理系统 工程项目
  • 运维工程师必须掌握的shell技术实战内容
    各类监控脚本,内存,磁盘,端口,URL监控报警如何监控网站目录是否被篡改,以及站点目录批量被篡改后如何恢复如何开发各类服务(rsync,nginx,mysql)等得启动及停止专业脚本如果开发mysql主从同...
    99+
    2024-04-02
  • Linux运维工程师必须学习Shell编程的原因有哪些
    这篇文章将为大家详细讲解有关Linux运维工程师必须学习Shell编程的原因有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。Shell脚本语言是Linux / Unix系统管理以及自动化运维的重要工具...
    99+
    2023-06-05
  • Linux运维工程师的十个基本技能点分别是什么
    这篇文章给大家介绍Linux运维工程师的十个基本技能点分别是什么,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。对于运维工程师来说管理系统必然离不开运维必须要掌握的工具,本篇文章就和大家分享一下Linux 运维工程师的十...
    99+
    2023-06-28
  • 天然气工程运维系统 毕业设计 JAVA+Vue+SpringBoot+MySQL
    作者主页:Designer 小郑 作者简介:3年JAVA全栈开发经验,专注JAVA技术、系统定制、远程指导,致力于企业数字化转型,CSDN博客专家,阿里云社区专家博主,蓝桥云课讲师。 ...
    99+
    2023-10-08
    java 课程设计 毕业设计 spring boot vue.js 天然气 工程管理系统 原力计划
  • 财务系统工程师构建企业财务系统的关键角色
    财务系统工程师是一种在企业中担任重要角色的专业人员,他们的主要任务是负责设计、开发和维护企业财务系统。他们使用财务软件和硬件,确保企业的财务数据准确无误,帮助企业做出更明智的财务决策。 财务系统工程师的主要职责财务系统设计:财务系统工程师需...
    99+
    2023-12-12
    系统 企业财务 角色
  • linux运维工程师必备的网络带宽监控常用命令有哪些
    小编给大家分享一下linux运维工程师必备的网络带宽监控常用命令有哪些,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!一些命令可以显示单个进程所使用的带宽。这样一来,用户很容易发现过度使用网络带宽的某个进程。这些工具使用不同...
    99+
    2023-06-16
  • 数据的工程师:重建操作系统数据的桥梁
    一、准备 收集信息:确定受影响系统的类型、安装的操作系统版本以及丢失或损坏数据的范围。 创建备份副本:在开始重建过程之前,对重要文件和数据创建备份副本至关重要,以防出现意外情况。 获取安装介质:获取与受影响系统兼容的操作系统安装介质,例...
    99+
    2024-04-02
  • 运维工程师怎样才能更好的进阶?-千锋深圳云计算培训
    运维工程师怎样才能更好的进阶-千锋深圳云计算培训云计算的就业前途,某种意义上也可以理解为云计算为我们提供的服务,存在一定的必然性,也就是说云计算对于社会、云计算使用者有哪些优势,也同时可以理解为,云计算的优势就是云计算的就业优势。关于“云计...
    99+
    2023-06-04
  • 网络技术——网络运维工程师必会的网络知识(2)(详细讲解)
    作者简介:一名在校云计算网络运维学生、每天分享网络运维的学习经验、和学习笔记。   座右铭:低头赶路,敬事如仪 个人主页:网络豆的主页​​​​​​ 目录 前言 网络传输介质 信号分类和失真来源地址:https...
    99+
    2023-09-12
    网络 服务器 运维
  • 线程调度大揭密:操作系统维持系统高效运行的幕后英雄
    线程调度的作用 在现代计算机系统中,多个应用程序和任务同时运行是很常见的。为了有效地管理这些并发活动,操作系统引入了线程的概念。线程是应用程序中轻量级的执行单元,它共享应用程序的地址空间和资源。 线程调度负责管理线程的执行顺序,确保每个...
    99+
    2024-03-04
    线程调度 操作系统 并行性 抢占式非抢占式 时间片算法
  • 揭秘操作系统线程管理的策略:优化系统性能的制胜法宝
    线程是操作系统管理资源和控制并发执行的重要机制。通过优化线程管理策略,可以显著提高系统性能和吞吐量。本文将深入探究线程管理的概念,揭示不同的调度算法和策略,并提供基于演示代码的优化技巧,帮助您释放系统的全部潜力。 1. 线程管理概述 线...
    99+
    2024-03-04
    线程、调度、优化、系统性能、并发
  • 架构大师的宝典:使用操作系统 IaaS 构建不可阻挡的云端应用程序
    使用基础架构即服务 (IaaS) 构建云端应用程序是现代软件开发中日益流行的方法。IaaS 提供了一个可扩展、弹性和经济高效的平台,可以让你专注于构建应用程序本身,而不是管理底层基础架构。在本文中,我们将介绍操作系统 IaaS 的概念,并...
    99+
    2024-03-02
    操作系统 IaaS、云端应用程序、架构、可扩展性、弹性
  • win8.1系统如何加入工作组?win8.1系统加入工作组的方法图文教程
      最近有很多小伙伴询问说win8.1怎么加入工作组加入工作组,这样可以更方便与工作和聊天,但是一些初学者对加入工作组的步骤一点都不熟悉,网上查相关win8.1加入工作组的内容比较少,为帮助大家解决此疑惑,下面我们的小编...
    99+
    2023-05-22
    win8.1如何加入工作组 win8怎么加入工作组
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作