3

I am new to python and if the question is very nooby i apologize. Simply put i need to write a script that connects to remote via ssh then telnets to localhost and execute a command on a shell there.

I am using Python 2.4.3. I have read alot of similar questions here , and alot of people suggest to use modules such as Paramiko, Pexpect etc. However this is out of question - i am supposed to use only "native" 2.4.3 libraries. I have tried messing around with subprocess module and i have managed to connect to remote shell (however i need to provide a password - and i would like to avoid that by providing a password in script for example) - but still i need to do a telnet to localhost and execute few commands on a different shell.

Could someone be so kind and give me some hints? Thanks in advance.

TL;DR I am looking for python alternative to this bash command :

./sshpass -p password ssh username@$ip -t "(sleep 1;echo "command" ; sleep 1) | telnet localhost $port;exit;bash" >> testing.txt

6
  • 1
    What are you actually trying to accomplish? If you are logged in over ssh it makes no sense to telnet localhost -- it just adds an insecure local second connection. Commented Feb 3, 2015 at 9:36
  • It is for my programming course on my technical university. Basically we have fedora installed on a server with various networking devices connected via localhost to it. What we need to do is to log to the server then connect to these devices, execute a command on their custom shells and save an output to a file on our desktops. We did a same thing in bash 2 weeks ago - and thats how we did it.Now we need to do the same but with python. Commented Feb 3, 2015 at 9:44
  • Are you required to use Python inside the ssh connection? That makes no sense really. Once you are in, you are in a shell. Commented Feb 3, 2015 at 9:48
  • But with ssh port forwarding you could expose the ports and connect to those using native socket calls. Is that what you want? Commented Feb 3, 2015 at 9:51
  • What we are supposed to do is to write a script in python that connects to a device,execute a command and save the output to a file. This device is connected as localhost with given port to a remote server with Fedora with given ip. I am sorry , i am only a second year student so please forgive my ignorance - not sure what you mean. With bash it was fairly simple because i used commands like this : ./sshpass -p password ssh username@$ip -t "(sleep 1;echo "command" ; sleep 1) | telnet localhost $port;exit;bash" >> testing.txt - i would like to do something similar in python. Commented Feb 3, 2015 at 9:55

1 Answer 1

2

after a simple search:

telnet: link

import getpass
import sys
import telnetlib

HOST = "hostname"

user = raw_input("Enter your remote account: ")

password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")

tn.write(user + "\n")

if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("ls\n")

tn.write("exit\n")

print tn.read_all()

ssh: link

import pxssh
import getpass
try:                                                            
    s = pxssh.pxssh()
    hostname = raw_input('hostname: ')
    username = raw_input('username: ')
    password = getpass.getpass('password: ')
    s.login (hostname, username, password)
    s.sendline ('uptime')   # run a command
    s.prompt()             # match the prompt
    print s.before          # print everything before the prompt.
    s.sendline ('ls -l')
    s.prompt()
    print s.before
    s.sendline ('df')
    s.prompt()
    print s.before
    s.logout()
except pxssh.ExceptionPxssh, e:
    print "pxssh failed on login."
    print str(e)
Sign up to request clarification or add additional context in comments.

2 Comments

Damn , there is no pxssh on my machine and i am not supposed to use any other modules besides what i have :S Is there a way to connect via ssh with subprocess and provide a password also ?
@sztyrymytyry you can try supplying the password to stdin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.