-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from QcloudApi.qcloudapi import QcloudApi | ||
from json import loads | ||
from json import dumps | ||
import json | ||
module='cvm' | ||
action = 'RunInstances' | ||
config = { | ||
'Region': 'ap-beijing', | ||
'secretId': 'AKIDYFqHXa0G82sW9bRtlXEwVt7B4kEZHsjO', | ||
'secretKey': 'c2KQK2YwAD5lGaieP2Vo0GNJ3by98vYz', | ||
} | ||
|
||
action_params = { | ||
'Version':'2017-03-12', | ||
'InstanceChargeType':'POSTPAID_BY_HOUR', | ||
'Placement.Zone':'ap-beijing-3', | ||
'InstanceType':'S2.SMALL1', | ||
'ImageId':'img-8toqc6s3', | ||
'DataDisks.0.DiskType':'CLOUD_BASIC', | ||
'DataDisks.0.DiskSize':'60', | ||
} | ||
#________________________________________________ | ||
|
||
|
||
try: | ||
service = QcloudApi(module, config) | ||
print(service.generateUrl(action, action_params)) | ||
# service.call(action, action_params) | ||
# answer = (service.call(action, action_params)) | ||
# print answer | ||
except Exception as e: | ||
import traceback | ||
print('traceback.format_exc():\n%s' % traceback.format_exc()) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/python | ||
# coding: utf-8 | ||
|
||
import pexpect | ||
import sys | ||
import threading | ||
import time | ||
import random | ||
|
||
HOST = "10.117.216.29" | ||
PORT = 2222 | ||
USER = "admin" | ||
PASSWORD = "admin" | ||
HOST_NUM = 4 | ||
DEBUG = True | ||
#DEBUG = False | ||
# 并发会话数 | ||
TOTAL = 100 | ||
|
||
PS1 = "sflog" | ||
RUN_COMMAND = ["tail /var/log/dmesg", "echo hello world"] | ||
stop_event = threading.Event() | ||
|
||
|
||
def connect(i=1): | ||
cmd = "ssh %s@%s -p%s" % (USER, HOST, PORT) | ||
print(u"开始连接 [%s]: %s" % (i, cmd)) | ||
child = pexpect.spawn(cmd) | ||
|
||
if DEBUG: | ||
child.logfile = sys.stdout | ||
|
||
while not stop_event.is_set(): | ||
index = child.expect([ | ||
"password:", "Jumpserver", "主机名", PS1, | ||
pexpect.EOF, pexpect.TIMEOUT | ||
]) | ||
if index == 0: | ||
if DEBUG: | ||
print(u"开始输入密码:") | ||
child.sendline(PASSWORD) | ||
time.sleep(2) | ||
elif index == 1: | ||
if DEBUG: | ||
print(u"输入 p") | ||
child.sendline("p") | ||
time.sleep(1) | ||
elif index == 2: | ||
n = random.choice(range(2, HOST_NUM + 1)) | ||
if DEBUG: | ||
print(u"选择一个登陆: %s" % (n)) | ||
child.sendline(str(n)) | ||
elif index == 3: | ||
if DEBUG: | ||
print(u"登陆成功,执行命令:%s" % RUN_COMMAND) | ||
child.sendline(RUN_COMMAND[0]) | ||
time.sleep(1) | ||
time.sleep(1) | ||
|
||
|
||
def run(): | ||
for i in range(TOTAL): | ||
t = threading.Thread(target=connect, args=(i,)) | ||
t.daemon = True | ||
t.start() | ||
time.sleep(0.1) | ||
done = input("Done ?") | ||
print("Done test") | ||
|
||
|
||
if __name__ == '__main__': | ||
run() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#-*- coding:utf-8 -*- | ||
print "It's Qcloud API test! For Stop Instances" | ||
from QcloudApi.qcloudapi import QcloudApi | ||
from json import loads | ||
from json import dumps | ||
import json | ||
module='cvm' | ||
action = 'DescribeImages' | ||
config = { | ||
'Region': 'ap-beijing', | ||
'secretId': 'AKIDYFqHXa0G82sW9bRtlXEwVt7B4kEZHsjO', | ||
'secretKey': 'c2KQK2YwAD5lGaieP2Vo0GNJ3by98vYz', | ||
} | ||
action_params = { | ||
# 'limit':2, | ||
'Version': '2017-03-12', | ||
'InstanceIds.1':'ins-3aesju6b', | ||
'private-ip-address':'172.21.0.5', | ||
'ForceStop':'TRUE', | ||
|
||
} | ||
try: | ||
service = QcloudApi(module, config) | ||
# print(service.generateUrl(action, action_params)) | ||
service.call(action, action_params) | ||
# res=json.loads(answer) | ||
# print res | ||
#------------------------------ | ||
service.generateUrl(action, action_params) | ||
answer = (service.call(action, action_params)) | ||
print answer | ||
# print answer | ||
# zi_dict=eval(answer) `` | ||
# print zi_dict['totalCount'] | ||
# instance=(zi_dict['instanceSet'][0]) | ||
# print(len(zi_dict) | ||
# for instance in zi_dict['instanceSet']: | ||
|
||
# a=dict(instance.items()) | ||
# print(a) | ||
# if a['lanIp']=='172.21.16.3': | ||
# print(a['instanceId']) | ||
# for k,v in instance.items(): | ||
# if k == 'lanIp' and v =='172.21.16.3': | ||
|
||
# print(k,v) | ||
#______________________________ | ||
except Exception as e: | ||
import traceback | ||
print('traceback.format_exc():\n%s' % traceback.format_exc()) |