#在/var/pythonjiaoben/目录下创建ip_list文件
vim iplist.txt
1.1.1.1
2.2.2.2

# 主python脚本创建在/var/pythonjiaoben/目录下
vim xunjian.py

import pexpect
import sys
import time
import os
import datetime

now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
os.makedirs("/var/pythonjiaoben/sw_config/%s"%now_time) #按照时间来命名文件夹的名字

#读取外面iplist的地址:
file = []
f = open("/var/pythonjiaoben/iplist.txt")
iplist=f.readlines()
for i in iplist:
file.append((i.strip("\n")))

def h3c_check():
for i in file:
print(i)
child = pexpect.spawn('ssh hostname@%s' %i)
fout = open('/var/pythonjiaoben/sw_config/%s/%s.txt' %(now_time,i), 'wb+')
child.logfile = fout
child.expect('password: ')
child.sendline("host_password")
child.expect(">")
child.sendline("dis version")
child.expect(">")
child.sendline("dis cu")
while True:
try:
index = child.expect("---- More ----")
child.send(" ")
except Exception as e:
child.sendline("qu")
break
time.sleep(2)
h3c_check()

#通过bash执行.py
vim backup1.sh
cd /var/pythonjiaoben
python3 /var/pythonjiaoben/xunjian.py

chmod 700 /var/pythonjiaoben/backup1.sh # 赋予backup1.sh的权限

32 9 * /var/pythonjiaoben/backup1.sh # 每天早上9点32分执行一次

          • /var/pythonjiaoben/backup1.sh # 用空格间隔开的5个*,每隔一分钟执行

tail -f /var/log/cron #查看自动任务日志