Skip to content

Instantly share code, notes, and snippets.

@sergeiwaigant
Last active March 8, 2020 12:49
Show Gist options
  • Save sergeiwaigant/87697910e20dfa75865108903522e043 to your computer and use it in GitHub Desktop.
Save sergeiwaigant/87697910e20dfa75865108903522e043 to your computer and use it in GitHub Desktop.

Here are several different ways to test a TCP port without telnet.

Source: https://gist.github.com/Khoulaiz/41b387883a208d6e914b

$ cat < /dev/tcp/127.0.0.1/22
SSH-2.0-OpenSSH_5.3
^C

$ cat < /dev/tcp/127.0.0.1/23
bash: connect: Connection refused
bash: /dev/tcp/127.0.0.1/23: Connection refused

cURL

$ curl -v telnet://127.0.0.1:22
* About to connect() to 127.0.0.1 port 22 (#0)
*   Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 22 (#0)
SSH-2.0-OpenSSH_5.3
^C

$ curl -v telnet://127.0.0.1:23
* About to connect() to 127.0.0.1 port 23 (#0)
*   Trying 127.0.0.1... Connection refused
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host

Python

# python
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('127.0.0.1', 22))
>>> clientsocket.send('\n')
1
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('127.0.0.1', 23))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in connect
socket.error: [Errno 111] Connection refused

Perl

# perl
use IO::Socket::INET;
$| = 1;
my $socket = new IO::Socket::INET(
  PeerHost => '127.0.0.1',
  PeerPort => '22',
  Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";
^D
connected to the server

Credits to skohrs

echo "- - -">/sys/class/scsi_host/host0/scan
echo "- - -">/sys/class/scsi_host/host1/scan
echo "- - -">/sys/class/scsi_host/host2/scan

[root@HOSTNAME scsi_host]# cd /sys/class/scsi_host/
[root@HOSTNAME scsi_host]# ls
host0  host1  host2 
 
or to rescan existing disk, which is our case: cd /sys/class/block/sdb/device 
echo "1" > /sys/class/block/sdb/device/rescan 
 
[root@HOSTNAME device]# pwd
/sys/class/block/sdb/device
[root@HOSTNAME device]# ls rescan
rescan 

resize2fs /dev/mapper/vg_kfkie0z013-lv_home

lvextend -L +268M /dev/mapper/vg_kfkie0z013-lv_home
lvextend -L 4G /dev/mapper/vg_kfkie0z013-lv_var

sudo firewall-cmd --list-all-zones
sudo firewall-cmd --permanent --zone=trusted --list-sources
sudo firewall-cmd --permanent --zone=trusted --add-source=10.59.1.163/32
sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp
sudo firewall-cmd --reload
## Flat JSON and escape quotes with backslash
```jq -c . reports-connector/value.schema.json | Sed -E 's/([^\]|^)"/\1\\"/g'```

from: https://www.lifewire.com/uses-of-linux-command-find-2201100

The 'find' Command

The command used to search for files is called find. The basic syntax of the find command is:

find filename

The currently active path marks the search location, by default. To start searching the whole drive you would type the following:

find / filename

If, however, you want to start searching for the folder you are currently in then you can use the following syntax:

find . filename

When you search by name across the whole drive, use the following syntax:

find / -name filename
  • The first part of the find command is the find command itself.
  • The second part is where to start searching from.
  • The next part is an expression which determines what to find.
  • Finally the last part is the name of the thing to find.

Search Location Shortcuts

The first argument after find is the location you wish to search. Although you may specify a specific directory, most people use a metacharacter to serve as a substitute. The three metacharacters that work with this command include:

  • Period: specifies the current and all nested folders
  • Forward Slash: specifies the entire filesystem
  • Tilde: specifies the active user's home directory

Tip: Searching the entire filesystem is likely to generate a lot of access-denied errors. Run the command with elevated privileges (e.g., by using sudo), if you need to search in places your standard account normally cannot access.

Expressions

The most common expression you will use is -name. The -name expression lets you search for the name of a file or folder.

There are, however, other expressions you can use:

  • amin n: The file was last accessed n minutes ago
  • anewer: The file was last accessed more recently than it was modified
  • atime n: The file was last accessed more n days ago
  • cmin n: The file was last changed n minutes ago
  • cnewer: The file was last changed more recently than the file was modified
  • ctime n: The file was last changed more than n days ago
  • empty: The file is empty
  • executable: The file is executable
  • false: Always false
  • fstype type: The file is on the specified file system
  • gid n: The file belongs to group with the ID n
  • group groupname: The file belongs to the named group
  • ilname pattern: Search for a symbolic line but ignore case
  • iname pattern: Search for a file but ignore case
  • inum n: Search for a file with the specified node
  • ipath path: Search for a path but ignore case
  • iregex expression: Search for a expression but ignore case
  • links n: Search for a file with the specified number of links
  • lname name: Search for a symbolic link
  • mmin n: File's data was last modified n minutes ago
  • mtime n: File's data was last modified n days ago
  • name name: Search for a file with the specified name
  • newer name: Search for a file edited more recently than the file given
  • nogroup: Search for a file with no group id
  • nouser: Search for a file with no user attached to it
  • path path: Search for a path
  • readable: Find files which are readable
  • regex pattern: Search for files matching a regular expression
  • type type: Search for a particular type
  • uid uid: Files numeric user id is the same as uid
  • user name: File is owned by user specified
  • writable: Search for files that can be written to

Example Usage of the Find Command

HOW TO FIND FILES ACCESSED MORE THAN A CERTAIN NUMBER OF DAYS AGO
To find all the files within your home folder accessed more than 100 days ago:

find ~ -atime 100

HOW TO FIND EMPTY FILES AND FOLDERS
To find all the empty files and folders in your system:

find / -empty 

HOW TO FIND ALL OF THE EXECUTABLE FILES
To find all of the executable files on your computer:

find / -exec

HOW TO FIND ALL OF THE READABLE FILES
To find all of the files that are readable:

find / -read

Patterns
When you search for a file you can use a pattern. For example, search for all files with the extension mp3:

find / -name *.mp3

How to Send Output from the Find Command to a File
The main problem with the find command is that it can sometimes return too many results to look at in one go. Pipe the output to the tail command or you can output the lines to a file as follows:

find / -name *.mp3 -fprint nameoffiletoprintto

How to Find and Execute a Command Against a File
To search for and edit a file at the same time:

find / -name filename -exec nano '{}' \;

The above command searches for a file called filename and then runs the nano editor for the file that it finds.

  • /var/log/messages : General message and system related stuff
  • /var/log/auth.log : Authenication logs
  • /var/log/kern.log : Kernel logs
  • /var/log/cron.log : Crond logs (cron job)
  • /var/log/maillog : Mail server logs
  • /var/log/qmail/ : Qmail log directory (more files inside this directory)
  • /var/log/httpd/ : Apache access and error logs directory
  • /var/log/lighttpd/ : Lighttpd access and error logs directory
  • /var/log/boot.log : System boot log
  • /var/log/mysqld.log : MySQL database server log file
  • /var/log/secure or /var/log/auth.log : Authentication log
  • /var/log/utmp or /var/log/wtmp : Login records file
  • /var/log/yum.log : Yum command log file.

from https://www.cyberciti.biz/faq/linux-log-files-location-and-how-do-i-view-logs-files/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment