python修改电脑名称_python 脚本之批量更改主机名
今天接到上面工作任務,讓更改多臺主機的主機名稱。使用python paramiko Module,隨手寫了一個批量修改主機名的腳本。
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import paramiko
HOSTNAME='root'
PORT=22
PASSWD='123456'
NUM=range(139, 144)
def changNetwork():
""" change /etc/sysconfig/network HOSTNAME. """
name = 1
for i in NUM:
NM = "dcache-ct3-node%s.et.letv.com" % name
HOST = "10.180.91.%s" % i
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(HOST, port=PORT, username=HOSTNAME, password=PASSWD)
ssh.exec_command('sed -i "s/HOSTNAME=.*/HOSTNAME=%s/" /etc/sysconfig/network' % NM)
ssh.close()
name +=1
def changeHostName():
""" change hostname. """
name = 1
for i in NUM:
NM = "dcache-ct3-node%s.et.letv.com" % name
HOST = "10.180.91.%s" % i
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(HOST, port=PORT, username=HOSTNAME, password=PASSWD)
ssh.exec_command('hostname %s ' % NM)
ssh.close()
name +=1
def showNetwork():
""" show /etc/sysconfig/network HOSTNAME. """
name = 1
for i in NUM:
HOST = "10.180.91.%s" % i
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(HOST, port=PORT, username=HOSTNAME, password=PASSWD)
stdin, stdout, stderr = ssh.exec_command('cat /etc/sysconfig/network | grep "^HOSTNAME="')
for i in stdout:
print i
print HOST
ssh.close()
#print "dcache-zb6-node%s.et.letv.com" % name
#name +=1
def showHostName():
""" show hostname. """
name = 1
for i in NUM:
HOST = "10.180.91.%s" % i
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(HOST, port=PORT, username=HOSTNAME, password=PASSWD)
stdin, stdout, stderr = ssh.exec_command('hostname')
for i in stdout:
print i
print HOST
ssh.close()
if __name__ == "__main__":
changNetwork()
changeHostName()
showNetwork()
showHostName()
總結
以上是生活随笔為你收集整理的python修改电脑名称_python 脚本之批量更改主机名的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python后端服务器_毕设里配合Pyt
- 下一篇: kafka 启动_「首席看Event H