Output from remote server at command prompt - shell

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.

Related

Plink - Remote interactive connection with bash script does not seem to work properly

I have a redis database on a remote docker host, and I'd like to access it through a single ssh script command via plink.
The script is simple (redis-script.sh):
#!/bin/bash
echo "Enter Redis Password."
read -s pass
docker exec -it my-redis-container redis-cli -a $pass
Which works fine when I do a standard ssh connection via putty then run the script after login. I am able to enter the password and connect to the db:
Enter Redis Password.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379>
The problem is when I use plink, my plink command line:
plink.exe -t container-host /containers/redis-script.sh
I get this:
Enter Redis Password.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[6n
One issue is the mangled characters, but the biggest issue is that I can no longer type in any commands at this point. I am able to interact when it asks for the password, but once it gets into redis-cli, I cannot type anything.
Perhaps it's the docker exec command which is messing up the interactivity?
Any help is appreciated.

SSH: Run command through sub server

My goal is to be able to send a command to an old server that can only be
reached by going through the new server.
I want to be able to automate this as much as possible.
I want to be able to just run a script and it will do the work for me so
that I don't have to type.
Meaning I would have to do the following:
ssh user#newserver
and then
ssh user#oldserver
Once I reach the old server I need to be able to run
curl icanhazip.com
and
cat /var/spool/cron/user
So far I was only able to do the following:
ssh -t -t root#newserver "ssh root#oldserver"
That would only allow me to reach the server, but I would have to manually send other commands.
I would Ideally want to be able to run something like this:
ssh -t -t root#newserver 'ssh root#oldserver "cat /var/spool/cron/user"'
ssh -t -t root#newserver 'ssh root#oldserver "cat /var/spool/cron/user"'
This Actually worked. Not sure why it didn't before.

Windows telnet script with # in the username

I'm trying to script the telnet command to log into a server. My problem is that my username is my email so it has an # in it - so something like this fails:
telnet user#email.com#1.1.1.1
I've also tried this but it fails the same way.
set USER=user#email.com
telnet %USER%#1.1.1.1
Is there any way to escape the ampersand or is there another way to pass it to the telnet command? I've tried passing -l as follows but it does not work (as it still prompts me for a username so I'm guessing the telnet server doesn't support that and that would be out of my control to change).
telnet -l user#email.com 1.1.1.1
Ultimately, my goal is to get a doskey script with a command like the following
doskey t=telnet user#email.com#1.1.1.1
EDIT: Originally I mistakenly said ampersand but meant the at (#) symbol.

single line telnet commands using terminal

I need to pull something along the lines of "telnet root#192.168.2.99: irinject BACK"
however this refuses to work. There is no password required.
What is the correct syntax to perform this task using the terminal on Ubuntu 11.10?
If you absolutely must do it this way, use echo or etc. to pipe commands to the telnet session — and be ready to reinstall machines as they get hacked.
Strongly preferred is to use ssh with key access; you can even include the command that way.
ssh -i path/to/root-key root#host command

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