Created
June 5, 2014 07:15
-
-
Save widoyo/4259d82a9bd2cff6bddb to your computer and use it in GitHub Desktop.
Collect Pra Recon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, datetime | |
from django.core.management.base import BaseCommand, CommandError | |
from django.db import connections | |
from django.conf import settings | |
import paramiko | |
SSH_USERNAME = getattr(settings, 'SSH_USERNAME', 'mulyo') | |
SSH_PASSWORD = getattr(settings, 'SSH_PASSWORD', 'toha123') | |
SSH_HOST = getattr(settings, 'SSH_HOST', 'nsn.sparelog.com') | |
# Perintah-perintah di Web faction | |
CD_WORK = "cd $HOME/webapps/g_nsn/g_nsn" | |
CD_SANDBOX_WORK = "cd $HOME/webapps/g_nsn/g_nsn" | |
class Command(BaseCommand): | |
help = "Push data pra recon into host where nsn.sparelog.com run" | |
def handle(self, *args, **kwargs): | |
self.data_path = os.path.join(settings.PROJECT_PATH, settings.STATIC_URL[1:] + 'data/') | |
if not os.path.exists(self.data_path): | |
os.mkdir(self.data_path) | |
self.fname = "spms_part.csv" | |
self.build_data() | |
self.put_into_remote() | |
def build_data(self): | |
cursor = connections['sparelog'].cursor() | |
cursor.execute("SELECT * \ | |
FROM VIEW_STOK_P20_REV \ | |
ORDER BY PARTNUMBER, SERIALNUMBER") | |
cur_data = [] | |
for row in cursor.fetchall(): | |
try: | |
cur_data.append("|".join(map(str, row))) | |
except UnicodeEncodeError: | |
print row | |
# Menulis ke output file | |
with open(os.path.join(self.data_path, self.fname), 'w') as f: | |
f.write("SN|PARTNUM|BASECODE|MATERIAL|POSITION|STATUS|POOL|WBS\n") | |
for row in cur_data: | |
f.write(row + "\n") | |
def put_into_remote(self): | |
ssh = paramiko.SSHClient() | |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
ssh.connect(SSH_HOST, username=SSH_USERNAME, password=SSH_PASSWORD) | |
# put file into remote | |
sftp = ssh.open_sftp() | |
sftp.put(os.path.join(self.data_path, self.fname), | |
os.path.join('/tmp', self.fname)) | |
sftp.close() | |
ssh.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment