How to close SSH socket - bash

I'm working on enabling Session Multiplexing between two servers.
I want to close all existing sockets for this server(or IP) before creating new one and close newly created one after finishing my task. This is what I've done so far:
remote_ip=192.168.20.2 #User inut
remote_port=222
Can create socket by:
SSHSOCKET=~/.ssh/remote_$remote_ip
ssh -M -f -N -o ControlPath=$SSHSOCKET $remote_ip -p $remote_port
Can search control path by:
ps x | grep $remote_ip | grep ssh | cut -d '=' -f 2
/root/.ssh/remote_192.168.20.2 192.168.20.2 -p 222
Can close socket by:
ssh -S /root/.ssh/remote_192.168.20.2 192.168.20.2 -p 64555 -O exit
Trying to close the socket by:
ps x | grep $remote_ip | grep ssh | cut -d '=' -f 2 | xargs ssh -S | xargs -i {} "-O exit"
But I get:
Pseudo-terminal will not be allocated because stdin is not a terminal.
Tried using -t and -tt:
ps x | grep $remote_ip | grep ssh | cut -d '=' -f 2 | xargs ssh -Stt | xargs -i {} "-O exit"
ssh: Could not resolve hostname /root/.ssh/remote_192.168.20.2: Name or service not known
xargs: ssh: exited with status 255; aborting
Can anyone please help me with this?

If you want to kill every connection from your machine to a given remote IP address and port, you can do so as follows (using fuser, a tool from the psmisc package included with all major Linux distros):
fuser -k -n tcp ",${remote_ip},${remote_port}"

Related

Problem using grep to specify process IDs for I/O priority setting

I want to set highest I/O priority using ionice for processes with specific name (farmer and harvest)
Please help check what is wrong with my script it's not working
sudo ionice -c 1 -n 0 -p $(ps -ef | grep farmer | awk '{print $2}')
sudo ionice -c 1 -n 0 -p $(ps -ef | grep harvest | awk '{print $2}')
I got the error "ionice: ioprio_set failed: No such process" even though these processes exist
Use pgrep.
sudo ionice -c 1 -n 0 -p $(pgrep farmer)
sudo ionice -c 1 -n 0 -p $(pgrep harvest)

GI want torep with inotify [duplicate]

(maybe it is the "tcpflow" problem)
I write a script to monitoring http traffic, and I install tcpflow, then grep
it works (and you should make a http request, for example curl www.163.com)
sudo tcpflow -p -c -i eth0 port 80 2>/dev/null | grep '^Host: '
it outputs like this (continuously)
Host: config.getsync.com
Host: i.stack.imgur.com
Host: www.gravatar.com
Host: www.gravatar.com
but I can't continue to use pipe
does not work (nothing output)
sudo tcpflow -p -c -i eth0 port 80 2>/dev/null | grep '^Host: ' | cut -b 7-
does not work (nothing output)
sudo tcpflow -p -c -i eth0 port 80 2>/dev/null | grep '^Host: ' | grep H
When I replace sudo tcpflow with cat foo.txt, it works:
cat foo.txt | grep '^Host: ' | grep H
so what's wrong with pipe or grep or tcpflow ?
update:
This is my final script: https://github.com/zhengkai/config/blob/master/script/monitor_outgoing_http.sh
To grep a continuous stream use --line-buffered option:
sudo tcpflow -p -c -i eth0 port 80 2> /dev/null | grep --line-buffered '^Host'
--line-buffered
Use line buffering on output. This can cause a performance penalty.
Some reflections about buffered outputting(stdbuf tool is also mentioned):
Pipes, how do data flow in a pipeline?
I think the problem is because of stdio buffering, you need to use GNU stdbuf before calling grep,
sudo tcpflow -p -c -i eth0 port 80 2>/dev/null | stdbuf -o0 grep '^Host: '
With the -o0, it basically means the output (stdout) stream from tcpflow will be unbuffered. The default behavior will be to automatically buffer up data into 40961 byte chunks before sending to next command in pipeline, which is what overriden using stdbuf
1. Refer this nice detail into the subject.

Turn off the return message from the executed command

I'm developing a bash script, I've used ssh command in my bash script to run some commands on a remote server and I need to get the result from the command which runs on the remote server. so I wrote this code:
db="$(ssh -t user#host 'mysql --user=username -ppassword -e \"SHOW DATABASES;\" | grep -Ev \"(Database|information_schema|performance_schema)\"' | grep -Ev \"(mysql)\")"
But each time which I run my bash script, I will get Connection to host closed. in first of the db result. this is a default message from ssh command.
Also, If I use > /dev/null 2>&1 end of my command the db variable would be empty.
How can I turn off the return message from the executed command?
Like this :
#!/bin/bash
db=$(
ssh -t user#host bash<<EOF
mysql --user=username -ppassword -e "SHOW DATABASES" |
grep -Ev "(Database|information_schema|performance_schema|mysql)" \
2> >(grep -v 'Connection to host closed')
EOF
)
or if Connection to host closed comes from STDOUT :
...
mysql --user=username -ppassword -e "SHOW DATABASES" |
grep -Ev "(Database|information_schema|performance_schema|mysql|Connection to host closed)"
...

bash script to kill process on remote machines

I want to kill process on remote machines via ssh but its not working
VAR=$(ssh ${HOSTS} ps -ef | grep $SERVICE | grep -v grep | awk '{print $2}' | xargs kill -9)
ssh ${HOSTS} ps ef < /dev/null > /dev/null 2> /dev/null
The problem is that your pipe process get execute on your local host rather than on the server.
A solution is to quote protect the command:
VAR=$(ssh ${HOSTS} "ps -ef | grep $SERVICE | grep -v grep | awk '{print \$2}' | xargs kill -9")
ssh ${HOSTS} "ps ef" < /dev/null > /dev/null 2> /dev/null
Below command worked well for me to kill processes on remove server.
I'm able to kill tail command running on remote server.
ssh -tty ${Host}" ps -efwww | grep tail |grep -v grep |cut -c 10-15|xargs kill -9 "

Getting PID of sshd

I am executing sshd in a bash script using
$ /usr/sbin/sshd
How do I get the process ID of this sshd that I executed?
sshd will typically write a PID file; by default this is at /var/run/sshd.pid. You can use this to find the process ID of the listening sshd process. You should be aware that sshd may fork several subprocesses as it works, so what you want really depends on what you intend to do with it.
Try this command:
ps aux | grep -e /usr/sbin/sshd | grep -v grep | tr -s " " | cut -d " " -f2
or
cat /var/run/sshd.pid

Resources