#!/usr/bin/env python
import re
import sys
import subprocess
#定義配置ip地址函數(shù)
def configip(fname,ip_addr,if_ind):
    content = '''TYPE=Ethernet
BOOTPROTO=none      
NAME=eth%s
DEVICE=eth%s
ONBOOT=yes
IPADDR=%s
PREFIX=24
''' % (if_ind,if_ind,ip_addr)
   with open(fname,'w') as fobj:
        fobj.write(content)
#定義檢測(cè)ip地址是否合法函數(shù)
def check_ip(ip_addr):     
    m = re.match(r'(\d{1,3}\.){3}\d{1,3}$',ip_addr)    
    if not m:
        return False
    return True
#定義配置主機(jī)名函數(shù)
def config_hostname():
    fhostname = '/etc/hostname'
    hostname = raw_input('Input You Hostname: ')
   with open(fhostname,'w') as fobj:
        fobj.write(hostname)
    subprocess.Popen('hostname %s ' % hostname,shell=True)
#定義顯示菜單函數(shù)
def show_menu():
    prompt = '''Configure IP Address:
(0)eth0
(1)eth2
(2)eth3
(3)eth4
Your choice(0/1/2/3):'''
    try:
        if_ind = raw_input(prompt).strip()[0]
    except:
        print 'Invalid raw_input.'
        sys.exit(1)
    if if_ind not in '0123':
        print 'Wrong Selection. Use 0/1/2/3/'
        sys.exit(2)
    fname = '/etc/sysconfig/network-scripts/ifcfg-eth%s' % if_ind
    ip_addr = raw_input('ip address: ').strip()
    result = check_ip(ip_addr)
    if not result:
        print 'Invalid ip address!'
        sys.exit(3)
    configip(fname,ip_addr,if_ind)
    subprocess.Popen('systemctl restart network',shell=True)
    print '\033[32;1mConfigure ip address done. \033[0m'
if __name__ == '__main__':
    main_menu = '''(0)config_hostname
(1)config_ipaddr
Your choice(0/1)'''
    try:
        ind = raw_input(main_menu).strip()[0]
    except:
        print 'Invalid raw_input.'
        sys.exit(4)
    if ind not in '01':
        print 'Wrong Selection. Use 0/1'
        sys.exit(5)
    if ind == '0':
        config_hostname()
    show_menu()
備注:
1.  通過指定方式的辦法來獲取地址,如果未指定的化可能會(huì)出現(xiàn)問題,有三種模式:static 靜態(tài)ip;dhcp 動(dòng)態(tài)ip;none 無(不指定)。
2.  m = re.match(r'(\d{1,3}\.){3}\d{1,3}$',ip_addr)    # r 表示 raw string(原始字符串常量),用于規(guī)避反斜杠的轉(zhuǎn)義。
3.  python2 中 raw_input() 可以接收字符串、中文;input()只接收數(shù)字。python3 中無 raw_input() 只有input(),接收字符串、數(shù)字、中文,相當(dāng)于python2中raw_input()和input()的結(jié)合。
for i in range(1 ,10):
    for j in range(1 ,i+1):
        print('%sx%s=%s ' % (j, i, j*i),end='')
    print()import sys
import getpass
import paramiko
import threading
import os
def remote_com(host,pwd,command):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=host,username='root',password=pwd)
    stdin,stdout,stderr = ssh.exec_command(command)
    out = stdout.read()
    error = stderr.read()
    if out:
        print('[%s] OUT:\n%s' % (host,out.decode('utf8')))
    if error:
        print('[%s] ERROR:\n%s' % (host,error.decode('utf8')))
    ssh.close()
if __name__ == '__main__':
    if len(sys.argv) != 3:
        print('Usage: %s ipaddr_file "command"' % sys.argv[0])
        exit(1)
    if not os.path.isfile(sys.argv[1]):
        print('No such file:', sys.argv[1])
        exit(2)
    fname = sys.argv[1]
    command = sys.argv[2]
    pwd = getpass.getpass()
   with open(fname) as fobj:
        ips = [line.strip() for line in fobj]
    for ip in ips:
        t = threading.Thread(target=remote_com,args=(ip,pwd,command))
        t.start()
備注:
1. 執(zhí)行方式< python3  腳本名稱  遠(yuǎn)程服務(wù)器地址文件  "需要的執(zhí)行命令" >另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

                文章標(biāo)題:python實(shí)例-創(chuàng)新互聯(lián)
                
                標(biāo)題來源:http://www.chinadenli.net/article16/igsgg.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、品牌網(wǎng)站制作、網(wǎng)站策劃、網(wǎng)站改版、網(wǎng)站內(nèi)鏈、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
