#-*- coding:utf-8 -*-

__authour__='CC'

from Tkinter import *
import os
import telnetlib
import time
import shutil
import socket
import paramiko
import glob
import re
import threading

def thread_it(func, *args):
'''将函数打包进线程'''
# 创建
t = threading.Thread(target=func, args=args)
# 守护 !!!
t.setDaemon(True)
# 启动
t.start()

def get_version_path(n1,n2,n3,versionNUM):
if(int(n2)==0 and int(n1)==5):
n11=n1
print('Upgrade version is v5.0 version')
if(int(n2)==1):
n11=n1 + '.' + n2
print('Upgrade version is v5.1 version')
if(int(n2)==5):
n11=n1 + '.' + n2
print('Upgrade version is v5.5 version')
if(int(n1)==6 and int(n2)==0):
n11=n1 + '.' + n2
print('Upgrade version is v6.0 version')
#判断是Temp version or official version
if int(n3)>500:
server_path_tp1 ='\\\\172.18.104.44\\R&D TN China\\R&D_Server\\Version Management\\Dev_Version\\TempVersion\\NPTI\\' + 'V' + n11 + '\\' + versionNUM
print 'version is a temp version'
else:
server_path_tp1 ='\\\\172.18.104.44\\R&D TN China\\R&D_Server\\Version Management\\Dev_Version\\Version to V&V\\NPTI\\' + 'V' + n11 + '\\' +'V' + versionNUM
print 'version is official vesion'
#print server_path_tp1
server_path_tp2=server_path_tp1+'*'

#通过通配符函数,通配名字"NPT/NPT-1800/V5.1/versionNUM*的路径。
#print server_path_tp2
server_path_tp3=glob.glob(server_path_tp2)
server_path_tp4=server_path_tp3[0]
server_path=str(server_path_tp4)

#print server_path

print ('Version path in server:%s' %server_path)
#print os.listdir(server_path)
#print os.getcwd()
#判断’D:\\version\\NPT\\1800‘是否存在
path='D:\\version\\NPT\\1800'
isExists=os.path.exists(path)
if not isExists:
os.makedirs(r'D:\\version\\NPT\\1800')
return server_path



def sftp_get_version(version_type,server_path,hostname,n1,n2,n3,sha):
os.chdir('D:\\version\\NPT\\1800')
s=n1+n2+n3
#print ('version_type is %d' %version_type)
if version_type==1 or 3:
source_file=server_path + '\\' +'NPT1800_Emb_'+ s +'.bin'
dest_file='NPT1800_Emb_'+ s +'.bin'
shafile= open(server_path + '\\' +'NPT1800_Emb_1p1_sha256')
sha.append(shafile.read( ))
print ('Hash of version is '+sha[0])
shafile.close( )
print ('source_file is %s' %source_file)
if version_type==2:
source_file=server_path + '\\' +'NPT1800_Emb_2p0_'+ s +'.bin'
dest_file='NPT1800_Emb_2p0_'+ s +'.bin'
shafile= open(server_path + '\\' +'NPT1800_Emb_2p0_sha256')
sha.append(shafile.read( ))
print ('Hash of version is '+sha[0])
shafile.close( )
print ('source_file is %s' %source_file)
#dest_file='NPT1800_Emb.bin'

FileExists=os.path.isfile(dest_file)
if not FileExists:
print 'Start get version ...........\n'
shutil.copyfile(source_file,dest_file)
print 'Get version successfully\n'
else:
print 'File exists'


try:
sftp_port = 22
username='root'
password='root'
t=paramiko.Transport(hostname,sftp_port)
t.connect(username=username,password=password)
sftp=paramiko.SFTPClient.from_transport(t)
local_dir = 'D:\\version\\NPT\\1800\\'
remote_dir='/sdboot/up/'
dest_file1='NPT1800_Emb.bin'
print 'Start upload new version ......'
sftp.put(os.path.join(local_dir,dest_file),os.path.join(remote_dir,dest_file1))
t.close()
print 'upload new version successful'

except:
print "upload new version fail"
t.close()

def main(ip,version,mode):
sha=[]
hostname =ip
versionNUM = version
version_type = int(mode)
'''
reset_flag=int(raw_input('Do you want to reset NE automatically or not? yes:1'))
if reset_flag=1:
clear_flag=(int(raw_input('Do you want clear NE DB? yes:1')))
'''
listNum=versionNUM.split('.',)
n1=listNum[0]
n2=listNum[1]
n3=listNum[2]
server_path=get_version_path(n1,n2,n3,versionNUM)

sftp_get_version(version_type,server_path,hostname,n1,n2,n3,sha)


#记录ssh 登录日志
paramiko.util.log_to_file("log.log")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#ssh.connect("某IP地址",22,"用户名", "口令")
ssh.connect(hostname,22,"root", "root")
#sh 登录到NE,设置FTP连接权限:
'''
stdin, stdout, stderr = ssh.exec_command("/etc/init.d/xinetd stop")
stdin, stdout, stderr = ssh.exec_command("tcpsvd 0 21 ftpd -w /ftppub &")
stdin, stdout, stderr = ssh.exec_command("/etc/init.d/xinetd start")
'''
#修改startup文件
stdin, stdout, stderr = ssh.exec_command("sed -i 's/down/up/g' /sdboot/startup")
ssh1=ssh.invoke_shell()
ssh1.send('cd /sdboot/up/\n')
time.sleep(1)
ssh1.send('sha256sum NPT1800_Emb.bin\n')
time.sleep(7)
sha1 = ssh1.recv(10000)
print sha1
pattern1=re.compile(sha[0])
hash_match=re.findall(pattern1,sha1)
if hash_match:
print "Main MCP hash is OK"
if version_type ==3:
dest_file='NPT1800_Emb.bin'
stdin, stdout, stderr = ssh.exec_command('arp\n')
a=''
for std in stdout.readlines():
a+=std
print a
pattern2=re.compile('(169.254.1.2|169.254.1.3)')
backup_ip=re.findall(pattern2,a)
print('Standby MCP IP is '+backup_ip[0])

str='ftpput -u root '+backup_ip[0]+' /sdboot/up/'+dest_file+' /sdboot/up/'+dest_file+'\n'
print str
stdin, stdout, stderr = ssh.exec_command(str)
print stdout.readline()
b=''
for std in stdout.readlines():
b+=std
print b
ssh1.send('telnet '+backup_ip[0]+'\n')
time.sleep(1)
ssh1.send('root\n')
ssh1.send('cd /sdboot/up/\n')
time.sleep(1)
ssh1.send('sha256sum NPT1800_Emb.bin\n')
time.sleep(7)
sha2 = ssh1.recv(10000)
print sha2
pattern1=re.compile(sha[0])
hash_match1=re.findall(pattern1,sha2)
if hash_match1:
print "Standby MCP hash is OK"
#print "Start sync version.........."
ssh1.send("sed -i 's/down/up/g' /sdboot/startup\n")
time.sleep(1)
edit_up= ssh1.recv(10000)
print edit_up
print "Start to sync standby MCP version of "+hostname
ssh1.send('sync\n')
else:
print "Standby MCP shasum error"


if (hash_match!=[])and(hash_match1!=[]):
print "Start to sync main MCP version of "+hostname
stdin, stdout, stderr = ssh.exec_command("sync")
print stdout.readline()
print "Finish main MCP sync version"
time.sleep(100)
sync_result=ssh1.recv(10000)
print sync_result
pattern_sync=re.compile('#')
sync_complete=re.findall(pattern_sync,sync_result)
if sync_complete:
print sync_complete[0]
print 'Finish Standby MCP sync version'+hostname
else:
print 'Standby MCP sync error!'
else:
print "shasum error"
'''
#+++++++++++++++++Reset++++++++++++++
if reset_flag=1:
reset_str='request reset ne'+'\n'
clear_str='request reset no-recovery-sdh'+'\n'
ssh2=ssh.invoke_shell()
ssh2.send('lsh\n')
time.sleep(1)
if clear flag=1:
ssh2.send(clear_str)
time.sleep(1)
ssh2.send('yes\n')
else:
ssh2.send(reset_str)
time.sleep(1)
ssh2.send('yes\n')

'''

ssh.close()
raw_input('press any key to exit \n')

root=Tk()
root.title('NE Upgrage for 1800')
root.geometry('600x600')
root.resizable(width=True, height=True)

#+++++++++Label+++++++++++++++++
l=Label(root,text='This a simple script to Upgrade NE.Provided by CC',bg='green', font=('Arial',10),width=100, height=5)
l.pack()
#+++++++++frame&Label+++++++++++
frm=Frame(root)
frm_L=Frame(root)
Label(frm_L, text='Input NPT1800 IP like a.b.c.d',font=('Arial,10')).pack(side=TOP)
frm_L.pack(side=TOP)
#+++++++++IP ENTRY++++++++++++++++
var_ip=StringVar(root)
e_ip=Entry(root,textvariable = var_ip)
var_ip.set('200.200.180.24')
e_ip.pack(side=TOP)

frm_R=Frame(root)
Label(frm_R, text='Input the full name of versionNUM like 5.0.018 please input 5.0.018',font=('Arial,10')).pack(side=TOP)
frm_R.pack(side=TOP)
#++++++++VERSION ENTRY+++++++++++++
var_version=StringVar(root)
e_version=Entry(root,textvariable = var_version)
var_version.set('6.0.005')
e_version.pack(side=TOP)

#+++++++++++Mode LABEL+++++++++++++
frm_M=Frame(root)
Label(frm_M, text='Select the Upgrade mode',font=('Arial,10')).pack(side=TOP)
frm_M.pack(side=TOP)
#+++++++++++Mode Selector+++++++++++++
mode= StringVar()
mode.set("1")

radioframe = Frame()
radioframe.pack()

# 'text' is the label
# 'variable' is the name of the variable that all these radio buttons share
# 'value' is the value this variable takes on when the radio button is selected
# 'anchor' makes the text appear left justified (default is centered. ick)
radioframe.choc = Radiobutton(
radioframe, text="1+0 mode",
variable=mode, value="1",
anchor=W)
radioframe.choc.pack(fill=X)

radioframe.straw = Radiobutton(
radioframe, text="2p0 mode",
variable=mode, value="2",
anchor=W)
radioframe.straw.pack(fill=X)

radioframe.lemon = Radiobutton(
radioframe, text="1+1 mode",
variable=mode, value="3",
anchor=W)
radioframe.lemon.pack(fill=X)

# this is a text entry that lets you type in the name of a flavor too.
'''
entry = Entry(textvariable=mode)
entry.pack(fill=X)
QUIT = Button(text='QUIT', foreground='red',command=quit)
QUIT.pack(side=BOTTOM, fill=BOTH)
'''
frm.pack()
#+++++++++++++++++++Text+++++++++++
t=Text(root,width=40,height=20)
t.insert(4.0,'This advertisement place is available.\n')
t.insert(END,'Just cantact me.\n')
t.insert(END,'This upgrade_script version will not reset NE automatically for you.\n')
t.insert(END,'If you want to add reset function, also cantact me!\n')
#t.insert(SEL_FIRST,'G')
t.pack(side=BOTTOM)
#+++++++++++++++++++Button+++++++++++
def printtime():
t.insert(END, time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+'\n')
def do_upgrade():
ip=e_ip.get()
version=e_version.get()
version_mode=mode.get()
main(ip,version,version_mode)
Button(root,text='Upgrade',command = lambda:thread_it(do_upgrade)).pack()
#+++++++++++++++++MENU++++++++++++++++++++
menubar=Menu(root)
#创建下拉菜单File,然后将其加入到顶级的菜单栏中
filemenu = Menu(menubar,tearoff=0)
filemenu.add_command(label="Open", command=printtime)
filemenu.add_command(label="Save", command=printtime)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

#创建另一个下拉菜单Edit
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Cut", command=printtime)
editmenu.add_command(label="Copy", command=printtime)
editmenu.add_command(label="Paste", command=printtime)
menubar.add_cascade(label="Edit",menu=editmenu)
#创建下拉菜单Help
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=printtime)
menubar.add_cascade(label="Help", menu=helpmenu)

#显示菜单
root.config(menu=menubar)
#++++++++++++++++++++++++++++++++++++++++++++++++++

if __name__=='__main__':

root.mainloop()

Upgrade NE script with GUI but cannot support multithread, need to add soon的更多相关文章

  1. NE Upgrade python script. Need to write a Tkinter GUI for it

    # -*- coding: utf-8 -*-#from ftplib import FTP __authour__='CC' import osimport telnetlibimport time ...

  2. From MSI to WiX, Part 8 - Major Upgrade, by Alex Shevchuk

    Following content is reprinted from here, please go to the original website for more information. Au ...

  3. 详解jQ的support模块

    jQuery的属性support是判断浏览器之间是否兼容的模块 ,该模块包含了leadingWhitespace,tbody,htmlSerialize,style,hrefNormalized,op ...

  4. Add GUI to connect to SQL

    (*********************************************************************************) (* *) (* Below i ...

  5. dat.gui stats.js 通用参数配置及图像统计工具

    在网上看到了一个非常好的JS烟雾效果 https://paveldogreat.github.io/WebGL-Fluid-Simulation/看源码时发现了dat.gui很好用. dat.gui ...

  6. Pascal Script

    MsgBox http://www.jrsoftware.org/ishelp/index.php?topic=isxfunc_msgbox ExpandConstant http://www.jrs ...

  7. Codelab for Android Design Support Library used in I/O Rewind Bangkok session

    At the moment I believe that there is no any Android Developer who doesn't know about Material Desig ...

  8. Android Meterial Design Support Library

    extends:http://inthecheesefactory.com/blog/android-design-support-library-codelab At the moment I be ...

  9. [Cypress] install, configure, and script Cypress for JavaScript web applications -- part2

    Use Cypress to test user registration Let’s write a test to fill out our registration form. Because ...

随机推荐

  1. ISO8583报文协议

    最开始时,金融系统只有IBM这些大的公司来提供设备,象各种主机与终端等.在各个计算机设备之间,需要交换数据.我们知道数据是通过网络来传送的,而在网络上传送的数据都是基于0或1这样的二进制数据,如果没有 ...

  2. Exec in Job and NewQuery

    1.背景 Job:一个步骤执行两个存储过程ProcA.ProcB.ProcA定义一个游标,从表TabA中检索数据,逐条插入到表TabB.如果某条数据不满足TabB上的约束(比如非空)导致插入失败.那么 ...

  3. Windows Server 2008 R2组策略设置计算机配置和用户配置

    一.认识Windows Server 2008 R2域控组策略管理 1.域控服务器zhuyu.com的组策略管理默认会读取AD用户和计算机目录下创建的OU容器(组织单元), 在对应的OU容器创建对应的 ...

  4. 在 Apache Ant中设置Proxy服务器

    <target name="proxy"> <property name="proxy.host" value="https://m ...

  5. Sass与Web组件化相关的功能

    Sass https://en.wikipedia.org/wiki/Sass_(stylesheet_language) Sass (Syntactically Awesome Stylesheet ...

  6. java实现excel表格导出数据

    /** * 导出清单 eb中 firstRow(EntityBean) 列表第一行数据,键值对(不包含序号)例:("name","姓名") * data(Ent ...

  7. lintcode bugfree and good codestyle note

    2016.12.4, 366 http://www.lintcode.com/en/problem/fibonacci/ 一刷使用递归算法,超时.二刷使用九章算术的算法,就是滚动指针的思路,以前写py ...

  8. GPS部标监控平台的架构设计(十一)-基于Memcached的分布式Gps监控平台

    部标gps监控平台的架构,随着平台接入的车辆越来越多,架构也面临越来越大的负载挑战,我们当然希望软件尽可能的优化并能够接入更多的车辆,减少在硬件上的投资.但是当车辆增多到某一个临界点的时候,仍然要面临 ...

  9. JAVA NIO系列(二) Channel解读

    Channel就是一个通道,用于传输数据,两端分别是缓冲区和实体(文件或者套接字),通道的特点(也是NIO的特点):通道中的数据总是要先读到一个缓冲区,或者总是要从一个缓冲区中读入. Channel的 ...

  10. Leetcode: Sequence Reconstruction

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...