python运维自动化脚本案例,python自动化运维工具

欢迎来到今天的讨论,我们将探讨,python自动化运维需要掌握的技能 python自动化运维快速入门 pdf,让我们开始吧!

习题26:恭喜你,现在可以考试了!

笔记:

优秀的科学家会对他们自己的工作持怀疑态度,同样,优秀的程序员也会认为自己的代码总有出错的可能Python的进阶教程。因此需要去排查检验所有可能出错的地方。今天的任务就是修改一份可能有错的代码,看自己是否能掌握之前的一些基础知识。总结一些常见错误:少括号、少冒号、代码单词拼写错误、符号拼写错误等常见的细小错误。注意:python3之前输出print可不用加括号;python3之后要求加上括号。

错误代码来源:点此

修改后:

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = (0)
    print(word)

def print_last_word(words):
    """Prints the last word after popping it off."""
    word = (-1)
    print(word)

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)


print ("Let's practice everything.")#加了括号
print ('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.')

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""

print("--------------")
print(poem)
print("--------------")

six = 10 - 2 + 3 - 5
print("This should be six: %s" % six)

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates
    
start_point = 10000

jelly_beans, jars, crates = secret_formula(start_point)

print("With a starting point of: %d" % start_point)
print("We'd have %d jeans, %d jars, and %d crates." % (jelly_beans, jars, crates))

start_point = start_point / 10

print("We can also do that this way:")
print("We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point))

#与python脚本交互
import test25
sentence = "All good things come to those who weight."

words = test25.break_words(sentence)
sorted_words = test25.sort_words(words)

print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = test25.sort_sentence(sentence)
print(sorted_words)

print_first_and_last(sentence)

print_first_and_last_sorted(sentence)

结果:

习题27:记住逻辑关系

笔记:

万事开头难!一些重要的概念还是得记住,每天花一小部分时间记一些知识点。逻辑术语:计算机的逻辑就是在程序的某个位置检查这些字符或者变量组合在一起表达的结果是真是假。逻辑术语,记了八百遍了,再复习一下吧。下来就是真值表其实蛮好记得,逻辑清楚即可。

习题28:布尔表达式练习

练习: 结果为False

3 != 4 and not ("testing" != "test" or "Python" == "Python")
//True and not(True or True)
//True and False
//False

习题29:如果(if)

练习1:

people = 30
cars = 40
buses = 15

if(cars > people):
	print("cars")
elif(cars < people):
	print("people")
elif(buses > people):
	print("people")
else:
	print("no decision")

练习2: 通过使用语句判断数字是正数、负数或零,也可使用if内嵌语句。

#用户输入数字
num = float(input("输入一个数字:"))
if num > 0:
	print("正数")
elif num == 0:
	print("零")
else:
	print("负数")

#if内嵌语句
num = float(input("输入一个数字:"))
if num >= 0:#大于等于两个情况
	if(num == 0):
		print("零")
	else:
		print("正数")
else:#否则就是小于零了
	print("负数")

结果测试:

习题30:else和if

笔记:

if对于下一行代码所做的就是if表达式为真才会进入if内部的功能操作,否则就跳过这一段if语句的缩进其实就是表示缩进的这几行是属于if表达式成立时要执行的语句;行尾的冒号作用是告诉python接下来你要创建一个新的代码区段。
原文地址1:https://blog.youkuaiyun.com/weixin_43136158/article/details/105925096
参考资料:python中用turtle画一个圆形 https://blog.youkuaiyun.com/SXIAOYAN_/article/details/140061099

Python 是一种开源的高级编程语言,在网络工程、运维自动化脚本开发等领域有着广泛的应用。以下是一个 Python 网工运维自动化脚本案例。 假设我们要完成一个自动化批量管理服务器的脚本。我们首先需要使用 Python 的 Paramiko 模块来建立与服务器的 SSH 连接,通过 SSH 协议进行远程管理。然后,我们可以编写一个函数,用于执行远程命令,例如重启服务器、查看系统信息等。 利用这个函数,我们可以编写一个批量执行命令的函数,它可以从一个配置文件中读取服务器的信息,然后依次连接到每个服务器,并执行相同的命令。这样就实现了批量管理服务器的功能。 另外,我们可以编写一个函数,来分析服务器的日志文件。我们可以使用 Python 的 re 模块来进行正则表达式的匹配,从日志文件中提取关键信息,例如错误日志、访问次数等。然后,我们可以进一步处理这些提取出来的信息,如生成报表、发送警报等。 此外,我们还可以使用 Python 的 requests 模块进行网络请求,例如发送 HTTP 请求来检查网站的可用性、获取网页内容等。我们可以编写一个自动化监控脚本,定期执行这些请求并分析返回的结果,如果发现异常就发送警报。 综上所述,Python 网工运维自动化脚本充分利用了 Python 强大的网络处理、文本处理和自动化编程能力,可以提高运维效率,减少重复工作,帮助管理员更好地管理和监控服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值