Friday, May 26, 2017

Execute commands on remote machine using python


import paramiko
remote_machine_name = raw_input('Enter the remote machine name:')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(remote_machine_name, username='xxx', password='yyy')
print "connected successfully!"
except paramiko.SSHException:
print "connection error"
stdin, stdout, stderr = ssh.exec_command('') # ssh.exec_command('(cd path;./shellscript.sh)')
print "stderr: ", stderr.readlines()
print "stdout: ", stdout.readlines()
view raw ssh_python.py hosted with ❤ by GitHub

Monday, May 8, 2017

Hex to Decimal conversion in python


import math
num = raw_input() #2017
sum = 0
length = len(num)
for l in xrange(length):
sum = sum + (math.pow(16, length-l-1) * int(num[l]))
print sum
view raw dectohex.py hosted with ❤ by GitHub

Sunday, January 29, 2017

List of lists in Java


package com.test;
import java.util.ArrayList;
public class Arraylist {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<ArrayList> x = new ArrayList<>();
ArrayList al1 = new ArrayList(); //first array list
al1.add("banana"); //add three fruits to first array list
al1.add("starwbeery");
al1.add("apple");
//Now add first array list to x which is array of array list
x.add(al1);
ArrayList al2 = new ArrayList(); //second array list
al2.add("carrom");//add three games to second array list
al2.add("chess");
al2.add("badminton");
//Now add second array list to x which is array of array list
x.add(al2);
ArrayList al3 = new ArrayList();//third array list
al3.add("Gangully"); //add three players to thirs array list
al3.add("Zaheer");
al3.add("Dravid");
//Now add third array list to x which is array of array list
x.add(al3);
//To get these elements
for(ArrayList al: x){
System.out.println(al); //This prints the three different array lists
//To get each element from each arraylist
for(Object element : al){
System.out.println(element);
}
}
}
}