[Twisted-Python] twisted python conch SSH shell?
Eli Criffield
elicriffield at gmail.com
Thu Aug 21 09:58:08 EDT 2008
conchClient.py is libarary. Running it from the command line was an
afterthought just for testing.
To run multiple commands in the same session you just give sshCmds a
list of commands to run and it will return the output of all of them.
Here is an example script that creates a function to up a port and
another to return the wwn (this is from a SAN switch) of a port.
#!/usr/bin/env python
# Eli Criffield
__version__ = 0.1186070837
__author__ = 'Eli Criffield <python AT zendo.net>'
import sys
import re
import time
import os
from conchClient import sshCmds
dryRun = False
Confirm = True
echo = False
class reError(Exception):
pass
def execList(switch,cmds,change=False):
global echo, dryRun, Confirm
# if change is False, the commands arn't suppose to do any damage
# always exec them, they need the info
if not change:
return sshCmds(switch,cmds,echo=echo)
if dryRun:
print 'DRY RUN'
print 'ssh %s'%switch
for cmd in cmds:
print cmd
print 'ENTER to continue:',
sys.stdin.readline()
return ('dryrun','')
if Confirm:
print "I'm going to run:"
print '\nssh %s'%switch
for cmd in cmds:
print cmd
ans = raw_input('Does that look ok (yes/no)? ')
while ans.lower() not in ('yes', 'no'):
ans = raw_input("Please type 'yes' or 'no': ")
if ans == 'no':
print 'exiting out'
sys.exit(1)
else:
return sshCmds(switch,cmds,echo=echo)
# No dryRun or Confirm just execute it
return sshCmds(switch,cmds,echo=echo)
def upPort(switch,port):
cmds=[ 'config term', 'interface fc%s'%port, 'no shutdown' ,'end'
,'exit' ]
execList(switch,cmds,change=True)
def wwnFromPort(switch,port):
portre = re.compile(r'%s '%port)
linere = re.compile(r'(\S*)\s*(\S*)\s*(\S*)\s*(\S*)\s*(\S*)')
(out,err) = execList(switch,['show flogi database'])
for line in out.split('fc'):
if portre.match(line):
#print 'line:',line #DEBUG
wwn = linere.match(line).groups()[3]
#print wwn #DEBUG
return wwn
On Thu, Aug 21, 2008 at 2:07 AM, The Noob <thenoob06 at gmail.com> wrote:
> Hello,
>
> If I understand your code, you can't find a way to run a real shell on Cisco
> ?
> For example if I connect to a router, I send a "sh log" , " conf t "
> commands but I do not know them before. With the actual script:
> conchClient.py 192.168.0.254 sh log
> The script disconnects us from the router
> conchClient.py 192.168.0.254 conf t
> The script disconnects us disconnect from the router
> I think the problem with your script it is for each command you are
> disconnected The best will be to find a solution to do this:
> conchClient.py 192.168.0.254 sh log
> conchClient.py 192.168.0.254 conf t
> conchClient.py 192.168.0.254 exit
> disconnect from server
>
> I try to do this with a custom ssh server which permits to connect to others
> ssh servers and have a shell. But I don't know how to communicate between my
> custom ssh server and my child ( an expect script which launch ssh root at host
> and permits to have a real shell)
>
>
> Best Regards
> TheNoob06
>
>
> 2008/8/21 Eli Criffield <elicriffield at gmail.com>
>>
>> check out
>>
>> http://twistedmatrix.com/pipermail/twisted-python/2007-July/015793.html
>>
>> Eli Criffield
>>
>> On Tue, Aug 12, 2008 at 3:40 PM, Bill Zeller <wzeller at cs.princeton.edu>
>> wrote:
>> > Hi,
>> >
>> > I've been playing around with Twisted's Conch client (which looks
>> > extremely
>> > well done, thanks!). I've been able to connect to a conch server and
>> > also
>> > connect to other ssh servers and execuate various commands. I'd like to
>> > be
>> > able to connect to an arbitrary SSH server and run /bin/bash.
>> >
>> > I'm able to execute a command like so:
>> >
>> > ---------------------
>> > class CommandChannel(channel.SSHChannel):
>> > ....
>> > def channelOpen(self, data):
>> > self.conn.sendRequest(
>> > self, 'exec', common.NS(self.command),
>> > wantReply=True).addCallback(
>> > self._gotResponse)
>> > ---------------------
>> >
>> > Looking through the SSH spec (connect protocol, rfc4254), it looks like
>> > shells are opened with this command.
>> >
>> > ---------------------
>> >
>> > byte SSH_MSG_CHANNEL_REQUEST
>> >
>> > uint32 recipient channel
>> > string "shell"
>> > boolean want reply
>> >
>> > ---------------------
>> >
>> > It doesn't look like Conch supports these requests (here's how
>> > CHANNEL_REQUESTs are currently sent out)
>> >
>> > ---------------------
>> > (Twisted-8.1.0)
>> > twisted/conch/ssh/connection.py (413):
>> > self.transport.sendPacket(MSG_CHANNEL_REQUEST, struct.pack('>L',
>> >
>> > self.channelsToRemoteChannel[channel])
>> > +
>> > common.NS(requestType)+chr(wantReply)
>> > + data)
>> > ---------------------
>> >
>> > I may be misunderstanding how remote shells are started, so any help
>> > would
>> > be appreciated.
>> >
>> > Again, I'm trying to launch an interactive /bin/bash shell from a Conch
>> > SSH
>> > client on an arbitrary remote SSH server.
>> >
>> > Thanks for your time.
>> >
>> > Best Regards,
>> > Bill Zeller
>> >
>> > _______________________________________________
>> > Twisted-Python mailing list
>> > Twisted-Python at twistedmatrix.com
>> > http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>> >
>> >
>>
>> _______________________________________________
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
More information about the Twisted-Python
mailing list