Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
If you have Mac system and created bash scripts so far, there is a fun feature on OS X that you can type say command. say will say the phrase:
say hello channel users

Or let's say you have a bash script that will take a lot of time and at the end of the script your want to hear operation done successfully from your speaker:
say operation done successfully

#osx #say #command #fun
There is a command in osX called open that will open finder for you from within of your terminal.

The great thing about this command is that you can open finder when you have traversed a long path and now you want it to have inside of finder. Go to the desired directory in your terminal using cd then use:
open .

it will open you finder. :)

#osx #open #command #fun #trick
In ls you can list files by using regex pattern as below:
ls data.subtitle_*

* is gready and list all the files that start with data.subtitle_.

Now lets say you want to get count of files which is listed by ls command. For that you can use wc -l as follow:
ls data.subtitle_* | wc -l

Now you shoud the count of files listed by the first command. This method of using 2 commands is piping commands, which is in fact separated by | (pipe)

#linux #sysadmin #ls #wc #command #bash #terminal
What are cat and zcat commands and what is their differences?

cat is used to print the content of a file (in stdout or a file, etc). It's main usage is when you want to search something in the whole file like a log file:
sudo cat /var/log/nginx/access.log | grep --color "SOMETHING TO SEARCH"

zcat command on the other is used to get the content of a .gz compressed text file like apache.log.1.gz:
sudo zcat /var/log/apache/apache.log.1.gz | grep --color "SOMETHING TO SEARCH"

NOTE: grep command is used to search in a file. the symbol | (pipeline) is used to send (pipe) first command result into the second command.

#linux #grep #cat #zcat #command
Executing MySQL statements from a text file:

In MySQL it is possible to put your SQL statements in a file and then tell mysql to read its input from that file. To do so, create a text file text_file that contains the statements you wish to execute. Then invoke mysql as shown here:

shell> mysql db_name < text_file

NOTE: If you place a USE db_name statement as the first statement in the file, it is unnecessary to specify the database name on the command line.


If you are already running mysql, you can execute an SQL script file using the source command or \. command:

mysql> source file_name


#mysql #execute #command #statement
How to monitor network cards on Icinga2? (part-2)

Ok for now we have added the plugin to nagios folder and ran some tests on target server's network interfaces. We need to add a command to Icinga2 to use it in service section of Icinga2. To create a new command create a new file in /etc/icinga2/conf.d/commands/check_nwc_command.conf and with the following content:

object CheckCommand "YOUR_COMMAND_NAME" {
import "plugin-check-command"
command = [ PluginDir + "/check_nwc_health", "--mode", "interface-usage" ]
arguments = {
"-H" = "$address$"
"-C" = "$community$"
"--name" = "$int$"
}
}

In brief it creates a new command called YOUR_COMMAND_NAME that calls the script check_nwc_health with interface-usage argument to get the bandwidth data.


Now we need to use this command in a service. We have to create a new service which will be used in our hosts configuration sections /etc/icinga2/conf.d/services/if_traffic.conf:

apply Service for (display_name => config in host.vars.int) {
import "generic-service"
check_command = "YOUR_COMMAND_NAME"
vars += config
assign where host.vars.int
}

Again in brief the service will be applied on hosts that have a variable section of int in their configuration that we will see a little bit later. YOUR_COMMAND_NAME is the name that we have given in the first part when creating the command.


The final part is to add this service to your desired host. Go to /etc/icinga2/conf.d/hosts and open the file which relates to your host. Host files content start with:

object Host "host-54 (Infra)" {


Add the service like below into your host:

vars.int["YOUR DISPLAY NAME"] = {
int = "Device 1af4:0001 2"
community = "YOUR SERVER COMMUNITY STRING"
}

int is the part that we give the interface name, this should be given from the output of list-interfaces in part-1.

You can go even further like me :) and add these data into Grafana dashboard to have a better understanding of what is happening around you.

#icinga2 #icinga #service #host #command #nagios #interface #network
Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary. It’s the “Command Line Interface Creation Kit”. It’s highly configurable but comes with sensible defaults out of the box.

- http://click.pocoo.org/6/

#python #argparse #click #command_line_interface
In order to run a mySQL query from command line you can use -e parameter:

mysql -u <user> -p -e "select * from schema.table"

#mysql #command #query
There are times you run a command in cronjob in a specific interval. Let's say you run that command every hour.

If your command copies a huge file, or you are doing a heavy task that may take longer than 1 hour sometimes, then you need run-one
command in your arsenal.

run-one: run just one instance at a time of some command and unique set of arguments (useful for cronjobs, eg)


Sample run-one command:

run-one rsync -azP $HOME $USER@example.com:/srv/backup

See more samples here:
- http://manpages.ubuntu.com/manpages/xenial/man1/run-one.1.html


#linux #cronjob #command #run_one #runone
Linux in Docker: Wheezy: "ps: command not found"

Solution:

RUN apt-get update && apt-get install -y procps


#docker #linux #ps #command_not_found #procps