Paramiko

该模块基于SSH用于连接远程服务器并执行相关操作

SSHClient

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

import paramiko

# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机(第一次登陆时需要输入yes,这行代码自动输入yes)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='c1.salt.com', port=22, username='rooti', 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='rooti', 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='c1.salt.com', port=22, username='rooti', 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='wupeiqi', 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='root',password='123') 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()

 

基于公钥:

import paramiko

private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa')

transport = paramiko.Transport(('hostname', 22))
transport.connect(username='wupeiqi', 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()

 

 

数据库

python MySQL API

插入数据:

 

import MySQLdb

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb')

cur = conn.cursor()

reCount = cur.execute('insert into UserInfo(Name,Address) values(%s,%s)',('alex','usa'))
# reCount = cur.execute('insert into UserInfo(Name,Address) values(%(id)s, %(name)s)',{'id':12345,'name':'wupeiqi'}) conn.commit() cur.close()
conn.close() print(reCount)

批量插入:

import MySQLdb

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb')

cur = conn.cursor()

li =[
('alex','usa'),
('sb','usa'),
]
reCount = cur.executemany('insert into UserInfo(Name,Address) values(%s,%s)',li) conn.commit()
cur.close()
conn.close() print(reCount)

 

删除数据:

import MySQLdb

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb')

cur = conn.cursor()

reCount = cur.execute('delete from UserInfo')

conn.commit()

cur.close()
conn.close() print(reCount)

 

修改数据:

import MySQLdb

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb')

cur = conn.cursor()

reCount = cur.execute('update UserInfo set Name = %s',('alin',))

conn.commit()
cur.close()
conn.close() print(reCount)

 

查询数据:

import MySQLdb

conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb')
cur = conn.cursor() reCount = cur.execute('select * from UserInfo') print(cur.fetchone())
print(cur.fetchone())
cur.scroll(-1,mode='relative')
print(cur.fetchone())
print(cur.fetchone())
cur.scroll(0,mode='absolute')
print(cur.fetchone())
print(cur.fetchone()) cur.close()
conn.close() print(reCount) # ############################## fetchall ############################## import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb')
#cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
cur = conn.cursor() reCount = cur.execute('select Name,Address from UserInfo') nRet = cur.fetchall() cur.close()
conn.close() print reCount
print nRet
for i in nRet:
print(i[0],i[1])

Paramiko,数据库的更多相关文章

  1. JSP应用开发 -------- 电纸书(未完待续)

    http://www.educity.cn/jiaocheng/j9415.html JSP程序员常用的技术   第1章 JSP及其相关技术导航 [本章专家知识导学] JSP是一种编程语言,也是一种动 ...

  2. paramiko与MySQL数据库

    一.paramiko 1.利用paramiko连接远端服务器 import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_p ...

  3. Python Paramiko模块与MySQL数据库操作

    Paramiko模块批量管理:通过调用ssh协议进行远程机器的批量命令执行. 要使用paramiko模块那就必须先安装这个第三方模块,仅需要在本地上安装相应的软件(python以及PyCrypto), ...

  4. python--第十一天总结(paramiko 及数据库操作)

    堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 实现思路 堡垒机执行流程: 管理员为用户在服务器上创建账号(将公钥放置服务器, ...

  5. python之实现基于paramiko和mysql数据库的堡垒机

    一.堡垒机结构 堡垒机执行流程: 管理员为用户在服务器上创建账号(将公钥放置服务器,或者使用用户名密码) 用户登陆堡垒机,输入堡垒机用户名密码,现实当前用户管理的服务器列表 用户选择服务器,并自动登陆 ...

  6. Python之路第一课Day10--随堂笔记(异步IO\数据库\队列\缓存)

    本节内容 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 RabbitMQ队列 Redis\Memcached缓存 Paramiko SS ...

  7. 利用paramiko模块实现堡垒机+审计功能

    paramiko模块是一个远程连接服务器,全真模拟ssh2协议的python模块,借助paramiko源码包中的demos目录下:demo.py和interactive.py两个模块实现简单的堡垒机+ ...

  8. Python之操作Redis、 RabbitMQ、SQLAlchemy、paramiko、mysql

    一.Redis Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.Redis是一个key-value存储系统.和 ...

  9. SQLALchemy(连表)、paramiko

    本节内容:

随机推荐

  1. SSM整合RocketMQ

    前言 RocketMQ是一个由阿里巴巴开源的消息中间件,脱胎于阿里内部使用的MetaQ,本文主要是写个小例子演示一下消息从生产到消费的过程. RocketMQ下载和安装 下载地址 http://roc ...

  2. Hadoop学习资料整理

    1.hadoop相关 hadoop 0.18文档(详细介绍Hadoop,MapReduce,FS Shell,Streaming等) hadoop资料汇总 2.实习的时候用的是streaming,非j ...

  3. 【HTML5】使用 JavaScript 来获取电池状态(Battery Status API)

    HTML5 规范已经越来越成熟,可以让你访问更多来自设备的信息,其中包括最近提交的 "Battery Status API".如其名称所示,该 API 允许你通过 JavaScri ...

  4. Linux驱动入门——构建和运行模块

    Hello world模块 本文介绍如何向内核中添加一个hello模块.该模块的功能是在模块加载时,向系统日志输出"hello world\n" 在模块卸载时输出"Goo ...

  5. 三张图看懂 clientheight、offsetheight、scrollheight

    clientheight clientheigh: 内容的可视区域,不包含border. clientheight=padding + height - 横向滚动轴高度. The Element.cl ...

  6. UART介绍

    https://baike.baidu.com/item/UART/4429746?fr=aladdin

  7. flowable ContentEngine和ContentEngineConfiguration的关系

    一.CommandExecutor ContentEngineConfiguration继承自 AbstractEngineConfiguration. 在 AbstractEngineConfigu ...

  8. angular 中不要使用location.href

    location.href = '#/HKorderList?gid='+gid+'&gname='+encodeURIComponent(gname)+'&cPeriod='+$(' ...

  9. NSLog 打印出方法函数,行数,内容

    #if DEBUG #define NSLog(FORMAT, ...) fprintf(stderr,"\nfunction:%s line:%d content:%s\n", ...

  10. iOS-----使用AVAudioPlayer播放音乐

    使用AVAudioPlayer播放音乐 AVAudioPlayer是一个属于AVFoundation.framework的类.它作用类似于一个功能强大的播放器.AVAudioPlayer支持广泛的音频 ...