一、Python的paramiko模块介绍

Python 的paramiko模块,该模块和SSH用于连接远程服务器并执行相关操作

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

基于用户名和密码的SSH连接代码如下:

 1 import paramiko
2
3 ssh = paramiko.SSHClient() #创建SSH对象。
4 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #允许连接不在know_hosts文件中的主机
5 ssh.connect(hostname="10.249.1.230",port=22,username="root",password="123456") #连接服务器
6 stdin,stdout,stderr = ssh.exec_command("df") #执行命令
7 result = stdout.read() #获取命令结果
8
9 ssh.close() #关闭连接
10 print result 执行结果:

C:\Python27\python.exe E:/code/homework/sshclient.py
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
9385620 1195720 7713128 14% /
tmpfs 2009884 0 2009884 0% /dev/shm
/dev/vda1 396672 30048 346144 8% /boot

SSH基于用户名和密码的连接,实现二:

 1 import paramiko
2 transport = paramiko.Transport(("10.249.1.230",22))
3 transport.connect(username="root",password="123456")
4 ssh = paramiko.SSHClient()
5 ssh._transport = transport
6
7 #stdin,stdout,stderr = ssh.exec_command("df")
8 stdin,stdout,stderr = ssh.exec_command("df")
9 print stdout.read()
10 #print stdout.read()
11 transport.close()

二、堡垒机的实现

2.1、实现思路:

2.2、堡垒机执行流程:

1、管理员为用户在服务器上创建账号(将公钥放置在服务器,或者使用用户名和密码的认证);

2、用户登录堡垒机,输入用户名和密码,实现当前用户管理的服务器列表;

3、用户选择需要登录的服务器,并实现自动登录;

4、执行相应的操作并将用户所有的操作记录的审计平台。

2.3、堡垒机实现过程

步骤一:实现用户登录:

1 import getpass
2 user = raw_input("username:")
3 pwd =getpass.getpass("password")
4 if user =="root" and pwd =="123":
5 print "登录成功"
6 else:
7 print "登录失败"

步骤二:根据用户获取相关服务器列表:

 1 dic = {
2 "root":[
3 "10.249.1.230",
4 "10.249.1.231",
5 "www.baidu.com",
6 ],
7 "eric":[
8 "10.249.1.244",
9 ]
10 }
11
12 host_list = dic["root"]
13
14 print "please select:"
15 for index,item in enumerate(host_list,1):
16 print index,item
17
18 inp = raw_input("your select (No):")
19 inp = input(inp)
20 hostname = host_list[inp-1]
21 port =22

步骤三:根据用户名、私钥登录服务器

 1 tran = paramiko.Transport((hostname,port,))
2 tran.start_client()
3 default_path = os.path.join(os.environ["HOME"],".ssh","id_rsa")
4 key = paramiko.RSAKey.from_private_key_file(default_path)
5 tran.auth_publickey("jason",key)
6
7 chan = tran.open_session() #打开一个通道
8 chan.get_pty() #获取一个终端
9 chan.invoke_shell() #激活器
10
11 """
12 利用sys.stdin,肆意妄为执行操作
13 用户在终端输入内容,并将内容发送到远程服务器
14 远程服务器执行命令,并将结果返回
15 在用户终端显示内容
16 """
17
18 chan.close()
19 tran.close() 建立连接之后,就开始传输数据了,来看下面的实现方式吧!!!!! 实现方式:
 1 while True:
2 """
3 监视用户输入和服务器返回数据
4 sys.stdin 处理用户输入
5 chan 是之前创建的通道,用于接收服务器返回信息
6 """
7 readable,writeable,error = select.select([chan,sys.stdin,],[],[],1)
8 if chan in readable:
9 try:
10 x = chan.recv(1024)
11 if len(x) ==0:
12 print "\r\n*** EOF\r\n",
13 break
14 sys.stdout.write(x)
15 sys.stdout.flush()
16 except socket.timeout:
17 pass
18 if sys.stdin in readable:
19 inp = sys.stdin.readable()
20 chan.sendall(inp)
三、堡垒机下数据库的基本操作

3.1创建数据库:create database [name];  例如:create database messi;    这样就创建了一个数据库名称为:messi.一定要记得后面要加(;)分好哦。。

3.2 使用这个数据库   use messi;   即:使用刚刚创建的messi数据库

3.3、在数据库中创建数据表,操作如下:记住:创建数据表的时候如下代码第3行,一定要输入几个空格,并使后面的内容对齐哦。如下代码所示:

 1 mysql> create table students
2 -> (
3 -> id int not null auto_increment primary key,
4 -> name char(8) not null,
5 -> sex char(4) not null,
6 -> age tinyint unsigned not null,
7 -> tel char(13) null default "-"
8 -> );
9 Query OK, 0 rows affected (0.03 sec)
10
11 mysql> mysql> show tables;

+-------------------+
| Tables_in_xiaoluo |
+-------------------+
| students |
+-------------------+
1 row in set (0.00 sec)

 

3.4、数据操作:

 插入数据操作:

 1 mysql> insert into students(name,sex,age,tel) values("oliver","F",22,"1310000000")
2 -> ;
3 Query OK, 1 row affected (0.00 sec)
4
5 mysql> select * from students;
6 +----+--------+-----+-----+------------+
7 | id | name | sex | age | tel |
8 +----+--------+-----+-----+------------+
9 | 1 | oliver | F | 22 | 1310000000 |
10 +----+--------+-----+-----+------------+
11 1 row in set (0.00 sec) 删除数据操作:

mysql> delete from students where id = 2;
   Query OK, 1 row affected (0.00 sec)

更新数据操作:

  mysql> update students set tel = "1320000000" where name = "jason";Query OK, 1 row affected (0.00 sec)
  Rows matched: 1 Changed: 1 Warnings: 0

查询数据操作:

mysql> select * from students;
+----+--------+-----+-----+------------+
| id | name | sex | age | tel |
+----+--------+-----+-----+------------+
| 1 | oliver | F | 22 | 1310000000 |
| 5 | jason | M | 18 | 1320000000 |
| 3 | alice | M | 37 | 1330000000 |
| 4 | cindy | F | 27 | 1340000000 |
| 6 | anny | M | 18 | 1350000000 |
| 7 | rice | M | 18 | 1350000000 |
| 8 | bily | M | 27 | 1370000000 |
+----+--------+-----+-----+------------+
7 rows in set (0.00 sec)

 
3.5、Python MySQL API
3.5.1、插入数据
1 import MySQLdb
2 conn = MySQLdb.connect(host="127.0.0.1",user="root",passwd="123.com",db="xiaoluo")
3 cur = conn.cursor()
4 reCount = cur.execute("insert into students(name,sex,age,tel) values(%s,%s,%s,%s)",("jet","M",30,"139999999"))
5 conn.commit()
6 cur.close()
7 conn.close()
8
9 print reCount 程序运行结果如下:

mysql> select * from students;
+----+--------+-----+-----+------------+
| id | name | sex | age | tel |
+----+--------+-----+-----+------------+
| 1 | oliver | F | 22 | 1310000000 |
| 5 | jason | M | 18 | 1320000000 |
| 3 | alice | M | 37 | 1330000000 |
| 4 | cindy | F | 27 | 1340000000 |
| 6 | anny | M | 18 | 1350000000 |
| 7 | rice | M | 18 | 1350000000 |
| 8 | bily | M | 27 | 1370000000 |
| 9 | alex | M | 23 | 131111111 |
| 10 | jet | M | 30 | 139999999 |
+----+--------+-----+-----+------------+
9 rows in set (0.00 sec)

mysql>

3.5.2 批量数据插入:

 1  1 import MySQLdb
2 2 conn = MySQLdb.connect(host="127.0.0.1",user="root",passwd="123.com",db="xiaoluo")
3 3 cur = conn.cursor()
4 4 #reCount = cur.execute("insert into students(name,sex,age,tel) values(%s,%s,%s,%s)",("jet","M",30,"139999999"))
5 5 li=[
6 6 ("AAA","F",31,"138111"),
7 7 ("BBB","F",32,"138222"),
8 8 ("CCC","M",33,"138333"),
9 9 ]
10 10 reCount = cur.executemany("insert into students(name,sex,age,tel) values(%s,%s,%s,%s)",li)
11 11
12 12
13 13 conn.commit()
14 14
15 15 cur.close()
16 16 conn.close()
17 17
18 18 print reCount
19
20 插入结果如下所示:
21 mysql> select * from students;
22 +----+--------+-----+-----+------------+
23 | id | name | sex | age | tel |
24 +----+--------+-----+-----+------------+
25 | 1 | oliver | F | 22 | 1310000000 |
26 | 5 | jason | M | 18 | 1320000000 |
27 | 3 | alice | M | 37 | 1330000000 |
28 | 4 | cindy | F | 27 | 1340000000 |
29 | 6 | anny | M | 18 | 1350000000 |
30 | 7 | rice | M | 18 | 1350000000 |
31 | 8 | bily | M | 27 | 1370000000 |
32 | 9 | alex | M | 23 | 131111111 |
33 | 10 | jet | M | 30 | 139999999 |
34 | 11 | AAA | F | 31 | 138111 |
35 | 12 | BBB | F | 32 | 138222 |
36 | 13 | CCC | M | 33 | 138333 |
37 +----+--------+-----+-----+------------+
38 12 rows in set (0.00 sec)

3.5.3 删除表:student的id==1的条目 数据:

 1 import MySQLdb
2 conn = MySQLdb.connect(host="127.0.0.1",user="root",passwd="123.com",db="xiaoluo")
3 cur = conn.cursor()
4 values(%s,%s,%s,%s)",("jet","M",30,"139999999"))
5 students(name,sex,age,tel) values(%s,%s,%s,%s)",li)
6 reCount = cur.execute("delete from students where id =1")
7
8 conn.commit()
9
10 cur.close()
11 conn.close()
12
13 print reCount
14 ~
15 ~

3.5.4 修改student中的所有名字为alin

 1 import MySQLdb
2 conn = MySQLdb.connect(host="127.0.0.1",user="root",passwd="123.com",db="xiaoluo")
3 cur = conn.cursor()
4
5
6 reCount = cur.execute("update students set name=%s",("alin",))
7
8
9 conn.commit()
10
11 cur.close()
12 conn.close()
13
14 print reCount 执行结果:

mysql> select * from students;
+----+------+-----+-----+------------+
| id | name | sex | age | tel |
+----+------+-----+-----+------------+
| 5 | alin | M | 18 | 1320000000 |
| 3 | alin | M | 37 | 1330000000 |
| 4 | alin | F | 27 | 1340000000 |
| 6 | alin | M | 18 | 1350000000 |
| 7 | alin | M | 18 | 1350000000 |
| 8 | alin | M | 27 | 1370000000 |
| 9 | alin | M | 23 | 131111111 |
| 10 | alin | M | 30 | 139999999 |
| 11 | alin | F | 31 | 138111 |
| 12 | alin | F | 32 | 138222 |
| 13 | alin | M | 33 | 138333 |
+----+------+-----+-----+------------+
11 rows in set (0.00 sec)

mysql>

3.5.5 查询数据

 1 import MySQLdb
2 conn = MySQLdb.connect(host="127.0.0.1",user="root",passwd="123.com",db="xiaoluo")
3 cur = conn.cursor() 12 14 reCount =cur.execute("select * from students")
15 print cur.fetchone()
16 print cur.fetchone()
17 cur.scroll(-1,mode="relative")
18 print cur.fetchone()
19 print cur.fetchone()
20 cur.scroll(0,mode="absolute")
21 print cur.fetchone()
22 print cur.fetchone()
原数据库表结构:

mysql> select * from students;
+----+------+-----+-----+------------+
| id | name | sex | age | tel |
+----+------+-----+-----+------------+
| 5 | alin | M | 18 | 1320000000 |
| 3 | alin | M | 37 | 1330000000 |
| 4 | alin | F | 27 | 1340000000 |
| 6 | alin | M | 18 | 1350000000 |
| 7 | alin | M | 18 | 1350000000 |
| 8 | alin | M | 27 | 1370000000 |
| 9 | alin | M | 23 | 131111111 |
| 10 | alin | M | 30 | 139999999 |
| 11 | alin | F | 31 | 138111 |
| 12 | alin | F | 32 | 138222 |
| 13 | alin | M | 33 | 138333 |
+----+------+-----+-----+------------+
11 rows in set (0.00 sec)


执行结果:
 
[root@centos6 lab]# python mysql.py 
(5L, 'alin', 'M', 18, '1320000000')
(3L, 'alin', 'M', 37, '1330000000')
(3L, 'alin', 'M', 37, '1330000000')
(4L, 'alin', 'F', 27, '1340000000')
(5L, 'alin', 'M', 18, '1320000000')
(3L, 'alin', 'M', 37, '1330000000')

Python的平凡之路(13)的更多相关文章

  1. Python的平凡之路(8)

    (本文是对平凡之路(7)的补充等) 一.动态导入模块 import importlib __import__('import_lib.metaclass') #这是解释器自己内部用的 #importl ...

  2. Python的平凡之路(16)

    一.HTML+CSS补充 0.常用页面布局 <!DOCTYPE html> <html lang="en"><head> <meta ch ...

  3. Python的平凡之路(12)

    一.数据库介绍 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我们也可以将数据存储在文件中,但 ...

  4. Python的平凡之路(11)

    一. rabbitmq 1 进程Queue:  父进程与子进程进行交互,或者同属于同一父进程下多个子进程进行交互 2 队列通信:   send1.py #!/usr/bin/env python#Au ...

  5. Python的平凡之路(9)

    一.Paramiko模块练习 1. Paramiko模块介绍 Paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接   2 .SSHclie ...

  6. Python的平凡之路(5)

    一.模块介绍 定义: 模块--用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名test.py,模块名test) 包—用来从逻辑上组织 ...

  7. Python的平凡之路(20)

    (提问复习为主) 一.Django请求的生命周期      武彦涛:           路由系统 -> 视图函数(获取模板+数据=>渲染) -> 字符串返回给用户     二.路由 ...

  8. Python的平凡之路(19)

    一.Django请求生命周期   对于所有的web框架来说本质就是一个socket服务端,浏览器是socket客户端                                          ...

  9. Python的平凡之路(18)

    一.JS 正则部分 test   - 判断字符串是否符合规定的正则rep = /\d+/;rep.test("asdfoiklfasdf89asdfasdf")# truerep ...

随机推荐

  1. js 判断 是否位数字

    isNaN(val) val为数字返回false 不为数字返回truedocument.write(isNaN(123)); document.write(isNaN(-1.23)); documen ...

  2. 阿里云docker

    https://cr.console.aliyun.com docker 安装mysql image sudo docker pull  mysql docker  运行 mysql docker r ...

  3. 无线OSS-高精度整数加法

    #include<iostream> #include<string> using namespace std; int compareStr(string str1, str ...

  4. xml和json的区别

    本文转自SanMaoSpace的博客 链接地址如下:http://www.cnblogs.com/SanMaoSpace/p/3139186.html 1.定义介绍 (1).XML定义扩展标记语言 ( ...

  5. java中使用sql的like关键字

    String sql = "select * from userinfo where nickname like ?"; PreparedStatement ps = conn.p ...

  6. DDOS分布式拒绝服务

    DDOS(分布式拒绝服务)概念 DDOS称为分布式拒绝服务,DDOS本是利用合理的请求伪造资源过载,导致服务不可用.比如一个停车场有100个停车位,当100个停车位都停满后,再有车想要进来.就必须要等 ...

  7. 深入浅出Mybatis系列(五)---TypeHandler简介及配置(mybatis源码篇)

    上篇文章<深入浅出Mybatis系列(四)---配置详解之typeAliases别名(mybatis源码篇)>为大家介绍了mybatis中别名的使用,以及其源码.本篇将为大家介绍TypeH ...

  8. Spring整合HBase

    Spring整合HBase Spring HBase SHDP § 系统环境 § 配置HBase运行环境 § 配置Hadoop § 配置HBase § 启动Hadoop和HBase § 创建Maven ...

  9. 【小月博客】 Html5 上传图片 移动端、PC端通用

    在博客园注册账号有些天了,感觉有些许欣慰,自己写的东西有人在看,有人在评论很是开心.(ps: 满足一下虚荣心吧!) 废话不多说了,说一下今天给大家分享的是 html5上传图片.我们是在移动端使用的,但 ...

  10. VS 2012 No exports were found that match the constraint 解决办法

    VS 2012 No exports were found that match the constraint 解决办法 删除C:\Users\你的用户名\AppData\Local\Microsof ...