Python Scripts

= Calculator =


 * 1) !/usr/bin/python

def calc: operation = raw_input( Please type operator: + for addition - for subtraction * for multiplication / for division )
 * 1) Define Function

#try: num1 = float(input('First Number: ')) num2 = float(input('Second Number: '))
 * 1) except:
 * 2) print("Invalid Input")

if operation == '+': add = num1 + num2 print("Addition is: %d" % int(add))

elif operation == '-': sub = num1 - num2 print("Subtraction is %d:" %int(sub))

elif operation == '*': mul = num1 * num2 print("Multiplication is %d:" %int(mul))

elif operation == '/': div = num1 / num2 print("Division is %.2f:" %float(div))

else: print("Invalid Operator")

again

def again: calc_again = raw_input( Do you want to calculate again? Please type Y for Yes or N for No. )

if calc_again.upper == "Y": calc elif calc_again.upper == "N": print('See you later on.. Bye') else: again

def welcome: print( Welcome to My Calculator )

welcome calc
 * 1) Call Function

= Add Routes to Remote Server =

import paramiko

ssh = paramiko.SSHClient ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) ssh.connect( '192.168.1.7', username = 'root', password = 'pwd@123' )

for x in range(1,256): for y in range(256): for z in range(256): for a in range(1): ssh.exec_command( "route add -net %d.%d.%d.%d netmask 255.255.255.0 gw 192.168.1.1 " %(x,y,z,a))

ssh.close

= CPU check of Remote Server =

import paramiko

ssh = paramiko.SSHClient ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy) ssh.connect( '10.107.88.93', username = 'aman', password = 'Passion@123' ) stdin, stdout, stderr = ssh.exec_command( 'top -bn2 | grep "Cpu(s)"' ) output = stdout.read ssh.close

stat = output.split idle = float(stat[24]) cpu = (100-idle)

print cpu


 * References