#-*-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+ ' * '
#通过通配符函数, Pass the path of the name "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 ' is present
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 ' input 5.0.018 \ n ')
version_type = int (raw_input (' + 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 logon Log
Paramiko.util.log_to_file ("log.log")
SSH = Paramiko. Sshclient ()
Ssh.set_missing_host_key_policy (paramiko. Autoaddpolicy ())
#ssh. Connect ("one IP address", 22, "username", "password")
Ssh.connect (hostname,22, "root", "root")
#sh Log on to the ne, set FTP connection permissions:
‘‘‘
stdin, stdout, stderr = Ssh.exec_command ("/etc/init.d/xinetd Stop")
stdin, stdout, stderr = Ssh.exec_command ("TCPSVD 0 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