how to start nc over a remote machine in a screen - linux

I have a requirement to start nc on remote machine in screen and start transfer of file from another remote machine in screen, I am trying to run this through deploy machine(jenkins) with bash script
on remote machine 1 i.e tester1 :
ssh -tt mysql#tester1 'screen -d -m nc -l -w 60 5555 | tar xvif -'
on remote machine 2 i.e tester2 :
ssh -tt tester2 'screen -d -m sudo -u mysql innobackupex --stream=tar --databases="sampledb" /mysql-backup/prodfullbkp | nc -w 30 tester 5555'
While the two above commands are not working when running from deploy machine.Could someone please help me give any better way of doing this.
Thanks in advance =)

You can have a better solution like
ssh user#host << EOF
#command to excecute
EOF
ie tester1 would be
ssh -tt mysql#tester1 << EOF
screen -d -m nc -l -w 60 5555 | tar xvif -
EOF
tester2 would be
ssh -tt tester2 << EOF
screen -d -m sudo -u mysql innobackupex --stream=tar --databases="sampledb" /mysql-backup/prodfullbkp | nc -w 30 tester 5555
EOF

Related

How to setup server with netcat? [duplicate]

I have an embedded linux application with a simple interactive command line interface.
I'd like to access the command line from telnet (or network, in general).
However, the process should be started when the board turns on, and in a unique instance. So, the following netcat command is not an option:
nc -l -p 4000 -e myapp
I can do
nc -l -p 4000 | myapp
to send remote commands to myapp, but this way I can't see myapp output.
Is there any way to redirect both stdin and stdout to netcat?
Thanks.
Is there a way to redirect both stdin and stdout to netcat
There is socat, which is a more advanced netcat. You can redirect both stdin and stdout with it. E.g.:
socat TCP4-LISTEN:5556,reuseaddr,fork EXEC:"cat - /etc/redhat-release"
In the above cat reads stdin and /etc/redhat-release and outputs them into stdout.
And then try using that:
$ echo "hello" | nc 127.0.0.1 5556
hello
Fedora release 22 (Twenty Two)
$ echo "hello 2" | nc 127.0.0.1 5556
hello 2
Fedora release 22 (Twenty Two)
I found that by using bash v. >= 4.0 I can use coproc:
#!/bin/bash
coproc myapp
nc -kl -p 4000 <&"${COPROC[0]}" >&"${COPROC[1]}"
EDIT
I eventually incorporated a telnet server in my cli library. You can find the result on GitHub: https://github.com/daniele77/cli
You can use ncat (from nmap package: apt install nmap) for that as well as follow:
ncat -lnvp 443 -e myapp
don't forget to fflush(stdout); after each printf("%s",str); in your app
Just found this trick reading man nc.
One could redirect both stdin and stdout to the same app using named pipes.
Example:
# create named pipe
rm -f /tmp/f; mkfifo /tmp/f
# launch interactive 'sh' session redirected to/from listening nc
cat /tmp/f | /bin/sh -i 2>&1 | nc -l 1234 > /tmp/f
From another terminal:
nc localhost 1234 # connect to the listening nc
$
$
$ ls -lah
total 16K
drwxrwxr-x 43 ky ky 12K Jun 17 01:31 .
drwxr-xr-x 123 ky ky 12K Jun 17 01:35 ..
Oh my, it really works. And if you don't mind keeping all the sent data in a file then this approach could also be using a file instead of a named pipe. Like this:
# create an empty file
echo -n '' > /tmp/session
# launch interactive 'sh' session redirected to/from listening nc
tail -f /tmp/session | /bin/sh -i 2>&1 | nc -l 1234 > /tmp/session

Can't get script to run in the backround

I would like my script to run in the backround, SSH into another computer, run tcpdump, produce a pcap file and save it to my local computer. I have all of this working save for the running in the background portion.
I have looked at several solutions on Stack Overflow (example) but they don't seem to work for me. Admittedly I am a novice with bash however so it is entirely possible that I am reading them incorrectly.
ssh root#ipaddress "tcpdump -c 400000 -s 0 -U -n -w - -i eth0 not arp" &>/dev/null &disown \ > /root/Destop/BashPcap/01Bash.pcap
Check your quotation endings maybe that's the problem...
Or you can save the file remotely and download back using scp (SecureCoPy).
Eg:
scp root#ipaddress:/path/to/file ~/Documents/path-where you-want-to-save.pcap
As far as I understood your task this is what you want:
nohup ssh root#ipaddress "tcpdump -c 400000 -s 0 -U -n -w - -i eth0 not arp" &> /root/Destop/BashPcap/01Bash.pcap &
In simple words:
nohup - it will allow you to close your terminal and the script will continue to run
ssh ... - this is the command to execute
&> - redirect both stdout and stderr to file (Bash 4)
& - sends command to the background
Note: &> will send to the file both stdout and stderr, you need this if you want to have in your file the summary lines from tcpdump. They are written to stderr:
N packets captured
X packets received by filter
Y packets dropped by kernel
If you do not want to have these lines, then send stderr to /dev/null
nohup ssh root#ipaddress "tcpdump -c 400000 -s 0 -U -n -w - -i eth0 not arp" 2>/dev/null > /root/Destop/BashPcap/01Bash.pcap &

Use ssh to launch remote process and exit

I am trying to use ssh from the command line to launch a python server on a remote server using the following command:
$ ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
-o ConnectTimeout=5 -f -i mykey.pem user#99.99.99.99 \
'python -m SimpleHTTPServer 3000 & echo $! > /home/user/pysrv.pid'
After the launch my ssh session goes to the background but does not exit until the python server is running. Is there a way I can setup the command so that ssh does not stick around as a background process on my current machine?
You can do
nohup python -m SimpleHTTPServer 3000 & echo $! > /home/user/pysrv.pid &
It will create a detached task that doesn't need the parent (ssh).
OTOH, if you kill the ssh server process, you won't be able to connect again. Is this what you want, or are you just trying to kill the session? If it's just the session, it should go away by itself after the connection is dropped.
The ssh command exits when the TCP connection is closed. If you redirect stdin and stderr of the remote command, nothing will be connected to the TCP connection and it will close.
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=5 -f -i mykey.pem user#99.99.99.99 'python -m SimpleHTTPServer 3000 2>/dev/null 2>&1 </dev/null& echo $! > /home/user/pysrv.pid'

What's wrong about my script with "ssh + nohup"

I want to execute specific script at remote server by ssh in background.
I found some solution about nohup.
But, nohup is not running without "2>&1"
I want to know what's the difference between existing "2>&1" and not.
nohup needs "2>&1" expression?
(Please understand my bad English)
This is my 'iperf_server.sh' script.
iperf -s -p 1 -w 128K
And, It is my host machine command.
$ ssh [id]#[host] "nohup echo [password] | sudo -S [Home_dir]/iperf_server.sh > /dev/null &"
$ ssh [id]#[host] "nohup echo [password] | sudo -S [Home_dir]/iperf_server.sh > /dev/null 2>&1 &"
$ ssh -t [id]#[host] "nohup echo [password] | sudo -S [Home_dir]/iperf_server.sh > /dev/null &"
Connection to iperf-server closed.
$ ssh -t [id]#[host] "nohup echo [password] | sudo -S [Home_DIR]/iperf_server.sh > /dev/null 2>&1 &"
Connection to iperf-server closed.
This is ps command result in iperf server
# ps -eLf | grep iperf | grep -v grep
# ps -eLf | grep iperf | grep -v grep
00:00:00 sudo -S [HOME_DIR]/iperf_server.sh
00:00:00 sh [HOME_DIR]/iperf_server.sh
00:00:00 iperf -s -p 1 -w 128K
00:00:00 iperf -s -p 1 -w 128K
00:00:00 iperf -s -p 1 -w 128K
# killall iperf
# ps -eLf | grep iperf | grep -v grep
# ps -eLf | grep iperf | grep -v grep
Take the & off the end.
This should do it:
ssh -t [id]#[host] "nohup echo [password] | sudo -S [Home_dir]/iperf_server.sh > /dev/null 2>&1"
By the way this is a huge security risk. Don't echo the password on the command line! If you really want to use a password like this at least do something like cat pwd.txt | sudo -S instead.

command hangs head / netcat

I am using two linux machines to simulate some firewall tests... I execute the tests by running nc through ssh on a remote machine... if I spawn the ssh like this, it works...
ssh -i id_dsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
-p 2224 root#a2-idf-lab nc -s 10.26.216.82 10.195.18.132 \
21 < /var/log/messages
However, if I try to control how much of /var/log/messages with head -c 20 /var/log/messages, the command hangs but I don't understand why...
ssh -i id_dsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
-p 2224 root#a2-idf-lab nc -s 10.26.216.82 10.195.18.132 \
21 < head -c 20 /var/log/messages
I also tried this with no better success...
ssh -i id_dsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
-p 2224 root#a2-idf-lab nc -s 10.26.216.82 10.195.18.132 \
21 < (head -c 20 /var/log/messages)
Question: Why does the second command hang, and how can I accomplish what I need?
FYI, these experiments were really in preparation for sending cat /dev/urandom | base64 | head -c 20 - into nc... bonus points if you can give me cli that would work with nc through an ssh session...
< is shell redirection, it redirects the input stream to read from a file, not to execute a command. try:
head -c 20 /var/log/messages | ssh -i id_dsa -o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
-p 2224 root#a2-idf-lab nc -s 10.26.216.82 10.195.18.132 21
this pipes /var/log/messages from the local machine into nc on the remote machine.
if you want to use the /var/log/messages file on the remote machine, use quotes around the command:
ssh -i id_dsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
-p 2224 root#a2-idf-lab "head -c 20 /var/log/messages |\
nc -s 10.26.216.82 10.195.18.132 21"
Try to use
head -n 20
My guess is the problem is the lack of carriage return at the end.

Resources