How to send the data to TCP PORT using shell script commands - shell

I’m looking for a solution to send the data from a file to the TCP port which is available on the specific server using the shell script/Unix command.
When I checking for this option I found the below one but it’s not working out? Is there any solution is available?
The command I tried from a shell script and find the response below.
Option 1: exec cat /home/data/load.txt > /dev/tcp/$host/$port
-bash: : Name or service not known
-bash: /dev/tcp//: Invalid argument
Option 2:
ncat -v -w 2 hostname 4053 < /home/data/test.txt
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to xx.xx.xx.xx:4053.
Ncat: Connection reset by peer.
It would be helpful if could someone help with this.
Thanks | SP

Related

Error ssh: connect to host 333 port 22: Invalid argument

I try to transfer the "test" folder to another server using command shell script scp, then I get an error like:
ssh: connect to host 333 port 22: Invalid argument
lost connection
my script:
scp /test_web/test ssh -i example.pem root#222.111.222.111 -p 333:/web
I'm just learning about shell script, I don't know if there's a syntax wrong or not, I'm very grateful if you can help solve this
I think it is
scp -P 333 -i example.pem /test_web/test root#222.111.222.111:/web
For most programs you can find a man page with synopsis through man {command name}.
For example: man scp
If you encounter further issues, check if you are able to use ssh with root at all. Sometimes root is not permitted to connect via ssh.

socat error - unknown option "proxyport:8080"

I'm trying to run a simple forwarding proxy through another proxy server.
But as soon as I connect to the opened port (e.g. curl localhost:9090) an error is thrown and the connection is closed.
$ socat -v TCP-LISTEN:9090,fork PROXY:proxyhost:google.com:80,proxyport:8080
2020/03/01 15:00:00 socat[109218] E parseopts(): unknown option "proxyport:8080"
I'm using socat version 1.7.3.2 on May 11 2017 13:28:30
with features -> #define WITH_PROXY 1
I tried this on Suse as well as alpine linux and get the same result.
Am I doing something wrong or is this a bug?
Thanks in advance
I wrote to the mailing list and the maintainer made me aware of my mistake.
The parameter value must be seperated by an equals instead of a colon.
$ socat -v TCP-LISTEN:9090,fork PROXY:proxyhost:google.com:80,proxyport=8080

capture plink failed connection messages to file [duplicate]

This question already has answers here:
How to redirect Windows cmd stdout and stderr to a single file?
(7 answers)
Closed last year.
I am using plink in windows to connect to a network device and capture the output to a file. That part works great.
Here is an example of the command line I am putting in a batch program.
plink.exe -v -l [username] -pw [password] [ip address] -m "c:\empty.txt" < "c:\commands.txt" > "c:\command_output.txt"
command_output.txt only contains the output from the ssh session.
I want to add some error handling in case it cannot connect for example. I can't figure out how to get the connection messages to go to a file. They go to the command window only. I have tried using start /c cmd.exe from another batch program and redirectiong that output to another log file. Tried using 2>&1. I get nothing. It always just goes to the command window.
Here is and example of what I want to capture.
Looking up host "xxx.xxx.xxx.xxx"
Connecting to xxx.xxx.xxx.xxx port 22
Failed to connect to xxx.xxx.xxx.xxx: Network error: Connection timed out
Network error: Connection timed out
FATAL ERROR: Network error: Connection timed out
So, to sum up use the command below. I'm incorporating #Martin Prikryl's suggestion into an answer so this shows as answered.
plink.exe -v -l [username] -pw [password] [ip address] -m "c:\empty.txt" < "c:\commands.txt" > "c:\command_output.txt" 2>&1

Connecting to an open port with bash

Is there a possible way to connect to any open port with bash ? i.e without calling any external command. :)
I wanted to connect to an open port with bash without using nc or telnet.
It depends on the shell; bash, for instance, uses I/O redirection to attach to arbitrary TCP and UDP sockets. From the man page:
Bash handles several filenames specially when they are used in redirections, as
described in the following table:
[...]
/dev/tcp/host/port
If host is a valid hostname or Internet address, and port is
an integer port number or service name, bash attempts to open a
TCP connection to the corresponding socket.
/dev/udp/host/port
If host is a valid hostname or Internet address, and port is
an integer port number or service name, bash attempts to open a
UDP connection to the corresponding socket.
For example, to get the current time:
cat < /dev/tcp/time-a.nist.gov/13
Note that it must be a redirection; cat /dev/tcp/time-a.nist.gov/13 would only work if your file system implemented some sort of sockets-on-demand.
A very basic HTTP client:
$ exec 3<>/dev/tcp/www.example.com/80
$ echo "GET /" >&3
$ cat <&3

shrewsoft command line interface to connect and terminate vpn on ubuntu

Shrewsoft [1] provides a command line interface for setting up the vpn tunnel automatically without any user intervention, such as by using the following command
ikec -u username -p password -r configuration -a
IS there any way to detect if the connect attempt was successful such as by reading live logs and how can we terminate the vpn tunnel after some time using the command line. Any help will be appreciable.
By looking at the terminal output from ikec -u username -p password -r configuration -a, you can tell if the connection was successful; if the output has a line ii : tunnel enabled, that means the connection was successful and should work. If you get a message such as >> : detached from key daemon or failed to connect to key daemon, it means there was a problem with the connection (https://askubuntu.com/a/793336/705434 suggests running sudo /usr/sbin/iked for a solution to this particular error). To exit the ikec command, just type q into the terminal.
EDIT: it looks like this page (https://gist.github.com/fschabmeyer/22b759994cf852df7e9b) has a shell script that can handle the detection, you should be able to add a case to exit the command automatically after a certain amount of time.

Resources