hot to get memcached stats without nc? - bash

this is how I'm getting the stats now:
echo -e "stats\nquit" | nc 127.0.0.1 11211
I can't use expect as it's not part of a default installation.
Is there a way to get memcached stats without nc?

Your question doesn't specify why you're looking for an alternative to netcat, so it's hard to to tell what you're looking for. You could do it in bash like this:
exec 3<>/dev/tcp/127.0.0.1/11211
echo -e "stats\nquit" >&3
cat <&3
You could do it using telnet:
(echo -e 'stats\nquit'; sleep 1) | telnet localhost 11211
The sleep is to precent telnet from exiting before receiving a response from memcached.
You could also write something simple in python or perl or some other high level scripting language. Or brush up on your c. There are lots of options.

Another, possibly simpler way, is with the memcached-tool script. It came installed with my installation of memcached 1.4.5 via yum, but under apt and ubuntu I didn't get it. I found it here and put it on my system: https://raw.githubusercontent.com/memcached/memcached/master/scripts/memcached-tool
on the server, type the following to get memcached stats:
memcached-tool 127.0.0.1:11211 stats
or the following to get slabs:
memcached-tool 127.0.0.1:11211
assuming your server is listening on port 11211 and IP 127.0.0.1 (set config options at /etc/sysconfic/memcached)
article: http://www.cyberciti.biz/faq/rhel-fedora-linux-install-memcached-caching-system-rpm/

Related

automated retrieval of the external ip address of all of my current connections

I am trying to make a program that automatically lists all of the connections to my computer from outside of the router. The end goal of this script is that I would like to be able to have a clean list of the external IP addresses of every server/website I am connecting to. I am also trying to use this as a way to learn more about how networks, websites, and servers work so I am sorry for any mistakes I make with terminology and general knowledge!
My tcpdump bash script:
while :
do
# get myip and assign it to a variable
myip="$(ifconfig wlp2s0 | grep -E -o -m 1 "inet................" | grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)")"
# tcpdump on my ip for all packets going to or from my ip address. the ipaddress of the packets is placed in IP Address.txt
sudo tcpdump -c 1 -nn host "$myip" | grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" >> IPaddress.txt
done
I thought that tcpdump would be the tool for this however I confess that I do not know how tcpdump works. This script is a bash file that I am running through ubuntu. How would I use tcpdump to collect the IP address of every website that I am connecting to? I read the tcpdump documentation and believe it can help me achieve my goal however if there are better tools out there I would love to hear it! Currently, this code only displays internal IP addresses. ;(
I'd lean more towards using ss or netstat.
ss --all --ipv4
Would show all IPv4 connections.
The same works for IPv6 of course; and you could add one of many arguments to get more detailed information if you want, such as --processes, --extended, or --info.
There's also a few more arguments to control the output format, making it more suitable for parsing:
ss --all --ipv4 --processes --no-header --oneline
Suggest to follow ss command .
Learn about ss command here.

Set source port when sending udp message with echo?

I'm using
echo "This is my data" > /dev/udp/192.168.0.92/7891
in a bash script to test udp communication with a device. The device then returns the message. I've seen via wireshark that my source port is always changing.
Anyway, I want to set the source port, can I do that?
Use netcat nc and its -p option to set the source port.
As said in the netcat man page
-p source_port
Specify the source port nc should use, subject to privilege restrictions and availability.
Then use it like this:
echo "This is my data" | nc -u -p 50000 192.168.0.92 7291

Bash: Check up, Run a process if not running [duplicate]

This question already has answers here:
How do I write a bash script to restart a process if it dies?
(10 answers)
Closed 6 years ago.
Bash: Check up, Run a process if not running
Hi ,
My requirement is that , if Memcache server is down for any reason in production , i want to restart it immediately
Typically i will start Memcache server in this way with user as nobody with replication as shown below
memcached -u nobody -l 192.168.1.1 -m 2076 -x 192.168.1.2 -v
So for this i added a entry in crontab this way
(crontab -e)
*/5 * * * * /home/memcached/memcached_autostart.sh
memcached_autostart.sh
#!/bin/bash
ps -eaf | grep 11211 | grep memcached
# if not found - equals to 1, start it
if [ $? -eq 1 ]
then
memcached -u nobody -l 192.168.1.1 -m 2076 -x 192.168.1.2 -v
else
echo "eq 0 - memcache running - do nothing"
fi
My question is inside memcached_autostart.sh , for autorestarting the memcached server , is there any problem with the above script ??
Or
If there is any better approach for achieving this (rather than using cron job )
Please share your experience .
Yes the problem is ps -eaf | grep 11211 | grep memcached I assume is the process ID which always changes on every start, so what you should do is ps -ef | grep memcached
hope that helped
Instead of running it from cron you might want to create a proper init-script. See /etc/init.d/ for examples. Also, if you do this most systems already have functionality to handle most of the work, like checking for starting, restarting, stopping, checking for already running processes etc.
Most daemon scripts save the pid to a special file (e.g. /var/run/foo), and then you can check for the existence of that file.
For Ubuntu, you can see /etc/init.d/skeleton for example script that you can copy.

Output from remote server at command prompt

I can connect to remote redis using the telnet command and get the value of "mytest" key. The following is working as expected.
[root#server shantanu]# telnet 10.10.10.100 6379
Trying 10.10.10.100...
Connected to 10.10.10.100 (10.10.10.100).
Escape character is '^]'.
get mytest
$14
this is first
But how do I use it in shell script?
I am used to connect to mysql using the following:
msyql -h10.10.10.100 -uroot -proot#123 -e"show databases"
Is a simialar syntax available for redis?
You can alternatively use redis-cli, included in redis
$ ./src/redis-cli --raw GET key
test
I don't know telnet, but with ssh you can:
ssh user#server "command arg1 arg2 ..."
for example
ssh user#server "ls -ltr | tail"
I would use a tool like wget, which is designed to get content from websites, and is very configurable and automaetable. You might even be able to get away with
export myTestKey=`echo "get mytest" | telnet 10.10.10.100 6379`
If the conversation needs to be more complex than that, I would use telnet in combination with expect, which is designed for trigger and response conversations.

ssh issue in a loop

I have a script that connects to a server using ssh. While in a loop, it fails to connect to the second server after connecting to the first one. I guess I have to quit from that server to come back to the calling script. How do I quit the ssh session?
while read dbname myip
do
ssh root#$myip "mysqldump - some command " | mysql -hhost -u -p myLocalDatabase > /dev/null 2>&1
done << iplist
db1 111.111.111.111
xyz 222.222.222.222
iplist
redirect stdin to /dev/null
while read -r dbname myip
do
0</dev/null ssh ...... <whatever> .........
done < "iplist"
At a slightly higher level of abstraction, you may be interested in e.g. Chef:
Chef is a systems integration framework, built to bring the benefits of configuration management to your entire infrastructure. With Chef, you can:
Manage your servers by writing code, not by running commands. (via Cookbooks)
Integrate tightly with your applications, databases, LDAP directories, and more. (via Libraries)
Easily configure applications that require knowledge about your entire infrastructure ("What systems are running my application?" "What is the current master database server?")
If you want to issue the same command on multiple SSH hosts, you can use DSH:
dsh is an implementation of a wrapper for executing multiple remote shell (rsh/remsh/ssh) commands.
If you don't have whitespace in the lines of file "dbname", you can use this:
for myip in $(cat dbname); do
...
done
(or use fabric: http://docs.fabfile.org/en/latest/)

Resources