Using ssh instead on telnetlib ... what are the ways

Ganesan R rganesan at myrealbox.com
Mon Dec 30 22:48:10 EST 2002


>>>>> "Amol" == Amol Sonaikar <amol at kenati.com> writes:

> Hi All
> We are using a script and passing and ip address and filename to it. We are 
> using Telnetlib to write our commands on the telnet instance. We need to use 
> SSH instead of telnet now.  Some part I have posted below.
> -----------------------------------------------------------------------------
> #!/usr/bin/env python2.1
> import sys
> import telnetlib
> import os

> tn = telnetlib.Telnet("")

> tn.open( sys.argv[1] )
> tn.write( "/usr/sbin/cli\n" )
> tn.read_until("Username: ")
> tn.write("admin\n")
> tn.read_until("Password: ")
> tn.write("admin\n")
> -----------------------------------------------------------------------------

> I am using telnet variable only to connect to, write on the application
> which I opened using telnet. What library or method I should use so thet I
> can access and connect using 'ssh' ???

> I have tried os.system but it directly executes and opens prompt in front 
> where it has to do it on backend. How can I use any other method ???

Try pexpect (http://pexpect.sf.net). Your script then becomes

--------------------------------------------------------------------
#!/usr/bin/env python2.1
import sys
import pexpect

ssh = pexpect.spawn("ssh admin@" + sys.argv[1])
ssh.setlog(sys.stdout)                         # for debugging
ret = ssh.expect(["continue connecting (yes/no)?", "password:"])
if ret == 0:
    ssh.sendline("yes")
    ssh.expect("password: ")
ssh.sendline("admin")
--------------------------------------------------------------------

You get the idea :-).

-- 
Ganesan R 




More information about the Python-list mailing list