Archive

Articles taggués ‘linux’

2 awesome open source apps to share your terminal over the web

24/12/2023 Comments off

Want to share your terminal over the web for demo, learning or collaboration purpose? Try these two applications to share your terminal as a web application.

Please note that accepting input from remote clients is dangerous for most commands. When you need interaction with the TTY for some reasons, consider starting following tools with tmux or GNU Screen and run your command on it. Use following tools with trusted parties or inside VM. Let us see how to install and use gotty and ttyd on a Unix-like system.

1. gotty

GoTTY is a simple command line tool that turns your CLI tools into web applications. It is written in go programming language.

Installation

You can install gotty on macOS using the brew command:
$ brew install yudai/gotty/gotty

Sample outputs:

Updating Homebrew...
==> Tapping yudai/gotty Cloning into '/usr/local/Homebrew/Library/Taps/yudai/homebrew-gotty'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 1), reused 2 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
Tapped 1 formula (30 files, 22.7KB)
==> Installing gotty from yudai/gotty
==> Downloading https://github.com/yudai/gotty/releases/download/v1.0.1/gotty_darwin_amd64.tar.gz
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/40808571/c401bd34-7bd9-11e7-8
######################################################################## 100.0%
==> Caveats GoTTY! ==> Summary
? /usr/local/Cellar/gotty/v1.0.1: 3 files, 8.2MB, built in 1 minute

Another option for Linux or Unix like system is to type the following command if you have a go language dev setup installed:

$ go get github.com/yudai/gotty

Usage

The syntax is:

gotty command
$ gotty htop

Sample outputs:

2017/09/23 22:31:19 Server is starting with command: htop
2017/09/23 22:31:19 URL: http://127.0.0.1:8080/
2017/09/23 22:31:19 URL: http://[::1]:8080/
2017/09/23 22:31:19 URL: http://[fe80::1]:8080/
2017/09/23 22:31:19 URL: http://[fe80::1c3a:3312:311b:cca4]:8080/
2017/09/23 22:31:19 URL: http://192.168.225.106:8080/
2017/09/23 22:31:19 URL: http://[fe80::6c1b:58ff:fe8a:4e6e]:8080/
2017/09/23 22:31:19 URL: http://[fe80::526a:bad6:960f:369f]:8080/
2017/09/23 22:31:19 URL: http://10.8.0.2:8080/

Fire a browser and type the url:
http://127.0.0.1:8080/
OR from another computer in your LAN/VLAN:
http://192.168.225.106:8080/

Sample outputs:

Gif 01: gotty in action

Gif 01: gotty in action



For more info and documentation see gotty home page.

2. ttyd

ttyd is a simple command-line tool for sharing terminal over the web, inspired by GoTTY. It is built on top of Libwebsockets with C for speed. Works with macOS, Linux, FreeBSD, OpenWrt/LEDE, and MS-Windows oses.

Installation

If you are using macOS, run the following brew command:
$ brew install ttyd
Sample outputs:

Viveks-MacBook-Pro:~ veryv$ brew install ttyd
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
No changes to formulae.
 
==> Installing dependencies for ttyd: json-c, libwebsockets
==> Installing ttyd dependency: json-c
==> Downloading https://homebrew.bintray.com/bottles/json-c-0.12.1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring json-c-0.12.1.sierra.bottle.tar.gz
?  /usr/local/Cellar/json-c/0.12.1: 27 files, 156.2KB
==> Installing ttyd dependency: libwebsockets
==> Downloading https://homebrew.bintray.com/bottles/libwebsockets-2.2.1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libwebsockets-2.2.1.sierra.bottle.tar.gz
?  /usr/local/Cellar/libwebsockets/2.2.1: 29 files, 4.2MB
==> Installing ttyd
==> Downloading https://homebrew.bintray.com/bottles/ttyd-1.3.3.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring ttyd-1.3.3.sierra.bottle.tar.gz
?  /usr/local/Cellar/ttyd/1.3.3: 6 files, 282.6KB

If you are using a Debian/Ubuntu Linux, run:
$ sudo apt-get install -y software-properties-common
$ sudo add-apt-repository ppa:tsl0922/ttyd-dev
$ sudo apt-get update
$ sudo apt-get install ttyd

Usage

The syntax is:
$ ttyd command
$ ttyd bash

Sample outputs:

[2017/09/23 22:57:32:9322] NOTICE: ttyd 1.3.3 (libwebsockets 2.2.0)
[2017/09/23 22:57:32:9323] NOTICE: tty configuration:
[2017/09/23 22:57:32:9323] NOTICE:   start command: bash
[2017/09/23 22:57:32:9323] NOTICE:   reconnect timeout: 10s
[2017/09/23 22:57:32:9323] NOTICE:   close signal: SIGHUP (1)
[2017/09/23 22:57:32:9323] NOTICE: Initial logging level 7
[2017/09/23 22:57:32:9323] NOTICE: Libwebsockets version: 2.2.0 brew@Sierra.local-
[2017/09/23 22:57:32:9323] NOTICE: IPV6 not compiled in
[2017/09/23 22:57:32:9323] NOTICE: libev support not compiled in
[2017/09/23 22:57:32:9323] NOTICE: libuv support not compiled in
[2017/09/23 22:57:32:9324] NOTICE:  Threads: 1 each 256 fds
[2017/09/23 22:57:32:9325] NOTICE:  mem: platform fd map:  2048 bytes
[2017/09/23 22:57:32:9325] NOTICE:  Compiled with OpenSSL support
[2017/09/23 22:57:32:9325] NOTICE:  SSL disabled: no LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT
[2017/09/23 22:57:32:9326] NOTICE: Creating Vhost 'default' port 7681, 2 protocols, IPv6 off
[2017/09/23 22:57:32:9337] NOTICE:  Listening on port 7681
[2017/09/23 22:57:32:9338] NOTICE:  mem: per-conn:          568 bytes + protocol rx buf
[2017/09/23 22:57:32:9339] NOTICE:  canonical_hostname = Viveks-MacBook-Pro.local

Fire a web browser and type url:
127.0.0.1:7681
Sample session:

Gif. 02: bash-ttyd demo



For more info and docs see project home page.

 
Categories: Logiciel, Système Tags: ,

Linux: 20 Iptables Examples For New SysAdmins

23/12/2023 Comments off

According to the official project site:

netfilter is a set of hooks inside the Linux kernel that allows kernel modules to register callback functions with the network stack. A registered callback function is then called back for every packet that traverses the respective hook within the network stack.

This Linux based firewall is controlled by the program called iptables to handles filtering for IPv4, and ip6tables handles filtering for IPv6. I strongly recommend that you first read our quick tutorial that explains how to configure a host-based firewall called Netfilter (iptables) under CentOS / RHEL / Fedora / Redhat Enterprise Linux. This post lists most simple iptables solutions required by a new Linux user to secure his or her Linux operating system from intruders. Lire la suite…

30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X

23/12/2023 Comments off

An alias is nothing but the shortcut to commands. The alias command allows the user to launch any command or group of commands (including options and filenames) by entering a single word. Use alias command to display a list of all defined aliases. You can add user-defined aliases to ~/.bashrcfile. You can cut down typing time with these aliases, work smartly, and increase productivity at the command prompt.

More about aliases

The general syntax for the alias command for the bash shell is as follows:

Task: List aliases

Type the following command:

alias

Sample outputs:

alias ..='cd ..'
alias amazonbackup='s3backup'
alias apt-get='sudo apt-get'
...

By default alias command shows a list of aliases that are defined for the current user.

Lire la suite…

Categories: Système Tags: , , ,

How to check the file size in Linux/Unix bash shell scripting

22/12/2023 Comments off

 

How to check file size in unix using wc command

The wc command shows the number of lines, words, and bytes contained in file. The syntax is as follows to get the file size:
wc -c /path/to/file
wc -c /etc/passwd

Sample outputs:

5253 /etc/passwd

You can easily extract the first field either using the cut or awk command:
wc -c /etc/passwd | awk '{print $1}'
Sample outputs:

5253

OR assign this size to a bash variable:

myfilesize=$(wc -c "/etc/passwd" | awk '{print $1}')
printf "%d\n" $myfilesize
echo "$myfilesize"

How to get the size of a file in a bash script using stat command

The stat command shows information about the file. The syntax is as follows to get the file size on GNU/Linux stat:
stat -c %s "/etc/passwd"
OR
stat --format=%s "/etc/passwd"
To assign this size to a bash variable:

myfilesize=$(stat --format=%s "/etc/passwd")
echo "$myfilesize"
## or ##
myFileSizeCheck=$(stat -c %s "/etc/resolv.conf")
printf "My file size = %d\n" $myFileSizeCheck

The syntax is as follows to get the file size on BSD/MacOS stat:
stat -f %z "/etc/passwd"
Please note that if the file is symlink you will get size of that link only with the stat command.

du command example

The syntax is

du --apparent-size --block-size=1  "/etc/passwd"
fileName="/etc/hosts"
mfs=$(du --apparent-size --block-size=1  "$fileName" | awk '{ print $1}')
echo "$fileName size = ${mfs}"

Sample outputs from above commands:

Fig.01: How to check size of a file using a bash/ksh/zsh/sh/tcsh shell?Fig.01: How to check size of a file using a bash/ksh/zsh/sh/tcsh shell?

 

Find command example

The syntax is:

find "/etc/passwd" -printf "%s"
find "/etc/passwd" -printf "%s\n"
fileName="/etc/hosts"
mysize=$(find "$fileName" -printf "%s")
printf "File %s size = %d\n" $fileName $mysize
echo "${fileName} size is ${mysize} bytes."
Categories: Système Tags: , , , ,

How to count total number of word occurrences using grep on Linux or Unix

21/12/2023 Comments off

I want to find out how many times a word (say foo or an IP address) occurs in a text file using the grep command on Linux or Unix-like system?

You can use the grep command to search strings, words, text, and numbers for a given patterns. You can pass the -coption to grep command. It only shows the number of times that the pattern has been matched for each file.

 

 

 

Show the total number of times that the word foo appears in a file named bar.txt

The syntax is:
grep -c string filename
grep -c foo bar.txt

Sample outputs:

3

To count total number of occurrences of word in a file named /etc/passwd root using grep, run:
grep -c root /etc/passwd
To verify that run:
grep --color root /etc/passwd
Pass the -w option to grep to select only an entire word or phrase that matches the specified pattern:
grep -w root /etc/passwd
OR
grep -c -w root /etc/passwd
In this example only match a word being with root:
grep --color -w '^root' /etc/passwd
grep -c -w '^root' /etc/passwd

To show only the matching part of the lines.
grep -o 'root' /etc/passwd
grep -c -o 'root' /etc/passwd

Sample session:

Fig.01: Counting occurrence of words/strings using grep commandFig.01: Counting occurrence of words/strings using grep command

Categories: Système Tags: , , , , , ,