paramiko

Python monitor SSH Telnet SNMP command Material

丶灬走出姿态 提交于 2020-03-27 23:16:51
First, refer https://blog.csdn.net/yannanxiu/article/details/55045108 Scenario: Windows -------- Monitor -------- > Linux via paramiko SSH login with psutil to get informaiton import paramiko import os import psutil import json ##读取当前路径 base_dir=os.getcwd() ##读取在远程主机执行的脚本 cmd_filepath=base_dir+r"\pu.txt" cmd_file=open(cmd_filepath,"r") cmd=cmd_file.read() ##连接远程主机 client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect("192.168.42.62", 22,'root','Abcd1234') ##执行命令 stdin, stdout, stderr = client.exec_command(cmd) ##读取信息 for line in stdout: data

常用模块介绍

孤街醉人 提交于 2020-03-25 03:10:27
subprocess模块 它是可以远程操作主机,模拟shell命令 'dir'是windows上查看任务的命令 import subprocess obj = subprocess.Popen('dir', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) #Popen是模拟cmd命令,'dir'是windows上查看任务的命令,shell=True是指可以用shell命令了, 输出和错误,放到管道(PIPE)里 # print(obj.stdout.read().decode('gbk')) #打印输出的内容 # print(obj.stderr.read().decode('gbk')) #打印错误的内容 res = obj.stdout.read() + obj.stderr.read() #错误和输入放到一起给变量res print(res.decode('gbk')) #打印res 在linux里可以创建python脚本,写入 import subprocess obj = subprocess.Popen('ls;lsblk', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) #Popen是模拟cmd命令,'dir

Python远程登陆服务器应该这样玩

别来无恙 提交于 2020-03-15 07:43:16
在使用 Python 写一些 脚本 的时候,在某些情况下,我们需要频繁登陆远程服务去执行一次 命令 ,并返回一些结果。 在使用 Python 写一些 脚本 的时候,在某些情况下,我们需要频繁登陆远程服务去执行一次 命令 ,并返回一些结果。 在 shell 环境中,我们是这样子做的。 $ sshpass -p ${passwd} ssh -p ${port} -l ${user} -o StrictHostKeyChecking=no xx.xx.xx.xx "ls -l" 然后你会发现,你的输出有很多你并不需要,但是又不去不掉的一些信息(也许有方法,请留言交流),类似这样 host: xx.xx.xx.xx, port: xx Warning: Permanently added '[xx.xx.xx.xx]:xx' (RSA) to the list of known hosts. Login failure: [Errno 1] This server is not registered to rmp platform, please confirm whether cdn server. total 4 -rw-r--r-- 1 root root 239 Mar 30 2018 admin-openrc 对于直接使用 shell 命令,来执行命令的,可以直接使用管道

error: command 'x86_64-linux-gnu-gcc' failed

时光毁灭记忆、已成空白 提交于 2020-03-15 02:19:01
在linux服务器中 安装paramiko模块报错如下: build/temp.linux-x86_64-3.5/_openssl.c:493:30: fatal error: openssl/opensslv.h: No such file or directory compilation terminated. error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 ---------------------------------------- Can't rollback cryptography, nothing uninstalled. 首先检查openssl和openssl-devel是否正常安装(Ubuntu不需要安装openssl-devel) 再检查libssl-dev是否安装 都安装之后再pip3 install paramiko模块 来源: 51CTO 作者: 珊子的木瓜 链接: https://blog.51cto.com/xiaoshanzi/2142134

使用Cygwin和 mingw 安装 python paramiko模块

我怕爱的太早我们不能终老 提交于 2020-03-11 22:24:49
1. 所需软件包 python http://www.python.org/ftp/python/ paramiko http://www.lag.net/paramiko/download/paramiko-1.7.7.1.zip Cygwin http://cygwin.com/setup.exe pycrypto http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz mingw http://sourceforge.net/projects/mingw/ 2. 安装 python (忽略) 3. 安装mingw ,如果本地没有gcc 则需要安装这个。下载后的exe文件进行网络安装,假设目录为C:\mingw,在PATH中加入 C:\mingw\bin,并在c:\python24\lib\distutils下新建一个名称是distutils.cfg的文件,填入: [build] compiler=mingw32 4. 安装Cygwin 下载后的exe文件进行网络安装 http://www.cygwin.cn/site/install/ ,这里我们需要选择deve包里的 binutils 其他的可以不选 5. 安装PyCrypto 解压缩 在dos下进入解压缩的目录,运行 python setup

windows下python SSH-Client模块paramiko的安装与修改

和自甴很熟 提交于 2020-03-07 19:55:54
安装篇: 1. 下载并安装 easy_install, 使用命令: C:\Python27>python.exe ez_setup.py 下载 RUL:http://peak.telecommunity.com/dist/ez_setup.py 2. 下载 pycryto ,根据实际版本进行下载并安装,我用的 window 7 64bit , python 2.7.2 64bit 下载 URL : http://www.voidspace.org.uk/python/modules.shtml#pycrypto 3. 安装 paramiko 使用命令: C:\Python27\Scripts>easy_install.exe paramiko 4. 测试,打开 python IDLE ,输入 import paramiko 看是否报错(无错表示安装成功),同时输入 paramiko.__file__ 查看并记录对应 egg 文件位置 源码改进篇: 问题描述 :使用函数 paramiko.util.log_to_file('d:\\paramiko.log') ,可实现对 SSH 登录及操作过程中的 log 记录功能,但是每次运行脚本调用该函数 paramiko.log 文件都会被覆盖,这将导致 log 文件的内容丢失,具体请看源代码: 源码 URL : http://www.lag

python paramiko的使用介绍

三世轮回 提交于 2020-03-03 17:38:41
一: 使用paramiko #设置ssh连接的远程主机地址和端口 t=paramiko.Transport((ip,port)) #设置登录名和密码 t.connect(username=username,password=password) #连接成功后打开一个channel chan=t.open_session() #设置会话超时时间 chan.settimeout(session_timeout) #打开远程的terminal chan.get_pty() #激活terminal chan.invoke_shell() 然后就可以通过chan.send('command')和chan.recv(recv_buffer)来远程执行命令以及本地获取反馈。 二: paramiko的两个模块介绍 paramiko有两个模块SSHClient()和SFTPClient() SSHClient()的使用代码: import paramiko ssh = paramiko.SSHClient() # 创建SSH对象 # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname='192.168.2.103', port=22,

python 连接linux服务器

风流意气都作罢 提交于 2020-03-03 17:02:57
import paramiko class Monitor(object): def __init__(self, server_ip, user, pwd): """ 初始化ssh客户端 """ try: client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.client = client print('------------开始连接服务器(%s)-----------' % server_ip) self.client.connect(server_ip, 22, username=user, password=pwd, timeout=4) print('------------认证成功!.....-----------') except Exception: print(f'连接远程linux服务器(ip:{server_ip})发生异常!请检查用户名和密码是否正确!') def link_server(self, cmd): """连接服务器发送命令""" try: stdin, stdout, stderr = self.client.exec_command(cmd) content = stdout.read().decode(

Python学习第九天

拟墨画扇 提交于 2020-03-03 08:29:20
生产者-消费者模型: 看下面的例子哦: 1 import threading,queue 2 import time 3 def consumer(n): 4 while True: 5 print("\033[32;1mconsumer [%s]\033[0m get task: %s"%(n,q.get())) 6 time.sleep(1) 7 q.task_done() 8 def producer(n): 9 count=1 10 while True: 11 print('producer [%s] produced a new task: %s'%(n,count)) 12 q.put(count) 13 count += 1 14 q.join()#queue is emtpy 15 print('all tasks has been cosumed by consumers...') 16 q=queue.Queue() 17 c1=threading.Thread(target=consumer,args=[1,]) 18 c2=threading.Thread(target=consumer,args=[2,]) 19 c3=threading.Thread(target=consumer,args=[3,]) 20 21 p=threading.Thread