Wednesday, November 2, 2016

Quick Connect through Jump host via proxy

When we have hosts shared with ssh-keys, from home network to office network via proxy, then jump host.

This simple ssh/config saves lots of manual work


Host jumphost
        HostName jumphost.my.home.net
        HostKeyAlgorithms ssh-dss
        ForwardAgent yes
        LocalForward 3128 proxyhost.my.home.net:3128
        User sanjeev
        StrictHostKeyChecking no
        ConnectTimeout 30
        KeepAlive no

Host *.my.office.net
        HostName %h
        User sanjeev
        ForwardAgent yes
        ProxyCommand ssh jumphost nc -w 120 %h %p
        StrictHostKeyChecking no
        ConnectTimeout 30
        KeepAlive no

Now from your laptop you can do seamless connection, for any ssh activities


 ssh host1.my.office.net "cat watch.status"


This goes through  proxyhost to connect to office network, login to jumphost and connect to office network host, cat the file watch.status from host1, in office network :)


Friday, October 21, 2016

process start time

For long I had been looking out for process starttime and elapsed time, its important for long stuck process

Refer: http://linuxcommand.org/man_pages/ps1.html

so here I got it..





$ ps wwf -o pid,fuser,lstart,etime,pcpu,pmem,args  -p 2128 

PID  FUSER   STARTED                   ELAPSED       %CPU  %MEM   COMMAND
2128 root    Wed Oct 19 22:00:31 2016  1-06:28:14    0.0   0.2    /sbin/dhclient -1 -v -pf /run/dhclient.enp0s8.pid -lf /var/lib/dhcp/dhclient.enp0s8.leases -I –df /var/lib/dhcp/dhclient6.enp0s8.leasesenp0s8                                                                                                                                        
$ date
Fri Oct 21 04:31:18 UTC 2016

Monday, June 13, 2016

Quickly deleting large file

Deleting larger file takes time, but when we dont want the file, why shout be their waiting time to delete it.

These files i am deleting

-rw-r--r--  1 sanjeev.tripurari  1114871328  24931090000 Jun 13 17:28 words3.txt
-rw-r--r--  1 sanjeev.tripurari  1114871328  24931090000 Jun 13 17:28 words2.txt

Normal deletion

$ time rm -fv words2.txt 
words2.txt

real 0m0.672s
user 0m0.001s
sys 0m0.080s


copy /dev/null will make it zerobyte quickly and then delete 

$ time cp -v /dev/null words3.txt 
/dev/null -> words3.txt

real 0m0.321s
user 0m0.001s
sys 0m0.294s


$ time rm -fv words3.txt 
words3.txt

real 0m0.006s
user 0m0.001s
sys 0m0.003s



Sunday, June 12, 2016

quick share code snippet

We often need to share the code snippet or some text file for review or comments, attaching to chat window or mail take time.

we can run netcat and expose the code on the web and do the quick correction,

$ cat poweroff.sh |nc   -l   0.0.0.0 8088

yes though it just stays for one instance, it saves time of getting on to another application,

the remote user can now use curl/links

function retun value

Have been using bash for long, and never expected to have return value, as the function was written just do the repetative job

But when we need a bash function to return value, be sure that it just has one echo which is for return, any other display will be returned otherwise

here how it is..



$ cat poweroff.sh 


#!/bin/bash

##
## Program to test return value from bash script..
##

function power(){

#
# power(3,2)=9
# power(5,3)=125
#
local fv=$1
local sv=$2

local p=1


for i in ` seq 1 $sv ` ; do
p=` expr $p \* $fv `
done

echo $p
}

v1=${1:-3}
v2=${2:-2}

p1=$(power $v1 $v2)

echo "power($v1,$v2)=$p1"

# Done.




$ ./poweroff.sh 

power(3,2)=9


Tuesday, May 17, 2016

Directory listing with for loop

Often had to check the files between directories if name matches or copy the missing ones, in cases of taking backup.

Most of the sites come up with the while loop, but i found us `for` loop
$ ls -l
total 1392
-rw-------@ 1 sanjeev  users  277935 May 17 18:21 Jenkins_Job_packaging.pdf
-rw-------@ 1 sanjeev  users  227774 May 17 18:21 Pipeline Based Alert and Notification.docx
-rw-r--r--@ 1 sanjeev  users    1324 May 17 18:21 SieveOfAtkin.js
-rw-r--r--  1 sanjeev  users    1301 May 17 18:21 VPN (Cisco IPSec).networkConnect
-rw-------@ 1 sanjeev  users  187319 May 17 18:21 algo.docx
-rw-r--r--  1 sanjeev  users    3797 May 17 18:21 bill.feb

-rw-r--r--  1 sanjeev  users      65 May 17 18:22 i

With for loop it was bad output
j=0
for i in `ls`; do
let j=j+1
echo "$j: $i"
done 

$ bash i

1: Jenkins_Job_packaging.pdf
2: Pipeline
3: Based
4: Alert
5: and
6: Notification.docx
7: SieveOfAtkin.js
8: VPN
9: (Cisco
10: IPSec).networkConnect
11: algo.docx
12: bill.feb

13: i

The spaces in filename is used as separators

$IFS
internal field separator
This variable determines how Bash recognizes fields, or word boundaries, when it interprets character strings.

Improved for loop, using IFS
IFS=$'\n'
j=0
for i in `ls`; do
let j=j+1
echo "$j: $i"

done 


And finally we get the correct filenames

$ bash i

1: Jenkins_Job_packaging.pdf
2: Pipeline Based Alert and Notification.docx
3: SieveOfAtkin.js
4: VPN (Cisco IPSec).networkConnect
5: algo.docx
6: bill.feb
7: i



Well IFS is well documented, but not popularly used .

Thursday, March 3, 2016

Emoji on shell prompt

Hey, are you bored with your text terminal..

so here is mine

💻  :t sanjeev.tripurari 👍 $ date
Thu Mar  3 15:23:34 IST 2016
💻  :t sanjeev.tripurari 👍 $ who am i
sanjeev.tripurari ttys003  Mar  2 10:59 

💻  :t sanjeev.tripurari 👍 $ 

you see the emoji's you can also have them and there are lots around, just check if your terminal supports

if you get..

 echo; echo -e "\xF0\x9F\x91\x8D"; echo; echo

👍



then you are on the way pick one of your choice, ignore other echo; this is the one
echo -e "\xF0\x9F\x91\x8D"

if you get 👍  then pick  for yourself, remember to pick utf8 hex codes


http://apps.timwhitlock.info/emoji/tables/unicode
http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=1F4BB%09&mode=hex


Wednesday, February 10, 2016

on fly Variable substitution

This is quite old case I had been using for long, but never blogged as it has security risk if not handled properly use of eval.

Using wide range of hosts services on distributed cluster it can be helpful.

Say we have list of hosts running different services, and you need to connect for particular reason the

Say we alias the list of hosts running services on different clusters,
  • e.g. cluster: 
    • ALPHA, BETA
  • services: 
    • FALCON, OOZIE


so we have aliases, host and services
ALPHA_FALCON_HOST=alpha001.my.home.net
ALPHA_OOZIE_HOST= alpha002.my.home.net
BETA_FALCON_HOST=beta1001.my.home.net
BETA_OOZIE_HOST=beta402.my.home.net


RANGE_HOST=beta402.my.home.net
we have aliases, service with port to access the service correctly
OOZIE_URL=’http://$OOZIE_HOST:11000/oozie’FALCON_URL=’http://$FALCON_HOST:15000’ 


Now we want to access BETA cluster OOZIE_HOST, and set the URL accordingly
setting two set of variables on fly to access there service,
OOZIE_HOST=$BETA_OOZIE_HOSTOOZIE_URL=`echo "$(eval echo $OOZIE_URL )"`


e.g. might be bad if you have few services,
but its very helpful if we have large cluster farm, and have proper aliasing


Wednesday, January 6, 2016

Code snippets on gist

Hi..

I will be further putting my code on https://gist.github.com/sanjeevtripurari

Blog will have just details, and link for the code..

now blogging seems boring will do more coding stuff..

:)