- Linux создать файл сокет
- NAME
- SYNOPSIS
- DESCRIPTION
- OPTIONS
- EXAMPLES
- DIAGNOSTICS
- SEE ALSO
- BUGS
- VERSION
- AUTHOR
- Learn the Socat Command Examples in Linux
- Socat Command in Action
- Install Socat Utility in Linux
- 1. Listen On a Specific Port
- 2. Connect to Remote Server on Port
- 3. TCP Port Forwarder
- 4. Listening to Local Port
- 5. Listening to Specific Port on Remote Socket
- 6. Network-based Message Collector
Linux создать файл сокет
NAME
socket - create a TCP or a UNIX domain socket and connect to stdin/out
SYNOPSIS
socket [ -bcfqrvw ] [ -p command ] [ -B local address ] host port socket [ -bcfqrvw ] [ -p command ] /path socket [ -bcfqrvw ] [ -p command ] [ -B local address ] -s [ -l ] port socket [ -bcfqrvw ] [ -p command ] -s [ -l ] /path
DESCRIPTION
Socket creates an Internet domain TCP or a UNIX domain stream socket and connects it to stdin and stdout. The host argument can be an Internet number in dot-notation (like ``130.149.28.10'') or a domain name. In this case it must be possible to resolve the name to a valid Internet address with gethostbyname(3). The port argument can be a port number or a service name which can be mapped to a port number by getservbyname(3). If an UNIX domain socket is wanted to be created instead of an Internet socket, specify the path instead of an internet (canonical domain named or dot-notated) host. The hostname is treated as a pathname if contains at least a single slash. I.e. if one wants to create or connect to a socket in the current directory, use ./filename to specify the connection point.
OPTIONS
-b (background) The program forks itself into the background, detaches from its controlling tty, closes the file descriptors associated with the tty, and changes its current directory to the root directory. -B (local address) This option specifies which local address to binded to when making a connection. -c (crlf) Linefeed characters (LF) are converted to a Carriage Return Linefeed sequence (CRLF) when written to the socket. CRLF sequences read from the socket are converted to a single LF. -f (fork) When a server connection has been accepted, a separate process is forked to handle the connection in background. -l (loop) (only valid with -s) After a connection has been closed, another connection is accepted. -p (program) The specified command is executed for each connection. Its standard input, standard output, and standard error channels are connected to the socket. Command can be any shell command since it is passed to /bin/sh. -q (quit) The connection is closed when an end-of-file condition occurs on standard input. -r (read only) No data is read from standard input and written to the socket. -s (server) A server socket is created. A hostname argument is not required of Internet sockets, only the port number but a pathname is required for UNIX domain sockets. -v (verbose) Messages about connections etc. are issued to stderr. -w (write only) No data is read from the socket and written to the standard output. -version Socket prints its version ID and terminates. This must be the first argument to have an effect.
EXAMPLES
The command socket -v coma.cs.tu-berlin.de nntp connects to the nntp port (port 119) of coma.cs.tu-berlin.de (130.149.28.10). The command socket -sl 3425 creates a server socket on port 3425 on the local host and waits for a connection. After a connection has been closed, a new connection is accepted. The command socket -wslqvp "echo Socket! " 1938 creates a server socket on port 1938 on the local host and waits for a connection. When a connection is accepted, the string "Socket!" is written to the socket. No data is read from the socket and written to the finger program. The connection is closed when an end- of-file condition at the standard output of the program occurs. Then a new connection is accepted.
DIAGNOSTICS
Lots of diagnostics for failed system calls. unknown host host host's address could not be resolved. Signal signal caught, exiting Socket exits on any signal other than SIGTSTP, SIGCONT, SIGCLD, SIGQUIT. A non-zero exit code is returned if socket terminates due to an error condition or a signal.
SEE ALSO
ip(7), tcp(7), unix(7), accept(2), bind(2), listen(2), connect(2), socket(2), gethostbyname(3), getservbyname(3)
BUGS
socket -p terminates due to a SIGPIPE signal when there is more data from the socket available than the executed program wants to read. Please report any other bugs to the author.
VERSION
This manual page describes Socket-1.1.
AUTHOR
Learn the Socat Command Examples in Linux
Socat or SOcket CAT is a Linux command-line/terminal-based utility used to establish and transfer data between two bidirectional byte streams.
The socat command can be implemented in multiple scenarios because of two main reasons:
- Data sinks and sources; of different types and existing in a large set, can be used to construct streams.
- These constructed streams can be associated with many address options.
The socat command can be likened to the netcat utility which is tied to the TCP and UDP protocols. However, socat has a security advantage (chrooting) over netcat and also supports devices, pipes, files, SSL, client for SOCKS4, TCP sockets, proxy CONNECT, UNIX sockets, etc.
Socat Command in Action
To be more acquainted with this Linux command-line utility, we need to list some of its practical applications. The following bullet points summarize some popular socat utility applications:
- Security testing and research.
- TCP-oriented programs to serial line redirection.
- As UNIX sockets shell interface.
- External socksifier.
- Establishing su and chroot secure environments for executing server/client shell scripts on a shared network connection.
- The logical connection of serial lines on distinct computers.
- IP6 relay.
- Security testing by attacking weak firewalls.
- TCP port forwarding.
Install Socat Utility in Linux
If socat Linux command-line-based utility is not already installed on your Linux operating system distribution, refer to one of the following installation commands in reference to the Linux OS distribution you are using:
$ sudo apt install socat [On Debian, Ubuntu and Mint] $ sudo yum install socat [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a net-misc/socat [On Gentoo Linux] $ sudo pacman -S socat [On Arch Linux] $ sudo zypper install socat [On OpenSUSE]
As already discussed, socat is a superior alternative to netcat utility because of its powerful and advanced features. We should now be able to see some practical examples of the usage of socat utility via the Linux command line environment.
Its usage syntax is as follows:
Make sure you have sudoer/root user privileges on your Linux machine.
1. Listen On a Specific Port
We can instruct socat to listen on a specific port e.g 80 via the TCP protocol and print out any associated findings via STDOUT as demonstrated below.
$ sudo socat TCP4-LISTEN:80 STDOUT
TCP can switch to other different values such as TCP6, TCP6-LISTEN, and TCP4.
2. Connect to Remote Server on Port
To connect to a server associated with a port, we would run:
$ sudo socat – TCP4:ubuntumint.com:80
3. TCP Port Forwarder
It is also an effective TCP port forwarder. For instance, port 81 connections can be forwarded to port 80 as demonstrated:
$ sudo socat TCP4-LISTEN:81 TCP4:192.168.122.1:80
$ sudo socat TCP4-LISTEN:81,fork,reuseaddr TCP4:TCP4:192.168.122.1:80
You can cancel port forwarding with the keyboard combination [Ctrl]+c .
4. Listening to Local Port
Listening to local port www.
$ sudo socat TCP4-LISTEN:www TCP4:ubuntumint.com:www
5. Listening to Specific Port on Remote Socket
If we wanted to listen to a specific port, accept its connection and forward it to a remote Unix socket e.g mysql.sock, we would implement the socat command in the following manner:
$ sudo socat TCP-LISTEN:3309,reuseaddr,fork UNIX-CONNECT:/var/lib/mysql/mysql.sock
6. Network-based Message Collector
This simple example demonstrates the implementation of a network-based message collector. After client connection to port 3354 is successful, the file /tmp/testing.log appends clients-sent data via a newly generated child process. This file is automatically created by socat when found to be absent.
$ sudo socat -u TCP4-LISTEN:3354,reuseaddr,fork OPEN:/tmp/testing.log,creat,append
With the above-discussed few examples and further reference to the socat manual page, we should be able to explore it to its depth.