公司有centos7.2和centos7.7系統百十臺,因辦公軟體的需要,要把所有的系統升級到centos7.9,百度了一下,還是覺得配置本地yum源比較合適,然後就是批量升級的操作了,別人建議使用ansible,可我沒學會,算了就使用py寫寫吧,步驟如下:
使用py批量登陸升級,因為root密碼都一樣,伺服器端可以聯網,客戶端皆不可以聯網
192.168.137.101 192.168.160.131 yum源伺服器,ftp192.168.160.129 客戶端,原來的系統centos7.2 3.10.0-327.el7.x86_64192.168.160.133 客戶端,原來的系統centos7.7 3.10.0-1062.el7.x86_64CentOS-7-x86_64-DVD-2009.iso 掛載
可以使用CentOS-7-x86_64-Everything-2009.iso
然後複製裡面的內容到本地目錄
創建目錄mkdir -p /data/CentOS7.9
mount /dev/cdrom /mnt (因為是vmware 直接掛載的,所以才這麼操作,如果你是直接複製iso到伺服器上的話,那你需要執行mount -t iso9660 -o loop /root/CentOS-7-x86_64-DVD-2009.iso /mnt)
複製光碟內容到指定的目錄內cp -R /mnt/* /data/CentOS7.9/
修改CentOS-Media.repo文件
cd /etc/yum.repos.d修改vi CentOS-Media.repo 內容如下
[c7.9-media]name=CentOS-$releasever - Mediabaseurl=file:///data/CentOS7.9/gpgcheck=0enabled=1gpgkey=file:///data/CentOS7.9/RPM-GPG-KEY-CentOS-7
清空yum緩存目錄yum clean all
建立yum元數據緩存yum makecache
查看本機yum repo 倉庫yum repolist然後安裝vsftp服務
安裝vsftpyum -y install vsftpd ftp
修改ftp配置文件 添加vi /etc/vsftpd/vsftpd.confanon_root=/data/CentOS7.9
啟動ftp systemctl start vsftpd systemctl enable vsftpd
測試ftp服務可用性 Trying ::1... Connected to localhost (::1). 220 (vsFTPd 3.0.2) Name (localhost:root): anonymous 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> ls然後客戶端測試
客戶端配置本地yum源(ftp方式)配置yum源cd /etc/yum.repos.d/mkdir bakmv *.repo bak
編輯local.repo文件 tee CentOS-Local.repo << EOF[base]name=CentOS-Localbaseurl=ftp://192.168.160.131 gpgcheck=0enabled=1EOF
更新yum設置yum clean allyum makecacheyum repolist
測試配置yum -y install net-tools然後就是py的命令了:
from tkinter import *from tkinter.messagebox import showinfofrom tkinter.ttk import *import paramikoimport time
class Xmlgrg(Tk): def __init__(self): super().__init__()
self.title("update") self.geometry("1200x600+400+100") self.resizable(0,0) self["bg"] = "lightblue"
self.setup_UI()
def setup_UI(self): self.style01 = Style() self.style01.configure("TLabel",font=("微軟雅黑",10,"bold"),background="lightblue") self.style01.configure("TEntry",foregroud="navy") self.style01.configure("TButton",font=("微軟雅黑",10,"bold"),background="lightblue")
self.Label_FW = Label(self,text="伺服器名:") self.Label_FW.place(x=20,y = 20) self.Entry_FW = Entry(self,width=120,font=("微軟雅黑",10,"bold")) self.Entry_FW.place(x=90,y = 20) self.Entry_FW.insert(0,"多個主機之間使用 英文符號 , 隔開") self.Button_Status_Cpu_nu = Button(self, text="update7.9", width=10, command=self.SSHupdate) self.Button_Status_Cpu_nu.place(x=50, y=50) scrollBar = Scrollbar(self)
self.text = Text(self, width=155, height=33, yscrollcommand=scrollBar.set) self.text.place(x=20, y=100)
self.text.tag_config('tag1', background='yellow', foreground='red') self.text.tag_config('tag2', background='blue', foreground='red') self.text.tag_config('tag3', background='gray', foreground='red')
scrollBar.pack(side=RIGHT, fill=Y)
scrollBar.config(command=self.text.yview) def SSHupdate(self):
temp_name = str(self.Entry_FW.get()) temp_list = temp_name.split(",") self.text.delete(0.0, END)
for temp_num in range(0, len(temp_list)):
var_FWN = str(temp_list[temp_num])
self.command = "cd /etc/yum.repos.d/ && mkdir bak ; mv *.repo bak ; touch CentOS-Local.repo && echo \"[base]\" >> CentOS-Local.repo && echo \"name=CentOS-Local\" >> CentOS-Local.repo && echo \"baseurl=ftp://192.168.160.131 \" >> CentOS-Local.repo && echo \"gpgcheck=0\" >> CentOS-Local.repo && echo \"enabled=1\" >> CentOS-Local.repo && yum clean all && yum makecache && yum update -y && yum update kernel -y && sudo init 6 "
try: ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(hostname=var_FWN, port=22, username="root", password="Xmlgrgnqt...") except Exception as e: Ex_host = "############# " + var_FWN + " 連接異常 ###########\n" self.text.insert(INSERT, Ex_host, 'tag2') pass
else: stdin, stdout, stderr = ssh_client.exec_command(self.command)
ZHxx = "############# " + var_FWN + " update 已經在後臺運行 ###########\n"
self.text.insert(INSERT, ZHxx, 'tag1')
finally: ssh_client.close() class LoginWindow(Tk): """ 創建登錄窗體的GUI界面以及登錄的方法 """ def __init__(self): super().__init__() self.title("登錄界面") self.geometry("564x174+520+400") self.resizable(0 ,0)
self["bg"] = "lightblue"
self.setup_UI()
self.user = "" self.password = ""
def setup_UI(self):
self.Style01 = Style() self.Style01.configure("user.TLabel", font=("微軟雅黑", 14 ,"bold"), foreground="red" ,background="lightblue") self.Style01.configure("TButton", font=("微軟雅黑", 13, "bold"))
self.Label_user = Label(self ,text="用戶名:" ,style="user.TLabel").place(x=10 ,y=80) self.var_user = StringVar() self.Entry_user = Entry(self ,width=10, textvariable=self.var_user ,font=("微軟雅黑", 14, "bold")).place(x=90 ,y=77) self.Label_password = Label(self, text="密碼:" ,style="user.TLabel").place(x=230 ,y=80) self.var_password = StringVar() self.Entry_password = Entry(self, width=10, textvariable=self.var_password, show="●" ,font=("微軟雅黑", 14, "bold")).place(x=295 ,y=77)
self.Button_login = Button(self, text = "登錄" ,width=9 ,command = self.login).place(x=450 ,y=77)
def login(self): self.user = self.var_user.get() self.password = self.var_password.get()
if self.user == 'admin' and self.password == 'Xmlgrgnqt': self.load_main()
else : showinfo("系統消息", "輸入的用戶名或者密碼錯誤")
def load_main(self): self.destroy() main_window = Xmlgrg()
if __name__ == "__main__": this_login = LoginWindow() this_login.mainloop()