- 论坛徽章:
- 0
|
1.你是如何学习Python的?看书?看视频?还是?
刚开始看书《python核心编程》,基本语法看完后,尝试将之前的shell脚本用python来实现,然后特别学习常用的模块,MysqlDB,paramiko,threading,WMI等等,之后基本上一个有个需求基本上都能有个大致的python解决方案了,碰到问题再查资料,学会,积累。
2.你工作中有用到Python吗?主要用来做什么?
现在工作中python的使用率比重还是比较高的,日志分析,自动化工具,监控脚本等等。
3.你觉得Python跟其他脚本语言比优势在哪?
跨平台,windows,linux都能用的上,python抽象出来的模块很容易学习,不用关注底层的实现,仅运维工作中,完全够用了。
4.谈谈Python在自动化运维中的应用。
目前有很多运维工具都是用python写成的,比如saltsack,ansible,已经证明了python在自动化运维中的实用以及灵活,如果要开发自己业务适用的自动化运维平台,python完全可以满足,就个人而言,python是首选。
5.给出一个你自己写的Python脚本(最好有详细注释)。
#!/usr/bin/env python
import sys
import time
import random
import cmd
import paramiko
import threading
import ConfigParser
#加载配置文件
config_file=ConfigParser.SafeConfigParser()
config_file.read('/tmp/sixther/config.ini')
#指定服务器私钥的路径,用初始化paramiko
pkey_path='/home/sixther/.ssh/id_rsa'
s=paramiko.SSHClient()
s.load_system_host_keys()
yuenan_iplist={'CCS':'1.1.1.1'}
malai_iplist={'CCS':'2.2.2.2'}
taiwan_iplist={'CCS':'3.3.3.3'}
xianggang_iplist={'CCS':'4.4.4.4'}
beimei_iplist={'CCS':'5.5.5.5'}
taiguo_iplist={'CCS':'6.6.6.6'}
#定义并发函数
def multi_thread(main_proc,slave_proc):
multi_thread=[]
main=threading.Thread(target=main_proc)
main.setDaemon(True)
multi_thread.append(main)
slave=threading.Thread(target=slave_proc)
multi_thread.append(slave)
for i in range(2):
multi_thread[i].start()
multi_thread[1].join()
multi_thread[0].join()
print "\x1b[0;31m%s\x1b[0m" % ('command compelete~')
#初始化cmd一级目录
class First(cmd.Cmd):
prompt='[Please choose the area]>>'
def do_yuenan(self,line):
second_loop(yuenan_iplist,'YueNan')
def do_malai(self,line):
second_loop(malai_iplist,'MaLai')
def do_taiwan(self,line):
second_loop(taiwan_iplist,'TaiWan')
def do_xianggang(self,line):
second_loop(xianggang_iplist,'XiangGang')
def do_beimei(self,line):
second_loop(beimei_iplist,'BeiMei')
def do_taiguo(self,line):
second_loop(taiguo_iplist,'TaiGuo')
def do_EOF(self):
return True
def emptyline(self):
print
#初始化cmd二级目录
class Second(cmd.Cmd):
def __init__(self,area):
cmd.Cmd.__init__(self)
cmd.Cmd.prompt='['+area+']>>'
def do_choose_area(self,line):
First().cmdloop()
def do_shell(self,line):
stdin,stdout,stderr=s.exec_command(line)
print "\n\x1b[0;32m%s\x1b[0m" % (stdout.read())
print "\n\x1b[0;35m%s\x1b[0m" % (stderr.read())
def emptyline(self):
print
def second_loop(iplist,area_name):
s.connect(hostname=iplist['CCS'],username='dagtlbb',key_filename=pkey_path)
command=[]
script=[]
for i in config_file.sections():
command.append(config_file.get(i,'command'))
script.append(config_file.get(i,'script'))
exec_command_func=[lambda self,line,i=i:exec_script(i) for i in script]
for i in range(len(command)):
setattr(Second,'do_'+command[i],exec_command_func[i])
Second(area_name).cmdloop()
#定义远程执行命令函数
def exec_script(script_name):
stdin,stdout,stderr=s.exec_command('/home/dagtlbb/admin/test/'+script_name)
print "\n\x1b[0;32m%s\x1b[0m" % (stdout.read())
print "\n\x1b[0;35m%s\x1b[0m" % (stderr.read())
#随机打印小故事函数,用于命令执行太慢的时候解乏用。
def show_story(self):
story_df=open('/tmp/duanchao/story.txt','r')
random_num=random.randint(1,4)
for i in story_df:
if i.startswith(str(random_num)):
for j in range(len(i)):
if j%50 == 0:
print "\n"
sys.stdout.write(i[j])
sys.stdout.flush()
if __name__ == '__main__':
First().cmdloop()
|
|