Thursday, September 1, 2016

Using ruby array insert

##When I have to insert numbers that are omitted between numbers###
##For example, the file contents are as follows.

00:data
04:data
05:data
06:data
07:data
08:data
09:data
10:data
13:data
14:data
15:data
16:data
19:data

I need to insert 01,02,03 between 00 and 04. And I also need to insert 11,12.

On linux box, I may have to use awk sed or perl. But for me ruby is much easier.
The code is as follows.


[root@redhat2 ~]# cat t.rb
#!/usr/bin/env ruby

#Function to less than 9 insert method differently
def more_than_9(a,n)
    if n < 9
       a.insert(n,"0#{n}:data")
    else
       a.insert(n,"#{n}:data")
    end
end

#File open and into array
a=File.new("./test.csv").read.split("\n")
s=a.size
i=0
 while i < (s-1)
    if (a[i+1].split(":")[0].to_i - a[i].split(":")[0].to_i) > 1
     nx=a[i+1].split(":")[0].to_i

     while i+1 < nx
      n=i+1
       more_than_9(a,n)
      i=n
     end
    end
    i=i+1
 end

#Puts results
puts a

#So, the result of executing this script is as follows.
[root@redhat2 ~]# ruby t.rb
00:data
01:data
02:data
03:data
04:data
05:data
06:data
07:data
08:data
09:data
10:data
11:data
12:data
13:data
14:data
15:data
16:data
19:data

# #################################3

Finding same file size in a current directory(ruby)-dirty script

#!/usr/bin/env ruby
#This is dirty ruby script but function well though.
#More with " https://github.com/ohyoungjooung2/ruby_scripts/blob/master/same_file.rb"
#^^; Have fun.

files=`find . -type f | xargs stat -c "%s %n"`
s=files.split("\n")
#p s

sk=[]
sv=[]

i=0
s_size=s.size
while i < s_size
  sk<<s[i].split(" ")[1]
  sv<<s[i].split(" ")[0]
  i+=1
end
# files name as a  array sk
#p sk
#puts ""
# files size as a array sv
#p sv


#Creating hash
j=0
h={}
#puts "sv size is #{sv.size}"
#puts "s_size is #{s_size}"
while j < sv.size
  #puts j
  h[sk[j]]=sv[j]
  j+=1
end

#Putting h hash
#puts h

z=0

#Getting uniq values of each hash value
u=h.values.uniq
while z < h.size
# puts z
 same_files=h.select { |k,v| v==u[z] }
 if same_files.size >= 2
   fs=0
   while fs < same_files.size
    puts  "same_file size with #{u[z]} bytes are #{same_files.keys[fs]}"
    fs+=1
   end
 end
 z+=1
end



young@ubuntu-16:~/test$ ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
young@ubuntu-16:~/test$ chmod 700 same_file.rb
young@ubuntu-16:~/test$ ./same_file.rb
same_file size with 8941 bytes are ./1
same_file size with 8941 bytes are ./2
same_file size with 548 bytes are ./apparmor.d/abstractions/ibus
same_file size with 548 bytes are ./f.sh
same_file size with 1748 bytes are ./tttt/iiii
same_file size with 1748 bytes are ./tttt/inputrc
same_file size with 1748 bytes are ./inputrc
same_file size with 1654 bytes are ./tttt/pass
same_file size with 1654 bytes are ./tttt/passwd
same_file size with 1654 bytes are ./pass
same_file size with 1654 bytes are ./passwd
same_file size with 1654 bytes are ./ssap
same_file size with 26 bytes are ./tttt/iss
same_file size with 26 bytes are ./tttt/issue2
same_file size with 26 bytes are ./issue
same_file size with 364 bytes are ./tttt/rrrr
same_file size with 364 bytes are ./rc.local
same_file size with 738 bytes are ./same_file.rb
same_file size with 738 bytes are ./groff/man.local

Monday, August 29, 2016

Finding same file size in a directory

young@ubuntu-16:~/test$ cat find_same_size.sh
#!/usr/bin/env bash
#set -x
#This is small script can find same size of files.
find_same_size(){

if [[ -z $1 || ! -d $1 ]]
then
 echo "Usage $0 directory_name" ;
 exit $?
else
dir_name=$1;
echo "current directory is $1"



for i in $(find $dir_name -type f); do
  ls -fl $i
done | awk '{f=""
        if(NF>9)for(i=9;i<=NF;i++)f=f?f" "$i:$i; else f=$9;
        if(a[$5]){ a[$5]=a[$5]"\n"f; b[$5]++;} else a[$5]=f} END{for(x in b)print a[x] }' | xargs stat -c "%s  %n" #For just list files
 fi
}




find_same_size $1




#Example usage
young@ubuntu-16:~/test$ bash find_same_size.sh tttt/
current directory is tttt/
26  tttt/iss
26  tttt/issue2
1654  tttt/pass
1654  tttt/passwd

#We can delete duplicated files by using like below.
#First delete line that starts with alphabet. Only find the line that begins with numeric. and
#then with md5sum command if the files are really same or not
young@ubuntu-16:~/test$ bash find_same_size.sh tttt/ | awk '{ if($1 !~ /^([[:alpha:]])+/) print $2}' | xargs md5sum
1e7672cbc2f76c3e9daad0e290e711b9  tttt/iss
1e7672cbc2f76c3e9daad0e290e711b9  tttt/issue2
cc44dec6bfd51d296c89d55e7c38b933  tttt/pass
cc44dec6bfd51d296c89d55e7c38b933  tttt/passwd







#-w32 means 32bytes; -d deletes duplicated ones that created most recently among same md5sums. Finally with xargs we can do rm -vf
young@ubuntu-16:~/test$ bash find_same_size.sh tttt/ | awk '{ if($1 !~ /^([[:alpha:]])+/) print $2}' | xargs md5sum | uniq -w32 -d | xargs rm -vf
removed 'tttt/iss'
removed 'tttt/pass'


Wednesday, August 17, 2016

Bash-sum of each array-Debug on

#!/usr/bin/env bash
#This script is very simple example that sum each array numbers.
#debug on
set -x
var="15899"
echo "first is ${var:0:1}"
echo "second is ${var:1:1}"
echo "third is ${var:2:1}"
echo "fourth is ${var:3:1}"
echo "fifth is ${var:4:1}"

sum=0
start=0
while (( $start < ${#var} ))
do
   sum=$(( $sum+${var:start++:1} ))
done
echo -e "Total sum is \e[;35m $sum \e[;0m"



#Just testing for fun.
young@ubuntu-16:~$ bash sum.sh
+ var=15899
+ echo 'first is 1'
first is 1
+ echo 'second is 5'
second is 5
+ echo 'third is 8'
third is 8
+ echo 'fourth is 9'
fourth is 9
+ echo 'fifth is 9'
fifth is 9
+ sum=0
+ start=0
+ ((  0 < 5  ))
+ sum=1
+ ((  1 < 5  ))
+ sum=6
+ ((  2 < 5  ))
+ sum=14
+ ((  3 < 5  ))
+ sum=23
+ ((  4 < 5  ))
+ sum=32
+ ((  5 < 5  ))
+ echo -e 'Total sum is  \e[;35m 32 \e[;0m'
Total sum is   32
young@ubuntu-16:~$

Sunday, February 14, 2016

Subversion on Centos7.

Subversion on Centos7.
Related: SSH, firewall-cmd, selinux.

1. Install svn subversion
[root@ct7-1 ~]# yum -y install subversion


2. Create repo
[root@ct7-1 ~]# mkdir -p /var/svn/repos/roller
[root@ct7-1 ~]# svnadmin create /var/svn/repos/roller
[root@ct7-1 ~]# svn mkdir file:///var/svn/repos/roller/trunk -m "create"

Committed revision 1.
[root@ct7-1 ~]# svn mkdir file:///var/svn/repos/roller/branches -m "create"

Committed revision 2.
[root@ct7-1 ~]# svn mkdir file:///var/svn/repos/roller/tags -m "create"

Committed revision 3.
[root@ct7-1 ~]#



3. Importing source
[root@ct7-1 roller]# wget https://github.com/apache/roller/archive/trunk.zip

[root@ct7-1 roller]# unzip trunk.zip

#Importing
[root@ct7-1 roller]# svn import /home/roller/roller-trunk file:///var/svn/repos/roller/trunk -m "initial import of roller"


4. Start svnserve daemon
[root@ct7-1 roller]# systemctl start svnserve
[root@ct7-1 roller]# ps -ef | grep svnserve
root     24434     1  0 18:40 ?        00:00:00 /usr/bin/svnserve --daemon --pid-file=/run/svnserve/svnserve.pid -r /var/svn
root     24443 22935  0 18:41 pts/0    00:00:00 grep --color=auto svnserve
[root@ct7-1 roller]#

root@ct7-1 roller]# netstat -tpln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:3690            0.0.0.0:*               LISTEN      24434/svnserve   

#From other server(ubuntu14)
192.168.56.103 is centos7 svnserve.
young@ubuntu14:~$ telnet 192.168.56.103 3690
Trying 192.168.56.103...
telnet: Unable to connect to remote host: No route to host
#Below is normal
young@ubuntu14:~$ telnet 192.168.56.101 3690
Trying 192.168.56.101...
Connected to 192.168.56.101.
Escape character is '^]'.
( success ( 2 2 ( ) ( edit-pipeline svndiff1 absent-entries commit-revprops depth log-revprops atomic-revprops partial-replay ) ) ) ^CConnection closed by foreign host.


#Gotta check firewall or etc.

5.firewall-cmd --add-port number/protocol
[root@ct7-1 roller]# firewall-cmd --add-port 3690/tcp
success
[root@ct7-1 roller]#
[root@ct7-1 roller]# firewall-cmd --list-all
public (default, active)
  interfaces: enp0s3 enp0s8
  sources:
  services: dhcpv6-client ssh
  ports: 3690/tcp
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:
^^ It seems very O.k

young@ubuntu14:~/roller_project$ svn list svn://192.168.56.103/repos/roller
svn: E000013: Unable to connect to a repository at URL 'svn://192.168.56.103/repos/roller'
svn: E000013: Can't open file '/var/svn/repos/roller/format': Permission denied

#This might be a selinux. So change from enforcing to permissive by issuing command "vi /etc/sysconfig/selinux"
[root@ct7-1 repos]# setenforce 0 #This command will cause immediate effect.
#To keep setenforce 0, just edit like belows.

 [root@ct7-1 repos]# cat /etc/sysconfig/selinux

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
# Only change from enforcing to permissive.
SELINUX=permissive
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted


#Now finally, we can list and checkout.
young@ubuntu14:~/roller_project$ svn list svn://192.168.56.103/repos/roller
branches/
tags/
trunk/



young@ubuntu14:~/roller_project$ svn co svn://192.168.56.103/repos/roller


#Using ssh
young@ubuntu14:~/test$ svn list svn+ssh://young@192.168.56.103/var/svn/repos/roller
young@192.168.56.103's password:
branches/
tags/
trunk/
young@ubuntu14:~/test$

young@ubuntu14:~/test$ svn co svn+ssh://young@192.168.56.103/var/svn/repos/roller


#Without password, use public key.
young@ubuntu14:~/test$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/young/.ssh/id_rsa):
/home/young/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/young/.ssh/id_rsa.
Your public key has been saved in /home/young/.ssh/id_rsa.pub.
The key fingerprint is:
a7:54:2e:08:5a:9d:fc:c8:9d:7e:18:1d:24:9a:56:a6 young@ubuntu14
The key's randomart image is:
+--[ RSA 2048]----+
|        + .      |
|     o B o       |
|    o E   o      |
|   o + = = .     |
|  .   + S +      |
|       o *       |
|        + .      |
|         .       |
|                 |
+-----------------+
young@ubuntu14:~/test$ ls
roller
young@ubuntu14:~/test$ scp /home/young/.ssh/id_rsa.pub young@192.168.56.103:/home/young/
young@192.168.56.103's password:
id_rsa.pub                                                                                             100%  396     0.4KB/s   00:00   
young@ubuntu14:~/test$



[young@ct7-1 .ssh]$ mv ~/id_rsa.pub ./authorized_keys
[young@ct7-1 .ssh]$ ll
total 4
-rw-r--r--. 1 young young 396 Feb 14 19:33 authorized_keys

[root@ct7-1 young]# chmod 700 .ssh/

#Login Test
young@ubuntu14:~$ ssh young@192.168.56.103
Last login: Sun Feb 14 19:46:55 2016 from 192.168.56.102
[young@ct7-1 ~]$



#Now without password.
young@ubuntu14:~$ svn list svn+ssh://young@192.168.56.103/var/svn/repos/roller
branches/
tags/
trunk/
young@ubuntu14:~$


young@ubuntu14:~/co_test$ svn co svn+ssh://young@192.168.56.103/var/svn/repos/roller




#It is very happy when things go smoothly. ^^;