포스팅 목적


요즘 보안 제품들을 보면, API 는 이제는 없어서는 안될 필수 요건인 것처럼 보여집니다. 폐쇄적인 느낌에서 좀더 개방적인 추세로 전환되고 있습니다. 서로 다른 제품 간의 연계를 통한 상관 관계 분석 또는 데이터 인리치먼트 등을 통해 새로운 도입 제품과 기존 운용중인 제품과의 유기적인 결합을 통해 보안 고도화를 이루어 내는 것이 시장의 분위기 인듯 합니다.

실무자의 입장에서 쉽게 Python 을 통해 REST API(JSON) 을 사용 할 수 있는 예제를 적어 봤습니다. 

작성자도 초보 수준의 코딩만 가능하여, 어려운 질문은 받을 수 없습니다...



사전 필요 사항


당연하게도 Python 이 설치 되어 있어야 합니다. (python 3.6 기준으로 작성되었습니다.)

저의 경우에는 테스트를 위해 eclipse를 활용했습니다. 

eclipse 에서 python 을 사용하는 방법은 다음에 포스팅 하겠습니다.




참고 사항

JSON 포멧으로 데이터를 호출하는 예제 테스트 페이지 : https://api.androidhive.info/contacts/




예제


* IF

number = 31

guess = int(input('Enter an Integer : '))


if guess == number:

    print('Conguraturations, you guessed it.')

elif guess < number:

    print('No, it is a little higher than that')

else:

    print('No, it is a little lower than that')

    

print('done')



* For

for i in range(1,5):

    print(i)

else:

    print('the for loop is over')


* Break + len 

while True:

    s = input('Enter something : ')

    if s == 'quit':

        break

    print('Length of the string is', len(s))

print('Done')


* While + IF

number = 31

running = True



while running:

    guess = int(input('Enter an Integer :'))

    

    if guess == number:

        print('Conguraturations')

    elif guess < number:

        print('No, It is a little higher than that')

    else:

        print('No, It is a little lower than that')

else:

    print('The while loop is over.')

print('done')


* 기본적인 한글 웹페이지 크롤링

import urllib.request



f = urllib.request.urlopen("http://finance.naver.com")

fp = f.read()

fps = fp.decode('euc-kr')

f.close()


print(fps)



* JSON 포멧으로 REST API 사용

import json

import urllib.request



urlTicker = urllib.request.urlopen('https://api.androidhive.info/contacts/')


readTicker = urlTicker.read()


dict = json.loads(readTicker)

dict2 = json.loads(readTicker)


for h in dict['contacts']:

    print(h['id'])

    

    

for h in dict2['contacts']:

    print(h['id'],h['phone']['mobile'])








[출처] 제타위키 http://zetawiki.com/wiki/%EB%A6%AC%EB%88%85%EC%8A%A4_%EB%A9%94%EB%AA%A8%EB%A6%AC_%EC%82%AC%EC%9A%A9%EB%A5%A0_%ED%99%95%EC%9D%B8





sar

명령어
sar -r 1
실행예시
[root@zetawiki ~]# sar -r 1
Linux 2.6.30.10-105.2.23.fc11.x86_64 www.jmnote.com) 	05/06/2012

12:54:07 PM kbmemfree kbmemused  %memused kbbuffers  kbcached kbswpfree kbswpused  %swpused  kbswpcad
12:54:08 PM     42440    490744     92.04     65124    168916   1146744       128      0.01        16
Average:        42440    490744     92.04     65124    168916   1146744       128      0.01        16




free

명령어
free
실행예시
[root@zetawiki ~]# free
             total       used       free     shared    buffers     cached
Mem:        533184     490736      42448          0      65264     168936
-/+ buffers/cache:     256536     276648
Swap:      1146872        128    1146744

top

명령어
top -n1 | grep Mem:
실행예시
[root@zetawiki ~]# top -n1 | grep Mem:
Mem:    533184k total,   494296k used,    38888k free,    64920k buffers

meminfo

명령어
cat /proc/meminfo | grep Mem
실행예시
[root@zetawiki ~]# cat /proc/meminfo | grep Mem
MemTotal:         533184 kB
MemFree:           41860 kB







1. Destination Server 에서 아래 명령어를 통해 인증 키 생성, 옵션을 통해 인증키의 인증방식을 수정 가능

[root@Destination Server ~]# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): 

Created directory '/root/.ssh'.

Enter passphrase (empty for no passphrase): 

Enter same passphrase again: 

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:6MLun8gQLmRbAqPGH5XKw+WFX3wKdfGC8mm+ziyWyUs root@Destination Server.net

The key's randomart image is:

+---[RSA 2048]----+

|         . o.    |

|      o o o .    |

|o    = + + o .   |

|+.o = o.= + .    |

|.*.B ...S=       |

|+.=oo.  o        |

|..o.o oEo.       |

| . + o.Bo .      |

|   .=.+.+=       |

+----[SHA256]-----+

[root@Destination Server /]# cd ~/.ssh/

[root@Destination Server .ssh]# vi ./authorized_keys <- Source 서버에서 생성한 키를 삽입 할 목적



2. Source가 될 리눅스 서버에서 아래 명령어 수행

[root@Source Server /]# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): 

Enter passphrase (empty for no passphrase): 

Enter same passphrase again: 

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

5e:32:7c:5e:67:58:5c:31:7d:c0:19:20:83:4e:c2:88 root@Source Server

The key's randomart image is:

+--[ RSA 2048]----+

|   . o   .o .oo*+|

|  E . o o  o .o.+|

|       +      o .|

|       ..    o   |

|        S o o o  |

|       . * . o   |

|        . .      |

|                 |

|                 |

+-----------------+

[root@Source Server /]# cat ~/.ssh/*pub

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDmk5uEnZOcMC8OtWyyu/FLQmqyla5VbSo2yR1bRwgDP4zcxDkm3bvKZAmRbQYOKzOum++jOUZBliW141wXhV+LqGAvg2vYOa5y06Gfdlw6pJwVA8/1/H/BBPFCpYGShUJNMxw89Y/Ct3gcH1f0umcxzaJRvhzpu0z/FDmr3ZFyMT4oQ+4pHz6IgtFKiDCzQZJbmm5i9zLBoGGyRtNYaQyF46YB+u567hU1SIbMRofzaWFqePt6ty0XboPmIdcgPD0HEjLDCWTttXgPdprK54m7MZP2SMR3EGTKRMsSp48ssMrQompMkP2HXHTEI3wK/Bia7gmx+VB+CpyQRMsV/eQB root@Source Server


3. Source가 될 리눅스 서버에서 해당 인증키를 복사하여 Destination Server authorized_keys 에 등록




4. 접속 테스트

[root@Source Server /]# ssh 192.168.0.241

Last login: Mon Apr  3 21:34:01 KST 2017 on cron

Last login: Mon Apr  3 21:34:55 2017 from 192.168.0.242

[root@Destination Server ~]# 



5. 원격 명령어 수행 테스트

[root@Source Server /]# ssh 192.168.0.241 free

             total       used       free     shared    buffers     cached

Mem:       4059404    3643352     416052          0       4752     423612

-/+ buffers/cache:    3214988     844416

Swap:            0          0          0

[root@Source Server /]# 

[root@Source Server /]# ssh 192.168.0.241 hostname

Destination Server



+ Recent posts