©著作权归作者所有:来自51CTO博客作者黑色时间的原创作品,如需转载,请注明出处,否则将追究法律责任
https://blog.51cto.com/blacktime/1795737
生产环境中,需求为关机,重启和修改ip地址,用Python脚本实现,脚本命名为
systemControl.py
。
#coding = utf8
Created on 2016-7-4
@desc: change the network of system and reboot,shutdown ;suit for ubuntu and centos
@attention: if ubuntu, need root or sudo without password ;and before use this script ,
you need change something; eg:
chown root:root systemControl.py
chmod 777 systemControl.py
chmod 777 /etc/network/interfaces
import sys
import os
import platform
import commands
class systemControl(object):
def __init__(self,command,ip,gateway,netmask,device):
@desc: init
self.platform = self.getPlatform()
self.command = command
self.ip = ip
self.gateway = gateway
self.netmask = netmask
self.device = device
def shutDown(self):
@attention: shutdown the system
os.system("sudo init 0")
def reboot(self):
@attention: reboot the system
os.system("sudo init 6")
def changeNetwork(self):
@attention: change the network of the system
if self.platform == "centos":
path = "/etc/sysconfig/network-scripts/ifcfg-"+str(self.device)
file_handler = open(path,"r")
network_content = file_handler.read()
file_handler.close()
conte = "IPADDR=%s\nNETMASK=%s\nGATEWAY=%s\n" %(self.ip,self.netmask,self.gateway)
num = network_content.find("IPADDR")
if num != -1:
network_content = network_content[:num]+conte
file_handler = open(path,"w")
file_handler.write(network_content)
file_handler.close()
elif self.platform == "ubuntu":
path = "/etc/network/interfaces"
file_handler = open(path,"r")
network_interfaces = file_handler.read()
file_handler.close()
network_content = network_interfaces.split("auto")
for i in network_content:
if self.device in i:
content = "auto %s\niface %s inet static\n\taddress %s\n\tnetmask %s\n\tgateway %s\n\t" %(self.device,self.device,self.ip,self.netmask,self.gateway)
network_interfaces = network_interfaces.replace(i,content)
break
file_handler = open(path,"w")
file_handler.write(network_interfaces)
file_handler.close()
result = commands.getoutput("sudo ifdown %s && sudo ifup %s" %(self.device,self.device))
print result
def delWith(self):
if self.command == "reboot":
self.reboot()
elif self.command == "shutdown":
self.shutDown()
elif self.command == "changeNetwork":
self.changeNetwork()
else:
print "no this command......"
def getPlatform(self):
@attention: get the platform of the system
platForm = platform.platform().lower()
if "ubuntu" in platForm:
print platForm
return "ubuntu"
elif "centos" in platForm:
print platForm
return "centos"
except:
print "unknow os..."
return None
if __name__ == "__main__":
@param: <shutdown/reboot>
<changeNetwork>,<ip>,<gateway>,<netmask>,<device>
command = sys.argv[1]
ip = sys.argv[2]
gateway = sys.argv[3]
netmask = sys.argv[4]
device = sys.argv[5]
except:
ip = None
gateway = None
netmask = None
device = None
doSomething = systemControl(command,ip,gateway,netmask,device)
doSomething.delWith()
目前只支持Ubuntu和centos,其他操作系统上没测试,应该也行。
若要是Ubuntu,需要修改sudo免密状态:
sudo vim /etc/sudoers.d/nopasswd4sudo
添加内容重启:
tony ALL=(ALL) NOPASSWD : ALL
修改文件权限:
chmod 777 /etc/network/interfaces
使用说明:
python systemControl.py shutdown #关机
python systemControl.py reboot #重启
python systemControl.py changeNetwork ip gateway netmask device #修改ip
Linux
系统安装完成以后,就要学会一些
Linux
的基本
操作
命令,首先是
关机
和
重启
命令,如果进入
Linux
系统之后连
关机
重启
都不会是不是太不应该了,总不能
关机
或
重启
的时候直接关闭电源在开机吧
Linux
的
关机
和
重启
1、shutdown 2、poweroff 3、init 4、reboot 5、halt具体用法: 1.shutdown shutdown命令安全地将系统
关机
1.shutdown
shutdown命令安全地将系统
关机
。 有些用户会使用直接断掉电源的方式来关闭
linux
,这是十分危险的。
shutdown命令shutdown [选项] 时间-c:取消前一个
关机
命令-h:
关机
-r:
重启
[root@localhost ~]# date2017年 06月 21日 星期三 15:48:33 CST
重启
命令:1、reboot2、shutdown -r now 立刻
重启
(root用户使用)3、shutdown -r 10 过10分钟自动
重启
(root用户使用)4、shutdown -r 20:35
在
Linux
下
关机
和
重启
的命令有shutdown、reboot、halt、poweroff、init。下面我们来了解下这些命令如何使用。
修改
IP
地址
和
网关是很常见的
操作
,在做相关实验的时候,如果没有设置好,会带来很多不必要的麻烦。1.
以前都是使用自动
IP
动态分配获取
IP
的,虽然每次获得的
ip
都是相同的,但我还是决定自己设置一个
IP
。
修改
网卡文件:vim /etc/sysconfig/network-scripts/ifcfg-eth0 , 内容如下
DEVICE=eth0
ONBOOT=yes  
vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
HWADDR=XX:XX:XX:XX:XX:XX
ONBOOT=yes
DHCP_HOSTNAME=X
IPADDR=192.168.1.2
NETMASK=255.255.255.0
GATWAY...
LINUX
修改
IP
地址
以前都是使用自动
IP
动态分配获取
IP
的,虽然每次获得的
ip
都是相同的,但我还是决定自己设置一个
IP
。
以前都是使用自动
IP
动态分配获取
IP
的,虽然每次获得的
ip
都是相同的,但我还是决定自己设置一个
IP
。
关机
(系统的
关机
、
重启
以及登出 ) shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown
二、知识点详解
在
linux
领域内大多用在服务器上,很少遇到
关机
的
操作
。毕竟服务器上跑一个服务是永无止...
一、启动高级电源管理支持 无法自动
关机
是因为电源的设置不当所引起的,而在所有的设置之中没有启动高级电源管理支持是一件非常普遍的事情。
LINUX
--
修改
IP
地址
A、
修改
ip
地址
即时生效:# ifconfig eth0 192.168.0.20 netmask 255.255.255.0启动生效:
修改
/etc/sysconfig/network-scripts
A、
修改
ip
地址
即时生效: # ifconfig eth0 192.168.0.20 netmask 255.255.255.0 启动生效:
修改
/etc/sysconfig/network-scripts
用ifconfig查看
IP
设置情况
dns# ifconfigle0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric
学习知识首先要从基础学起,不如说现在很多人在学习
Linux
操作
系统,学习
Linux
就要先了解一些
Linux
命令
HarmonyOS(鸿蒙)技术社区是由51CTO和华为共同打造的综合性开发和应用技术社区。作为华为的官方战略合作伙伴,51CTO将多年的社区运营经验与华为的技术赋能相结合,为开发者提供高质量有深度的HarmonyOS(鸿蒙)学习交流平台。