# -*- coding: utf-8 -*-
#from ftplib import FTP

__authour__='CC'

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

def get_version_path(n1,n2,n3):
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):

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()

if __name__ == '__main__':
sha=[]
hostname = raw_input('Input NPT1800 IP like a.b.c.d\n')

versionNUM = raw_input('Input the full name of versionNUM like 5.0.018 please input 5.0.018 \n')
version_type = int(raw_input('1+1 mode input 1;2+0 mode input 2;1+1 mode print 3\n'))

listNum=versionNUM.split('.',)
n1=listNum[0]
n2=listNum[1]
n3=listNum[2]
server_path=get_version_path(n1,n2,n3)

sftp_get_version(version_type,server_path,hostname)


#记录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........"
ssh1.send('sync\n')
else:
print "Standby MCP shasum error"


if (hash_match!=[])and(hash_match1!=[]):
print "Start to sync main MCP version.........."
stdin, stdout, stderr = ssh.exec_command("sync")
print stdout.readline()
print "Finish main MCP sync version"
time.sleep(60)
sync_result=ssh1.recv(10000)
print sync_result
pattern_sync=re.compile('#')
sync_complete=re.findall(pattern_sync,sync_result)
if sync_complete==1:
print 'Complete Standby MCP.'
else:
print 'Standby MCP sync error!'
else:
print "shasum error"
ssh.close()
raw_input('press any key to exit \n')

NE Upgrade python script. Need to write a Tkinter GUI for it的更多相关文章

  1. java + spring (jython\python\script) Error:SyntaxError: no viable alternative at character '\n'

    使用Jython结合java和Python开发功能时,要是遇到如下情况: 2016-03-10 16:16:49 DEBUG [com.freedom.orion.configs.JyhtonConf ...

  2. Windows 配置Apache以便在浏览器中运行Python script的CGI模式

    打开httpd.conf,找到”#ScriptInterpreterSource Registry “,移除前面的注释# (如果找不到这行,就自己添加进去) 找到“Options Indexes Fo ...

  3. Notepad++插件Emmet和Python Script的安装

    最近在做一个项目,涉及到大量的HTML.CSS代码的编写,手动写代码效率实在 是低下.于是想搜索一下,有没有Notepad++插件可以支持自动生成的,果不其然还真有.Emmet,这款神器其实就是 Ze ...

  4. rc.local 注意事項,call python script, file position

    如果要在 rc.local 呼叫 python script python script 的位置需使用絕對路徑 其 python script 裡的有關 file 的位置也需使用 絕對路徑 如果要在 ...

  5. A python script to check NE syncfail and get log from CIPS

    #! /usr/bin/env python # -*- coding: UTF-8 -*- """The script is to check whether NE i ...

  6. My first python script for work

    I write it yesterday to watch the NE process(rcpfd,cfgd) automatically, then i will write a window t ...

  7. [译]Why do people write #!/usr/bin/env python on the first line of a Python script?

    If you have several versions of Python installed, /usr/bin/env will ensure the interpreter used is t ...

  8. [Python] Execute a Python Script

    Python scripts can be executed by passing the script name to the python command or created as execut ...

  9. Excel vba call Python script on Mac

    How can I launch an external python process from Excel 365 VBA on OSX? It took me a while, but I fig ...

随机推荐

  1. Eclipse中配置svn

    1.打开eclipse,help--> Eclipse MarketPlace...,搜索输入“subclipse”,点击安装,一路按向导安装: 2.安装成功后,在Window --> S ...

  2. 建模分析之机器学习算法(附python&R代码)

    0序 随着移动互联和大数据的拓展越发觉得算法以及模型在设计和开发中的重要性.不管是现在接触比较多的安全产品还是大互联网公司经常提到的人工智能产品(甚至人类2045的的智能拐点时代).都基于算法及建模来 ...

  3. JDBC基础与连接sql2012

    JDBC简介 JDBC全称为:Java Data Base Connectivity(java数据库连接),它主要由接口组成. 组成JDBC的2个包: java.sql javax.sql 以上2个包 ...

  4. zepto和jquery的区别,zepto的不同使用8条小结

    说到诡异事件发生的原因,自然是想到两者之间的差异性. 首先是效果: jquery中有fadeIn和fadeOut两个效果,用来实现渐隐渐显的效果,这个在PC端自然是常用的效果.然后我们前端组的组员Mr ...

  5. visual studio 两个以上sln 引用同一个project ,生成时会改变projectguid问题

    当两个以上解决方案添加现有项,选择了同一个项目,那么在 sln 文件中,会自己带一个guid. 当打开两个解决方案,一个生成时,会影响另一个的project值,导致每次都看到了签出. 解决办法,打开共 ...

  6. 此操作失败的原因是对 IID 为“{000208DA-0000-0000-C000-000000000046}”的接口的 COM 组件调用 QueryInterface

    有些电脑报错,有些电脑正常. 环境:VS2010 WinForm程序, Office2007 C#操作Excel时报错.错误: 无法将类型为“System.__ComObject”的 COM 对象强制 ...

  7. ASP.NET 前后台调用方法

    Asp.Net中几种相似的标记符号: <%=...%>< %#...%>< %%>< %@ %>解释及用法 答: < %#... %>:是在 ...

  8. Android 监听返回键、HOME键

    拦截返回键,HOME键,继承BaseActivity即可 import android.app.Activity; import android.content.BroadcastReceiver; ...

  9. centos 配置 ssl服务

    使用的是appach 2.4.10 版本 各个版本配置不同 1.首先修改httpd.conf 文件 appach 安装目录下的 conf文件夹中找到 #LoadModule socache_shmcb ...

  10. 常见UI布局之1-2-1单列变宽布局

    扩展前一篇“上中下三栏布局”,中间栏划分成两列,一列宽度固定,一列宽度随浏览器窗口宽度的变化而变化.固定宽度列定义为#side,可分为左侧布局和右侧布局,分别实现如下: 1-2-1左侧固定宽度布局 & ...