Saturday, November 12, 2022

How do we kill the processes in linux/unix with a process name?

Below is the command to kill all the processes running based on a process name: ps -ef | grep {process_name} | grep -v grep | awk '{print $2}' | xargs kill


Eg: Kill all the processes that have chrome as part of the process name: ps -ef | grep chrome | grep -v grep | awk '{print $2}' | xargs kill

Monday, March 9, 2020

Calculate Network Subnet Range efficiently!

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

import static com.varra.auth.utils.CommonUtils.ipStringToLong;
import static java.lang.Integer.parseInt;
import static java.lang.Math.pow;
import static java.util.stream.Collectors.toList;

/**
* TODO Description go here.
*
* @author Rajakrishna V. Reddy
* @version 1.0
*/
public class SubnetCalculator {

private static final Pattern CIDR_PATTERN = Pattern.compile("/");

@Accessors(chain = true, fluent = true)
@Data
@Builder(access = AccessLevel.PRIVATE)
public static class Subnet {
private final int cidr;
private final int ipCount;
private final int startIp;
private final int endIp;

public static Subnet of(final String ipWithCIDR)
{
final String[] ipWithCIDRTokens = CIDR_PATTERN.split(ipWithCIDR);
final int numericCIDR = parseInt(ipWithCIDRTokens[1]);
final int netmaskNumeric = 0xffffffff << (32 - numericCIDR);
final int numberOfIPs = (int) pow(2, 32 - numericCIDR) - 1;
final int baseIP = (int)ipStringToLong(ipWithCIDRTokens[0]) & netmaskNumeric;
return Subnet.builder().cidr(numericCIDR).ipCount(numberOfIPs).startIp(baseIP).endIp(baseIP + numberOfIPs -1).build();
}
}

/**
* Specify IP in CIDR format like: new IPv4("10.1.0.25/16");
*
* @param ipWithCIDR ""
*/
public static List<String> getAvailableIps(final String ipWithCIDR) throws NumberFormatException {
return getAvailableIpsAsIntegers(ipWithCIDR).mapToObj(CommonUtils::ipLongToString).collect(toList());
}

/**
* Specify IP in CIDR format like: new IPv4("10.1.0.25/16");
*
* @param ipWithCIDR ""
* @return IntStream Stream of ips as integers
*/
public static IntStream getAvailableIpsAsIntegers(final String ipWithCIDR) throws NumberFormatException {
final Subnet subnet = Subnet.of(ipWithCIDR);
return IntStream.range(1, subnet.ipCount()).map(i -> i + subnet.startIp());
}

public static boolean isIpWithInSubnet(final String ipWithCIDR, final String ip) throws NumberFormatException {
final Subnet subnet = Subnet.of(ipWithCIDR);
final int ipAsInt = (int) ipStringToLong(ip);
return subnet.startIp() <= ipAsInt && ipAsInt <= subnet.endIp();
}
}

Friday, February 7, 2020

Problem with github: permission denied to your own repo In Windows!! ??

Using multiple GitHub Accounts on Windows sucks by default. If you are tired of Window's Credential Manager storing only one account's credential for git:https://github.com then here is a 1-minute fix for you.

TL;DR

If you know what you're doing just:

  • delete your GitHub credentials from Windows Credential Manager
  • run git config --global credential.github.com.useHttpPath true
  • continue coding

If you need more detailed instructions and learn about the background just read on here: https://dev.to/configcat/lazy-man-s-guide-multiple-github-https-accounts-on-windows-2mad



Thursday, December 5, 2019

Problem with github: permission denied to your own repo?

 I had this problem too but managed to solve it, the error is that ur computer has saved a git username and password so if you shift to another account the error 403 will appear. Below is the solution

For Windows you can find the keys here:

control panel > user accounts > credential manager > Windows credentials > Generic credentials

Next -> remove the Github keys.

Monday, August 12, 2019

How to replace all the entries in a string in Javascript? like replaceAll?

This should do the trick by adding a new method (replaceAll) to the String prototype.

String.prototype.replaceAll = function(search, replacement){
            return this.replace(new RegExp(search, 'g'), replacement);
        };

Tuesday, August 7, 2018

How to increase the number of processes in Oracle DB

The solution to this question is to increase the number of processes :

  1. Open command prompt
  2. sqlplus / as sysdba
  3. startup force;
  4. show parameter processes; -- This shows 150 processes allocated, i will increase the count to 800 now
  5. alter system set processes=800 scope=spfile;

Tried and tested.


Monday, July 2, 2018

How to setup a beyond compare as git mergetool?

How to set up a beyond compare as git mergetool?

git config --global diff.tool bc3
git config --global difftool.bc3.path "c:/program files/beyond compare 4/bcomp.exe"
git config --global merge.tool bc3
git config --global mergetool.bc3.path "c:/program files/beyond compare 4/bcomp.exe"



If the above instructions aren't working on your system, it's possible repository local settings are overriding the global settings.
The contents of the global git config file (c:\users\username\.gitconfig) from my working test system:

[diff]
        tool = bc3
[difftool "bc3"]
        path = c:/program files/beyond compare 4/bcomp.exe
[merge]
        tool = bc3
[mergetool "bc3"]
        path = c:/program files/beyond compare 4/bcomp.exe


Monday, May 28, 2018

How to merge a branch in git

Follow the commands below to do the auto merge the branch in git:

$ git checkout -b {new-branch-name}
$ git merge --no-ff {old-branch-name}
$ git push -u origin {new-branch-name}

Friday, May 25, 2018

Concatenating and combining lists in Python

CODE:
[4, None, 'foo'] + [7, 8, (2, 3)]

or

x = [4, None, 'foo']
x.extend([7, 8, (2, 3)])
print(x)


OUTPUT:
[4, None, 'foo', 7, 8, (2, 3)]



Ternary expressions in Python

A ternary expression in Python allows you to combine an if-else block that produces
a value into a single line or expression. The syntax for this in Python is:

value = true-expr if condition else false-expr

Here, true-expr and false-expr can be any Python expressions. It has the identical
effect as the more verbose:

if condition:
    value = true-expr
else:
    value = false-expr


This is a more concrete example:

CODE:
x = 5
'Non-negative' if x >= 0 else 'Negative'

OUTPUT:
'Non-negative'