Friday, March 6, 2020

vi editor or vim editor in linux

                                      vi /vim-related Linux commands
 Note: To use vim tool/ Editor, we must install the command:  sudo apt-get install vim 
How to open a file?
vi file_name  ==> to open the file
Ex: vi narendra.log   / vi narendra.txt

How to modify a file's content? 
Open the file & (press insert / i ) to make modifications to that file,    
Do the modifications / add new content to the file, Then 
press (Esc button) then type(:wq! ) to save the file with modifications.

Directly how to go particular line a file?
 vi  <filename>  +13 To go for a particular line(13 / 72) while opening the file with vi editor 

If you don't know the file name exactly but if you remember the file name half, 
then you can open that file using vi *half file name*     Ex: vi *105*

How to move all the files  at a time a  particular Directory 
 use the * symbol (* means all)
Ex: If you move all files to another directory /destination path.
      mv <space> * <space> Destination_path
                           or 
     mv <space> Source_path/* <space> Destination_path

How to set line numbers in a file?
Esc :set nu or Esc :set number ==> command will set line numbers for your code
Esc :set nonu or Esc :set nonumber ==> command will remove line numbers for your code

How to copy multiple lines in a file?
<Number>yy   ==>  To copy the Number of lines in a file 
Example:    yy   ==>  To copy single line in file 
                 4yy   ==>  To copy 4 lines in a file

   p    ==> to paste copied lines at once
 3p    ==> to paste copied lines 3 times

How to perform undo in vi editor?
 u   ==> undo (undo any number of previous actions)

How to delete multiple lines in a file?
<Number>dd  ==> To delete the Number of lines in a file
Example:  dd   ==> To delete a single line (where your cursor is present)
                3dd  ==> To delete 3 lines from the cursor position

How to delete from a specific line to a specific line in a file without opening it.
commandsed <space> -i <space> 'starting line number, ending line numberd' <space> file_name
d means delete
Example : sed -i '567,670d' test_Cfm3Util_5.py

How to Search a particular word or content in a file? 
/pattern — Search forward for "pattern."
/(forward slash
/<word>    *** make sure that word is case sensitive ***
after that, use lower case n to search downwards from the current cursor position (Repeat the search in the same direction)
after that, use Upper case N to search downwards from the current cursor position (Repeat the search in the opposite direction.)
?pattern — Search backwards for "pattern."

<Number> Shift + <   ==> To move the Number of lines towards the left-hand side of a file 
Example:    Shift + <   ==> To move a single line towards the left-hand side of the file
                Shift + <   ==>  To move the Number of lines towards the left-hand side of the file 

<Number> Shift + >   ==> To move the Number of lines towards right-hand side of a file 
Example:    Shift + >   ==> To move a single line towards right-hand side of the file
                Shift + >   ==>  To move the Number of lines towards the right-hand side of the file

 Esc: set paste then paste your content by using p  (to paste precisely how the user copied content with indentation)

How to Replace a string ( single / multiple occurrences) in a file?

  • :s/old/new/ — Replace the first occurrence of "old" with "new" on the current line.
  • :s/old/new/g — Replace all occurrences of "old" with "new" on the current line.
  • :%s/old/new/g — Replace all occurrences of "old" with "new" in the entire file.

Request: If you find this information useful, please provide your valuable comments.

Optional Part 

How to delete lines in vi editor or vim editor in linu

Delete a single line in vi editor

The command to perform the deletion is dd, which is pressing the letter ‘d’ in quick succession twice. It is just like double-clicking.
  1. Go to the line that you want to delete in the editor
  2. Press the ESC key. (This is to make sure that you are in the command mode)
  3. Press dd to delete the line
Delete multiple lines in vi editor
If you know how many lines you want to delete ahead of time, then deleting multiple lines is just as easy as deleting a single line. You will prefix the dd command with the number of lines you want to remove.
  1. Go to the first line that you want to delete
  2. Press the ESC key
  3. Press the number of lines you want to delete, followed by dd (eg. 4dd or 25dd)
The lines that you want to remove should be in succession or consecutive.

Delete lines in a range in vi editor

You can delete lines with in a range, if you know the start and end line numbers. This is a similar case to the one we mentioned in the previous section. The general syntax for deleting a range of lines is:
:[start number],[end number]d
So, to delete all lines between the lines number 111 and 234, you will
  1. Hit ESC key
  2. type :111,234d
  3. Hit Enter

Delete all lines in vi editor

You can use the same syntax to delete all lines in the file. 
There are other ways to empty or truncate a file if you want to, but if you are already in the editor, then this will work just as well.
  1. Hit ESC key
  2. type :1,$d
  3. Hit Enter
The $ character denotes the last line in the file. So the above command will delete lines starting with the first line and ending with the last line.

delete all lines before the current line

Another variation of the range is to delete the lines before the cursor or the current line in the editor. The dot (.) character denotes the current line in the editor.
  1. Hit ESC key
  2. type :1,.-1d
  3. Hit Enter
The above command will remove all lines starting with the first line till the line before the current line.

Delete all lines after the current line

To delete lines after the current line, you can use the dot (.) to denote the current line and dollar ($) to denote the last line.
  1. Hit ESC key
  2. type :.+1,$d
  3. Hit Enter

Delete lines that match a pattern in vi editor

You can also do a search and delete, if you want to delete lines that contain certain words or character sequences. So, if you want to delete all lines in the file that contain the word ‘saloon‘, then
  1. Hit ESC key
  2. type :g/saloon/d
  3. Hit Enter

Delete lines that do not match the pattern

If you want to delete that do not match a specific pattern, then you will need to negate the match before deleting. So, to delete lines that do not have the word ‘saloon’, you will
  1. Hit ESC key
  2. type :v/saloon/d
  3. Hit Enter

Delete all blank lines in a file

To delete all blank lines in a file, you can use a variation of the pattern matching.
  1. Hit ESC key
  2. type :g/^$/d
  3. Hit Enter
Pattern matching in vi editor is a very powerful tool. If you make yourself familiar with the pattern matching commands in vi, then you can use it for all the various different text manipulation that you will come across.
Request: If you find this information useful, please provide your valuable comments.

Pylint in python/ Pylint score testing/ pylint package / Pylint Tutorial

pylint:
Pylint isn't smarter than you are: it may warn you about things that you have dedicated done.
Pylint tries hard to report as few false positives.

How to install pylint
==> Windows / OS X (MAC)     pip install pylint 
==> Ubuntu / Debian OS            sudo apt-get install pylint

from command prompt:
=================
python.exe -m pip install --upgrade pip
python -m pip install pylint

Utilization of pylint:
------------------------
==>  In terminal / command prompt go to the python file path.
==>  python -m pylint operators.py
==>  pylint  file_name.py  # this command will display  how much score your code rated.
                       or
        pylint directory/mymodule.py

Parallel pylint score validation : 
======================
It is possible to speed up the execution of Pylint.
If the running computer has more CPUs than one, then the work for checking all files could be spread across all cores via Pylints's sub-processes.
This functionality is exposed via the -j command-line parameter.
If the provided number is 0, then the total number of CPUs will be autodetected and used.

pylint -j 4 File_name1.py File_name2.py File_name3.py File_name4.py


This will spawn 4 parallel Pylint sub-process, where each provided module will be checked in parallel. Discovered problems by checkers are not displayed immediately. They are shown just after checking a module is complete.
There are some limitations in running checks in parallel in the current implementation. It is not possible to use custom plugins (i.e. --load-plugins option), nor it is not possible to use initialization hooks (i.e. the --init-hook option).

Exit codes

Pylint returns bit-encoded exit codes. If applicable, the table below lists the related stderr stream message output.
exit codemeaningstderr stream message
0no error
1fatal message issued
2error message issued
4warning message issued
8refactor message issued
16convention message issued
32usage error
  • "internal error while receiving resultsfrom child linter" "Error occurred, stopping the linter."
  • "<return of linter.help()>"
  • "Jobs number <#> should be greater than 0"

Thursday, January 23, 2020

Connect to Linux/Ubuntu based Remote desktop & execute commands on it using Python

Python  code useful for following :

==> Connect to remote desktop machine(Linux/ Ubuntu based systems) 
==> Copy the files to from local to remote machine 
==> Copy the files to from remote machine to local  machine
==>Executing commands on Remote machine from local machine
==>To take screenshot of remote machine

Note: I have used config.json file, for future modifications

{
  "modelName": "Desktop",
  "Local_Machine_Path": "C:\\Bhagyasree\\images\\",

  "Linux_Remote_Machine_Path": "/home/Narendra/ntest/",
  "Linux_Remote_Machine_IP_Addr": "192.10.14.3",
  "Linux_Remote_Machine_User_Name": "narendrab",
  "Linux_Remote_Machine_Pass_word": "Nanna@786",
}

ip_addr = Linux_Remote_Machine_IP_Addr
User_Name = Linux_Remote_Machine_User_Name
Pass_word = Linux_Remote_Machine_Pass_word
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print("Please wait while connecting to host machine/Remote server")
ssh_client.connect(ip_addr, username=User_Name, password=Pass_word)
print("Please wait, executing command on remote machine")

remote_path = Linux_Remote_Machine_Path
local_path = Local_Machine_Path

path = Linux_Remote_Machine_Path
# image_name = "Desktop"

image_name = "Desktop"
cmd1 = "import -window root -display :0 " + path + "{}.png".format(image_name)
stdin, stdout, stderr = ssh_client.exec_command(cmd1)
host = Local_Machine_Path
target = Linux_Remote_Machine_Path
image_name = "Desktop.png"sftp = ssh_client.open_sftp()
sftp.get(target + image_name, host + image_name)
print("file successfully copied")
ssh_client.close()

How to install OpenSSL on CentOS?

How to install latest version of OpenSSL on CentOS?

Step 1:  mkdir Openssl
Step 2:  cd Openssl/
Step 3:  wget https://www.openssl.org/source/old/1.0.2/openssl-1.0.2t.tar.gz
Step 4:  tar -zxvf openssl-1.0.2t.tar.gz
Step 5:  cd openssl-1.0.2t/
Step 6:  ./config --prefix=/usr/ -fPIC -pthread -shared
Step 7:  make all
Step 8:  make install
Step 9:  openssl version