Getting the caller method details using Java

1
2
3
4
5
6
7
8
9
10
 public static String getCallerClassName() {
        StackTraceElement[] stElements = Thread.currentThread().getStackTrace();
        for (int i=1; i<stElements.length; i++) {
            StackTraceElement ste = stElements[i];
            if (!ste.getClassName().equals(YOUR_CURRENTCLASS.class.getName()) && ste.getClassName().indexOf("java.lang.Thread")!=0) {
                return ste.getClassName()+"."+ ste.getMethodName()+"()";
            }
        }
        return null;
     }
Posted in java | Comments Off on Getting the caller method details using Java

Compare File Permission Recussively on Linux Directories

Scan

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/perl

use File::Find;

my $directory1 = '/root/rpmbuild/RPMSX';
my $directory2 = '/root/rpmbuild/RPMSX.bak';

find(\&hashfiles, $directory1);

sub hashfiles {
  my $file1 = $File::Find::name;
  (my $file2 = $file1) =~ s/^$directory1/$directory2/;
        return(0) if (! -f $file2) ;
  my $mode1 = (stat($file1))[2] ;
  my $mode2 = (stat($file2))[2] ;

  my $uid1 = (stat($file1))[4] ;
  my $uid2 = (stat($file2))[4] ;

  print "Permissions for $file1 and $file2 are not the same\n" if ( $mode1 != $mode2 );
  print "Ownership for $file1 and $file2 are not the same\n" if ( $uid1 != $uid2 );
}
Posted in Linux | Comments Off on Compare File Permission Recussively on Linux Directories

Linux find files and total their size

1
find ./ -type f -newerct "1 May 2015" ! -newerct "1 Jul 2015" -print0 | du --files0-from=- -hc| tail -n1
Posted in Bash, Linux | Comments Off on Linux find files and total their size

Wireshark filters

Filter by ip dst or source using wildcard on the last 3 digits:

(ip.dst == 192.168.0.0/24) || (ip.src == 192.168.0.0/24)

Posted in Linux | Comments Off on Wireshark filters

MSBuild common errors and how to fix them

Building with MSBuild produced an error or warning

Run MSBuild with (d)etailed verbose mode and capture the output to a file:

1
msbuild someproject.csproj /t:Clean;Build;Transfer /p:OutputPath=bin\autobuild;BuildNumber=-1;Configuration=Debug /v:d >build.txt

Open build.txt and search for the MSBxxxx error or warning code. For example:

1
2
Consider app.config remapping of assembly "DotNetOpenAuth.AspNet, Culture=neutral, PublicKeyToken=2780ccd10d57b246" from Version "4.0.0.0" [] to Version "4.1.0.0" [C:\somedir\bin\DotNetOpenAuth.AspNet.dll] to solve conflict and get rid of warning.
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3247: Found conflicts between different versions of the same dependent assembly.

If it doesn’t already exist, add an App.config file with the following content:

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="DotNetOpenAuth.AspNet" publicKeyToken="2780ccd10d57b246" />
                <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

Also change your .csproj to include the App.config file:

1
2
3
<ItemGroup>
    <None Include="App.config" />
</ItemGroup>

A few useful additions.

This fix applies to classic .NET Framework, not .NET Core / .NET 5+. Binding redirects in App.config are a .NET Framework concept. Modern .NET (Core 3.x, 5, 6, 7, 8, 9, …) resolves assembly versions through *.deps.json and unifies them automatically — you don’t write binding redirects, and MSB3247 generally doesn’t show up the same way. If your project is <Project Sdk=”Microsoft.NET.Sdk”> rather than the legacy XML format, you’re on modern .NET and this post doesn’t apply. 🧭

A better way to inspect a build than reading build.txt. Pass /bl (or -bl) to MSBuild instead of (or in addition to) /v:d:

1
msbuild someproject.csproj /t:Build /bl

This produces an msbuild.binlog file with the full structured build log. Open it in the free MSBuild Structured Log Viewer and you get a navigable tree of every target, task, and property, with full search — a vastly better experience than grepping through a text file. /v:diag is the next step up in plain-text verbosity if you don’t want a binlog (and is what the docs call “diagnostic” level).

Two other MSBuild errors that come up a lot.

MSB3644 — “Reference assemblies for .NETFramework,Version=vX.X were not found”. Classic on a fresh build agent. The targeting pack for the .NET Framework version your project asks for isn’t installed. Two fixes: install the matching .NET Framework Targeting Pack via the Visual Studio Installer (Individual components → “.NET Framework X.X targeting pack”), or downgrade <TargetFrameworkVersion> in your .csproj to a version that is installed.

MSB4018 — “The X task failed unexpectedly”. The MSBuild equivalent of “something blew up, here’s the stack trace”. Usually one of: a custom task that crashed, an SDK version mismatch (your global.json pins an SDK that isn’t on the machine), or a corrupted obj/ folder from an interrupted build. The standard checklist: delete bin/ and obj/, run dotnet restore (or nuget restore for legacy), confirm dotnet –version matches what global.json demands, then rebuild. If it still fails, the binlog from the previous tip will show you which task threw — that’s nearly always the real clue. 🛠️

Posted in C# | Tagged | Comments Off on MSBuild common errors and how to fix them

Allowing a linux/unix user all sudo access without password

/etc/sudoers

yourusername ALL=(ALL) NOPASSWD:ALL

Posted in Linux | Tagged , | Comments Off on Allowing a linux/unix user all sudo access without password

Solaris Notes

Extracting a solaris package without installing it.

#http://serverfault.com/questions/287469/extract-files-out-of-solaris-pkg-file-without-installing
pkgtrans filename.pkg /home/user/temporary_package_prefix

Posted in Operating System | Comments Off on Solaris Notes

Find with xargs

Search all files under current directory, look for xml node ‘‘, copy and print that node. Send the output to a file.

find . |xargs -n1 xmlstarlet sel -t -c “//processorInfo[@ruleType=’store’]” 2>/dev/null > /cygdrive/b/allstore.txt

Posted in Linux | Comments Off on Find with xargs

XMLStartlet – Command line xml queries

xmlstarlet sel -t -c “//YOUR_NODE_ELEMENT_TAG_NAME[@ATTRIBUTE_NAME=’ATTRIBUTE_VALUE’]” YOUR_XML_FILE.xml

Posted in Linux | Comments Off on XMLStartlet – Command line xml queries

Bash Scripting templates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# ==========================
# Bash tips
# ==========================

# Run bash by
$ /bin/bash scriptname.sh
$ /bin/bash scriptname.sh +x  # debug mode

# Or put the next line on the 1st line of your file.
#!/bin/bash -x

## Double round braket for integer
if (( $MYINT < 10 ]]   #or: =
## Double square braket for string
if [[ $USER = "ron" ]]   #or: !=
   then
      echo "Hello ron"
   else
      echo "Hello Stranger"
fi

$ date +%x   # shows only the date 07/02/13 depending on locale, currently en_CA
$ LANG=en_US
$ date +%x   # shows only the date 02/07/13


# ==========================
# vi tips
# ==========================

# Using normal mode
# Delete line at the cursor
#    type dd
# Insert at cursor
#    type i
# Insert after the cursor (append)
#    type a
k
J
o

# To bring in (read in) the content of an extrnal file into your current file
:r ~/snippets/if

# To read in a result of an executable, such as date use the :r!
:r!date
:r!date "+\%x"

# Macro normal mode
:map
# Macro insert mode
:map!

# Macro F2 to insert text: "#This file was created on 2014-05-03"
:map <F2> i#This file was created on <ESC>:r!date "+\%x" <ESC> kJ

put the line above to .vimrc
map <F2> i#!/bin/bash<ESC>
map <F3> o#This file was created on <ESC>:r!date "+\%x" <ESC> kJ
Posted in Linux | Comments Off on Bash Scripting templates