Execute a command on remote machine using telnet in a single line - python

I would like to execute a command on a remote machine using telnet as,
telnet x.x.x.x command or similar
I want to include this as part of a script in Python ( subprocess ) and hence need it in one line. Or, are there any other ways to do the same?
Any help is appreciated.
Thanks.

Rather than relying on subprocess, you could try Python's built-in telnetlib instead.
A complete example that does almost exactly what you want is available as an example in the documentation: http://docs.python.org/library/telnetlib.html#telnet-example
I would personally also see if SSH is available on the target system. Not only will you be using a secure connection, but you can also set up SSH keys and use SSH's built-in support for executing a single command (e.g. ssh user#example.com ls -l).

#!/bin/sh
empty -f -i in -o out telnet foo.bar.com
empty -w -i out -o in "ogin:" "luser\n"
empty -w -i out -o in "assword:" "TopSecret\n"
empty -s -o in "who am i\n"
empty -s -o in "exit\n"
http://empty.sourceforge.net/

Maybe this solution will be useful, but as stated in there, it won't work in case you need user/password authentication.
In order to skip manual user/password authentication, setting up ssh keys is the way to go. There is a short explanation about this here: SSH to a server without password for Admin Ease

Related

Paramiko equivalent of ssh host -t bash

In my Python3 script, I am trying to use Paramiko to ssh into remote devices. I can do that just fine. The issue is that ssh dumps me into a proprietary shell. I need to add -t bash to the .connect command to dump me into a bash shell. Here is what I have that is working:
ssh_tranfer = paramiko.SSHClient()
ssh_transfer.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh.connect(hostname=device, port=22, username=username, key_filename=private_key_name)
With this I can connect just fine, but like I said, I am in the proprietary shell and can't pass it commands. I am not even sure that I can do it this way.
I define all the parameters that I am using (ie - device, username, and the private key) prior to the ssh.connect shown above.
If I was to ssh into the device directly from my computer to go straight to the bash shell, I would use:
ssh username#device.com -t bash
I would like to find a way to do this using paramiko.
Thanks for the help!
The -t + bash in ssh do two things:
Starts command bash in "exec" channel (instead of starting "shell" channel, what ssh does by default).
For that, see Python Paramiko - Run command
The -t forces an interactive session, what would be the default for "shell", but is by default disabled for "exec".
For that, pass get_pty=True to SSHClient.exec_command.
Obligatory warning: Do not use AutoAddPolicy this way – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".

Paramiko equivalent for OpenSSH directives PreferredAuthentications=password and PubkeyAuthentication=no

Can I get Paramiko code for the following ssh command?
ssh Administrator#xx.xx.xx.xx -vv -o PreferredAuthentications=password -o PubkeyAuthentication=no
Need to know how to handle fields PreferredAuthentications and PubkeyAuthentication in Paramiko.
There's no direct equivalent, as Paramiko has different logic than ssh when selecting the authentication methods. And you actually didn't tell us why are you using those directives.
If your point was to avoid using autodiscovered key files, use allow_agent=False and look_for_keys=False.
See Force password authentication (ignore keys in .ssh folder) in Paramiko in Python

Fabric: How to edit file across machines?

Is there a way to use Fabric to open editor and set text?
I have to set new keys on 50+ servers and not willing to do it manually.
okay create .aws directory in your local machine and put whatever contents you want to copy.
install sshpass
run this command over a loop changing hostname and password
sshpass -p abcpass rsync -arvce "ssh -o StrictHostKeyChecking=no" ./* user#ip:/home/user/.aws

How do i give ssh keys for multiple hosts from terminal command line in fabric

I want to test various package installation on multiple hosts. Different hosts have different password/ssh-key.
I dont want to hard code host name and their ssh-key in my fab file. How can i pass multiple host and their ssh-key through terminal command line.
Code in my fab file looks like -
from fabric.api import settings, run, env
def test_installation(cmd):
run("dpkg -s %s" %cmd)
And i am calling it like -
fab test_installation:tomcat7 --hosts "user1#host1:port","vuser2#host2:port" -i "ssh-file-path for host1","ssh-file-path for host2"
Please suggest me the proper way. any help is most welcomed.
You don't provide ssh keys of hosts, but only your ssh key, that is used to register you in authorized_keys on host. And you provide only path to it (usually it is ~/.ssh/id_rsa).
Moreover, you can configure fabric to use your ssh config, so you don't need to hardcode any path at all. It can use the same keys, as it would use if you typed ssh my_host in shell.
How to do that you can find in fabric tutorial:
http://docs.fabfile.org/en/1.8/usage/execution.html#leveraging-native-ssh-config-files
http://docs.fabfile.org/en/1.8/usage/env.html#full-list-of-env-vars
You can also set your ~/.ssh/config to use different key for different host.
If you are not familiar with ssh and configuring it, please see:
http://linux.die.net/man/5/ssh_config

How can you automate terminal commands?

I'm tired of doing this.
ssh me#somehost.com
input my password
sudo su - someuser
input my password
cd /some/working/directory
<run some commands>
Is there anyway to automate this? Do I need a special shell? or a shell emulator? can I programmatically drive the shell up to certain point then run manual commands on it?
Bonus points of it's programmed in python for extra hacking goodness
edit: All the answers below focus on the "full automation" part of the question: Where the hard part is what I highlighted above. Here is another example to see if I can capture the essence.
ssh me#somehost.com
<get a shell because keys are setup>
sudo su - user_that_deploys_the_app
<input password, because we don't want to give passwordless sudo to developers>
cd env; source bin/activate
cd /path/where/ur/app/is/staging
<edit some files, restart the server, edit some more, check the logs, etc.>
exit the term
For the ssh/authentication piece, you can setup passwordless authentication by using keys. Then you can simply use ssh and a bash script to execute a series of commands in an automated fashion.
You could use Python here, but if you are executing a series of shell commands, it's probably a better idea to use a shell script, as that's precisely what they do.
Alternately, look into Fabric for your automation needs. It's Python-based, and your "recipes" are written in Python.
I'm not quite sure what you're asking, but what you're probably asking about is getting SSH working in password-less mode using public keys. The general idea is you generate an SSH keypair:
ssh-keygen -t rsa
which gives you id_rsa and id_rsa.pub. You append the contents of id_rsa.pub to the ~/.ssh/authorized_keys file of your target user, and SSH from that point on will not ask for credentials. In your example, this will work out to:
Only once
# On your source machine
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub
# Copy this to clip board
# On somehost.com
su - someuser
# edit ~/.ssh/authorized_keys and paste what you had copied from previous step
From now on, you can now just run
ssh someuser#somehost.com "sh -c 'cd /some/dir; command.sh'"
and not be prompted for credentials.
fabric is a fine choice, as others have pointed out. there is also pexpect which might be more what you're looking for.
You can play with autoexpect. It creates expect script (script language intended to handle interaction with user). Run
autoexpect ssh me#somehost.com
followed by rest of commands. Script script.exp will be created.
Please note, that exact results of input and output will be recorded by the script. If output may differ from execution to execution, you'll need to modify a bit generated script.
As Daniel pointed out you need to have a secure way of doing ssh and sudo on the boxes. Those items are universal to dealing with linux/unix boxes. Once you've tackled that you can use fabric. It's a python based tool to do automation.
You can set stuff up in your ~/.ssh/config
For example:
Host somehost
User test
See ssh_config(5) for more info.
Next, you can generate a SSH key using ssh-keygen(1), run ssh-agent(1), and use that for authentication.
If you want to run a command on a remote machine, you can just use something like:
$ ssh somehost "sh myscript.sh ${myparameter}".
I hope this at least points you in the right direction :)
If you need sudo access, then there are obvious potential security issues though ... You can use ChrootDirectory on a per user basis inside a Match block though. See sshd_config(5) for info.
try module paramiko. This can meet your requirement.

Categories

Resources