Python installation

1 install python 2.7.x
2 install pycrypto
3 install paramiko: uncompress the „paramiko-1.7.7.1.rar“ to local, login to local directory in CMD command window, and then execute: python setup.py install

Recommended programming tool is WingIDE for development.

Script sample

import paramiko
import time
import os
def sftpNeLog(ip, devname, username, passwd, nename):

print ‚Begin back:‘ + devname

# The files and directories to be backed up are specified in a list.
fileList = { ‚see‘ : ‚/home/see/nginrun/log/run/*.log*‘,
‚dr‘ : ‚/home/see/dr/log/*.*‘,
‚bmp‘ : ‚/home/bmp/log/debug/*.log* /home/bmp/log/state/*.log* /home/bmp/log/run/*.log* /home/bmp/jboss/logs/*.log* /home/bmp/log/performance/*prf* /home/bmp/jboss/logs/VPNLogger.log*‘,
‚runtime_0111‘ : ‚/opt/jnetx/runtime/1.1/slee/log/*.log /opt/jnetx/runtime/SMP_Node.Server1/smp/log/*.log‘,
‚runtime_0221‘ : ‚/opt/jnetx/runtime/2.1/slee/log/*.log /opt/jnetx/runtime/SMP_Node.Server2/smp/log/*.log‘
}
remoteTarPath = { ‚see‘ : ‚/home/see/‘,
‚dr‘ : ‚/home/see/‘,
‚bmp‘ : ‚/home/bmp/‘,
‚runtime_0111‘ : ‚/opt/jnetx/‘,
‚runtime_0221‘ : ‚/opt/jnetx/‘
}

# Conn string to NE.
client=paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip,username=username,password=passwd)

# local back directory.
targetDir = ‚d:/runlogrestore/‘
today = targetDir + ‚CZ_‘ + time.strftime(‚%Y%m%d‘)
now = time.strftime(‚%H%M%S‘)
# Take a comment from the user to create the name of the zip file
#comment = raw_input(‚Enter a comment –> ‚)
comment = “
if len(comment) == 0: # check if a comment was entered
target = today + os.sep + ‚CZ_‘ + time.strftime(‚%Y%m%d‘) + ‚_‘ + now + ‚_‘ + devname + ‚_‘ + nename + ‚.tar.gz‘
else:
target = today + os.sep + ‚CZ_‘ + time.strftime(‚%Y%m%d‘) + ‚_‘ + now + ‚_‘ + devname + ‚_‘ + nename + ‚_‘ +
comment.replace(‚ ‚, ‚_‘) + ‚.tar.gz‘
# Create the subdirectory if it isn’t already there
if not os.path.exists(today):
os.makedirs(today) # make directory
print ‚Local back directory %s‘ % (today)

# tar service log
tarName = remoteTarPath[nename] + ‚getrunlog.tar.gz‘
command = „tar -zcvf %s %s“ % (tarName, fileList[nename])
print command
aa,bb,cc=client.exec_command(command)
print bb.read()

# get file
transport = paramiko.Transport((ip,22))
transport.connect(username=username,password=passwd)
sftp = paramiko.SFTPClient.from_transport(transport)
print ‚Back file:‘ + target
sftp.get(tarName,target)
sftp.close()
transport.close()

# delete remote temp file
comment = ‚rm ‚ + tarName
print comment + ‚n‘
aa,bb,cc=client.exec_command(comment)
client.close()
if __name__==’__main__‘:

#sftpNeLog(‚ip.xx.xx.xx‘, ‚hostname‘, ‚see‘, ‚password‘, ‚see‘)
#sftpNeLog(‚ip.xx.xx.xx‘, ‚hostname‘, ‚see‘, ‚password‘, ‚see‘)

Jan D.
Jan D.

"The only real security that a man will have in this world is a reserve of knowledge, experience, and ability."

Articles: 664