一、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. PHP Fatal Error: call to undefined function mysql_connect() [duplicate]

    You shouldn't use mysql_* functions to start with. They are deprecated as of PHP 5.5. Use mysqli or ...

  2. testlink部署与迁移

    几个特殊的文件: 1.D:\xampp\htdocs\testlink\config.inc  安装配置文件,此处需要修改安装目录(g_repositoryPath.log_path) 2.D:\xa ...

  3. (44) odoo中的WebService

    * 前言   erp系统会和其它系统进行对接,这时就要接口,官方给出的是两解决方案   * XML-RPCLibrary  举例    import xmlrpclib root = 'http:// ...

  4. JAVA语法02之课程问题解决

    (一)示例程序+运行结果: ①EnumTest.java public class EnumTest { public static void main(String[] args) { Size s ...

  5. 使用CXF发布WebService

    这里普及一下WebService和cxf的知识.关于webservice和cxf:   WebService.各种提供服务的组件     .企业总线.通讯总线(ESB)CXF:是一个SOA框架,Axi ...

  6. 学习PYTHON之路, DAY 3 - PYTHON 基础 3 (函数)

    一 set 集合 (无序且不重复的元素集合) 基本操作: t.add('x') # 添加一项 s.update([10,37,42]) # 在s中添加多项 删除一项: t.discard('H') t ...

  7. java系统高并发解决方案-转

    转载博客地址:http://blog.csdn.net/zxl333/article/details/8685157 一个小型的网站,比如个人网站,可以使用最简单的html静态页面就实现了,配合一些图 ...

  8. ThinkPHP 3.2.3(三)架构之URL模式

    一.标准URL格式 http://serverName/index.php/模块/控制器/操作   二.URL大小写 在/ThinkPHP/Conf/convention.php文件里有URL大小写的 ...

  9. javascript和jquey的自定义事件小结

    “通过事件机制,可以将类设计为独立的模块,通过事件对外通信,提高了程序的开发效率.” 可以把多个关联但逻辑复杂的操作利用自定义事件的机制灵活地控制好 对象之间通过直接方法调用来交互 1)对象A直接调用 ...

  10. F2工作流引擎参与者类型成员的交、并、互拆计算规则

          计算描述:计算规则指的是和其它“参与者类型成员”的之间的计算,必须求解处理人不为空的情况下才进行规则计算,各个“参与者类型成员”按序号顺序执行. 计算算法:并集(权重最低),交集(权重中) ...