Skip to content

Instantly share code, notes, and snippets.

@rtomaszewski
rtomaszewski / cloud_api_limits.py
Created Jul 29, 2012
Be aware of the cloud API limitations when creating cloud servers
View cloud_api_limits.py
# example code that creates cloud server and try not to hit the cloud API climits
cs_count=13 # number of cs to build
build_nr=1
delayed_10s=False
while build_nr <= cs_count :
if 0==build_nr % 11 and not delayed_10s:
time.sleep(60+10)
@rtomaszewski
rtomaszewski / naive_cloud_api_limits.py
Created Jul 29, 2012
Naive API trying to create too many cloud server
View naive_cloud_api_limits.py
def cs_create(self, count, sample_nr):
name='test' + str(int(time.time()))
image=112
flavor=1
log("[%2d][%2d] creating image: " % (sample_nr, count) + pformat( {'name': name, 'image' : image, 'flavor' : flavor } ) )
sm=self.cs.servers
server=sm.create(name, image, flavor)
@rtomaszewski
rtomaszewski / example_debug.py
Created Jul 29, 2012
Primitive logging functions in your python scripts that are buffered before will be printed on the stdout
View example_debug.py
# Try to execute the script in two different modes to see the difference
#
# $ python log.py | tee log
# <noting for a long time>
#
# $ python -u log.py | tee log
# my log message 0
#
import time
@rtomaszewski
rtomaszewski / gil_threads_test.py
Created Jul 31, 2012
An example of multithreaded python code that creates about 500 long running threads
View gil_threads_test.py
import threading
import datetime
import time
from random import random
class ThreadClass(threading.Thread):
def set_id(self, id):
self.id=id
def dummy_delay(self):
@rtomaszewski
rtomaszewski / object_variables_example.py
Created Aug 9, 2012
Differences between class and instance variables in Python
View object_variables_example.py
class Test1InstanceVariable:
mylist=[]
def __init__(self, number):
self.number= number
def add(self, i):
self.mylist.append(i)
def show(self):
@rtomaszewski
rtomaszewski / JavaExample.java
Created Aug 9, 2012
Simple java code showing that before we can assign value to variable it has to be known to the javac compiler in advance
View JavaExample.java
public class JavaExample {
// with the next line commented the code is erroring out when we try to compile it
//public int number;
public JavaExample (int _number) {
number=_number;
}
public void show() {
System.out.println("[test " + number + "] list=%s");
@rtomaszewski
rtomaszewski / ssh_example.py
Last active Jul 7, 2019
Example script written in python that executes remotely commands over ssh
View ssh_example.py
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect('5.79.4.10', username='root', password='7raW6nS6Krttest')
stdin, stdout, stderr = ssh.exec_command('echo text on stdout stream; echo text on stderror stream 1>&2')
print("reading outputs from the remote command")
for l in stdout :
@rtomaszewski
rtomaszewski / check_rackconnect.sh
Created Aug 13, 2012
An example bash script checking RackConnect deployment status
View check_rackconnect.sh
#!/bin/bash
iptables=$(iptables -nL)
ret=no
if echo $iptables | grep -q 'Chain INPUT .policy DROP.' ; then
if echo $iptables | grep -q 'Chain FORWARD .policy DROP.' ; then
if echo $iptables | grep -q 'Chain OUTPUT .policy ACCEPT.' ; then
if echo $iptables | grep -q 'Chain RS-RackConnect-INBOUND' ; then
ret=yes
@rtomaszewski
rtomaszewski / example_paramiko_notty.py
Created Aug 19, 2012
example paramiko script that tries to run an interactive command that needs a terminal
View example_paramiko_notty.py
import paramiko
bastion_ip='ip' # you have to edit and provide valid IP address
bastion_pass='pass' # you have to edit it and provide valid password
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect(bastion_ip, username='root', password=bastion_pass)
chan = ssh.invoke_shell()
@rtomaszewski
rtomaszewski / example_paramiko_with_tty.py
Created Aug 19, 2012
example paramiko script with interactive terminal
View example_paramiko_with_tty.py
import paramiko
import time
import re
bastion_ip='ip'
bastion_pass='pass'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
ssh.connect(bastion_ip, username='root', password=bastion_pass)