Re: Getting to an SSH account over a HTTP proxy

2007-01-24 Thread amadain
use pexpect to set the prompt after the login.

class Login(General):
"""Class spawning an ssh expect instance"""
def __init__(self, user, host, pwd, cfg_name=None, log=None):
if cfg_name:
self.testcell = test_config(cfg_name)
self.spawn = pexpect.spawn("ssh [EMAIL PROTECTED]" % (user, 
host), [], 100)
if log:
self.spawn.logfile = log
sshreply = self.spawn.expect(["Last login", "assword", 
"connecting"])

if sshreply == 1:
self.spawn.sendline(pwd)
self.spawn.expect("Last login")
elif sshreply == 2:
time.sleep(0.1)
self.spawn.sendline("yes")
print self.spawn.after
Login(user, host, cfg_name, log)
time.sleep(1)
self.prompt=prompt_chg(self.spawn, "PROMPT:")
self.spawn.sendline("uname -a")
self.spawn.expect(self.prompt)


On Jan 23, 10:28 am, Willi Richert <[EMAIL PROTECTED]> wrote:
> Am Dienstag, 23. Januar 2007 02:16 schrieb Nanjundi:
>
>
>
> > BJörn Lindqvist wrote:
> > > I want to use Python to connect to a SSH account over a HTTP proxy to
> > > automate some operations. I thought paramiko would be able to do that,
> > > but it can not (it seems).
>
> > > Is there some other Python module that can do what I want?
>
> > > --
> > > mvh Björn
>
> > Did you take a look at twisted library?
> > twistedmatrix.com
> >http://twistedmatrix.com/projects/core/documentation/howto/clients.html
>
> > I haven't tried to connect over port 80, but its worth a try.
>
> > -NIf you need it for automation you might want to 
> > usepexpect:http://pexpect.sourceforge.net/
>
> It listens to the tty-stream and simulates a user typing in commands. It is
> very useful, e.g. if you want to start processes on many machines over ssh.
> If there are gateways/firewalls between - no problem just use a
> second "sendline('ssh [EMAIL PROTECTED]')"
>
> The only problem is the regular expression: If e.g. you use a custom $PS1
> variable for your prompt you have to account for that.
> 
> Regards,
> wr

-- 
http://mail.python.org/mailman/listinfo/python-list


replacing substrings within strings

2007-02-14 Thread amadain
Hi
I was wondering if there was a nicer way to swap the first 2
characters in a string with the 4th and 5th characters other than:

darr=list("010203040506")
aarr=darr[:2]
barr=darr[4:6]
darr[:2]=barr
darr[4:6]=aarr
result="".join(darr)

The above code works fine but I was wondering if anybody had another
way of doing this?

A

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: replacing substrings within strings

2007-02-14 Thread amadain
On Feb 14, 12:16 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> amadain wrote:
> > Hi
> > I was wondering if there was a nicer way to swap the first 2
> > characters in a string with the 4th and 5th characters other than:
>
> > darr=list("010203040506")
> > aarr=darr[:2]
> > barr=darr[4:6]
> > darr[:2]=barr
> > darr[4:6]=aarr
> > result="".join(darr)
>
> > The above code works fine but I was wondering if anybody had another
> > way of doing this?
>
> You can do it like this:
>
> darr=list("010203040506")
> darr[:2], darr[4:5] = darr[4:6], darr[:2]
> result="".join(darr)
> print result
>
> Diez



Thats the same code. I was wondering if the string manipulation can be
done without an excursion into the list world.

A

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: replacing substrings within strings

2007-02-14 Thread amadain
Thanks all. I usually turn strings into arrays for processing. I was
looking to see if that was the best way to do it from others that use
python. No one else uses python in my company so its nice to get
different points of view from other python users from lists like this.
A

-- 
http://mail.python.org/mailman/listinfo/python-list


multi processes

2007-02-14 Thread amadain
Hi
Heres a poser. I want to start a program 4 times at exactly the same
time (emulating 4 separate users starting up the same program). I am
using pexpect to run the program from 4 separate locations accross the
network. How do I start the programs running at exactly the same time?
I want to time how long it takes each program to complete and to show
if any of the program initiations failed. I also want to check for
race conditions. The program that I am running is immaterial for this
question - it could be mysql running queries on the same database for
example. Using threading, you call start() to start each thread but if
I call start on each instance in turn I am not starting
simultaneously.
A

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multi processes

2007-02-14 Thread amadain
On Feb 14, 3:32 pm, [EMAIL PROTECTED] wrote:
> On Feb 14, 7:53 am, "amadain" <[EMAIL PROTECTED]> wrote:
>
> > Hi
> > Heres a poser. I want to start a program 4 times at exactly the same
> > time (emulating 4 separate users starting up the same program). I am
> > using pexpect to run the program from 4 separate locations accross the
> > network. How do I start the programs running at exactly the same time?
> > I want to time how long it takes each program to complete and to show
> > if any of the program initiations failed. I also want to check for
> > race conditions. The program that I am running is immaterial for this
> > question - it could be mysql running queries on the same database for
> > example. Using threading, you call start() to start each thread but if
> > I call start on each instance in turn I am not starting
> > simultaneously.
> > A
>
> Standard answers about starting anything at *exactly* the same time
> aside, I would expect that the easiest answer would be to have a fifth
> controlling program in communication with all four, which can then
> send a start message over sockets to each of the agents at the same
> time.
>
> There are several programs out there which can already do this. One
> example, Grinder, is designed for this very use (creating concurrent
> users for a test). It's free, uses Jython as it's scripting language,
> and even is capable of keeping track of your times for you. IMO, it's
> worth checking out.
>
> http://grinder.sourceforge.net


Thank you. I'll check that out.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pexpect regex help

2007-02-23 Thread amadain
On Feb 21, 11:15 pm, [EMAIL PROTECTED] wrote:
> On Feb 21, 6:13 pm, [EMAIL PROTECTED] wrote:
>
>
>
> > I have apexpectscript to walk through a cisco terminal server and I
> > was hoping to get some help with this regex because I really suck at
> > it.
>
> > This is the code:
>
> > index = s.expect(['login: ',pexpect.EOF,pexpect.TIMEOUT])
> > if index == 0:
> > m = re.search('((#.+\r\n){20,25})(\s.*)',
> > s.before)  #<-- MY PROBLEM
> > print m.group(3),
> > print ' %s %s' % (ip[0], port)
> > s.send(chr(30))
> > s.sendline('x')
> > s.sendline('disco')
> > s.sendline('\n')
> > elif index == 1:
> > print s.before
> > elif index == 2:
> > print
> > print '%s %s FAILED' % (ip[0], port)
> > print 'This host may be down or locked on the TS'
> > s.send(chr(30))
> > s.sendline('x')
> > s.sendline('disco')
> > s.sendline('\n')
>
> > This is attempting to match the hostname of the connected host using
> > the output of a motd file which unfortunately is not the same
> > everywhere...  It looks like this:
>
> > #
> > #   This system is the property
> > of: #
> > #
> > #
> > #DefNet
> > #
> > #
> > #
> > #   Use of this system is for authorized users
> > only.#
> > #   Individuals using this computer system without authority, or
> > in #
> > #   excess of their authority, are subject to having all of
> > their   #
> > #   activities on this system monitored and recorded by
> > system  #
> > #
> > personnel.  #
> > #
> > #
> > #   In the course of monitoring individuals improperly using
> > this   #
> > #   system, or in the course of system maintenance, the
> > activities  #
> > #   of authorized users may also be
> > monitored.  #
> > #
> > #
> > #   Anyone using this system expressly consents to such
> > monitoring  #
> > #   and is advised that if such monitoring reveals
> > possible #
> > #   evidence of criminal activity, system personnel may provide
> > the #
> > #   evidence of such monitoring to law enforcement
> > officials.   #
> > #
>
> > pa-chi1 console login:
>
> > And sometimes it looks like this:
>
> > #
> > #   This system is the property
> > of: #
> > #
> > #
> > #DefNet
> > #
> > #
> > #
> > #   Use of this system is for authorized users
> > only.#
> > #   Individuals using this computer system without authority, or
> > in #
> > #   excess of their authority, are subject to having all of
> > their   #
> > #   activities on this system monitored and recorded by
> > system  #
> > #
> > personnel.  #
> > #
> > #
> > #   In the course of monitoring individuals improperly using
> > this   #
> > #   system, or in the course of system maintenance, the
> > activities  #
> > #   of authorized users may also be
> > monitored.  #
> > #
> > #
> > #   Anyone using this system expressly consents to such
> > monitoring  #
> > #   and is advised that if such monitoring reveals
> > possible #
> > #   evidence of criminal activity, system personnel may provide
> > the #
> > #   evidence of such monitoring to law enforcement
> > officials.   #
> > #
> > pa11-chi1 login:
>
> > The second one works and it will print out pa11-chi1  but when there
> > is a space or console is in the output it wont print anything or it
> > wont match anything...I want to be able to match just the hostname
> > and print it out.
>
> > Any ideas?
>
> > Thanks,
>
> > Jonathan
>
> It is also posted here more clearly and formatted as it would appear
> on the terminal:  http://www.pastebin.ca/366822



what about using s.before.split("\r\n")[-1]?

A

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pexpect regex help

2007-02-23 Thread amadain
On Feb 23, 8:46 am, "amadain" <[EMAIL PROTECTED]> wrote:
> On Feb 21, 11:15 pm, [EMAIL PROTECTED] wrote:
>
>
>
> > On Feb 21, 6:13 pm, [EMAIL PROTECTED] wrote:
>
> > > I have apexpectscript to walk through a cisco terminal server and I
> > > was hoping to get some help with this regex because I really suck at
> > > it.
>
> > > This is the code:
>
> > > index = s.expect(['login: ',pexpect.EOF,pexpect.TIMEOUT])
> > > if index == 0:
> > > m = re.search('((#.+\r\n){20,25})(\s.*)',
> > > s.before)  #<-- MY PROBLEM
> > > print m.group(3),
> > > print ' %s %s' % (ip[0], port)
> > > s.send(chr(30))
> > > s.sendline('x')
> > > s.sendline('disco')
> > > s.sendline('\n')
> > > elif index == 1:
> > > print s.before
> > > elif index == 2:
> > > print
> > > print '%s %s FAILED' % (ip[0], port)
> > > print 'This host may be down or locked on the TS'
> > > s.send(chr(30))
> > > s.sendline('x')
> > > s.sendline('disco')
> > > s.sendline('\n')
>
> > > This is attempting to match the hostname of the connected host using
> > > the output of a motd file which unfortunately is not the same
> > > everywhere...  It looks like this:
>
> > > #
> > > #   This system is the property
> > > of: #
> > > #
> > > #
> > > #DefNet
> > > #
> > > #
> > > #
> > > #   Use of this system is for authorized users
> > > only.#
> > > #   Individuals using this computer system without authority, or
> > > in #
> > > #   excess of their authority, are subject to having all of
> > > their   #
> > > #   activities on this system monitored and recorded by
> > > system  #
> > > #
> > > personnel.  #
> > > #
> > > #
> > > #   In the course of monitoring individuals improperly using
> > > this   #
> > > #   system, or in the course of system maintenance, the
> > > activities  #
> > > #   of authorized users may also be
> > > monitored.  #
> > > #
> > > #
> > > #   Anyone using this system expressly consents to such
> > > monitoring  #
> > > #   and is advised that if such monitoring reveals
> > > possible #
> > > #   evidence of criminal activity, system personnel may provide
> > > the #
> > > #   evidence of such monitoring to law enforcement
> > > officials.   #
> > > #
>
> > > pa-chi1 console login:
>
> > > And sometimes it looks like this:
>
> > > #
> > > #   This system is the property
> > > of: #
> > > #
> > > #
> > > #DefNet
> > > #
> > > #
> > > #
> > > #   Use of this system is for authorized users
> > > only.#
> > > #   Individuals using this computer system without authority, or
> > > in #
> > > #   excess of their authority, are subject to having all of
> > > their   #
> > > #   activities on this system monitored and recorded by
> > > system  #
> > > #
> > > personnel.  #
> > > #
> > > #
> > > #   In the course of monitoring individuals improperly using
> > > this   #
> > > #   system, or in the course of system maintenance, the
> > > activities  #
> > > #   of authorized users may also be
> > > monitored.  #
> > > #
> > > #
> > > #   Anyone using this system expressly consents to such
> > > monitoring  #
> > > #   and is advised that if such monitoring reveals
> > > possible #
> > > #   evidence of criminal activity, system personnel may provide
> > > the #
> > > #   evidence of such monitoring to law enforcement
> > > officials.   #
> > > #
> > > pa11-chi1 login:
>
> > > The second one works and it will print out pa11-chi1  but when there
> > > is a space or console is in the output it wont print anything or it
> > > wont match anything...I want to be able to match just the hostname
> > > and print it out.
>
> > > Any ideas?
>
> > > Thanks,
>
> > > Jonathan
>
> > It is also posted here more clearly and formatted as it would appear
> > on the terminal:  http://www.pastebin.ca/366822
>
> what about using s.before.split("\r\n")[-1]?
>
> A



result=[x for x in s.before.split("\r\n") if x != ""]
print result[-1]

should cover the blank line problem

A

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pexpect regex help

2007-02-23 Thread amadain
On Feb 23, 8:53 am, "amadain" <[EMAIL PROTECTED]> wrote:
> On Feb 23, 8:46 am, "amadain" <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Feb 21, 11:15 pm, [EMAIL PROTECTED] wrote:
>
> > > On Feb 21, 6:13 pm, [EMAIL PROTECTED] wrote:
>
> > > > I have apexpectscript to walk through a cisco terminal server and I
> > > > was hoping to get some help with this regex because I really suck at
> > > > it.
>
> > > > This is the code:
>
> > > > index = s.expect(['login: ',pexpect.EOF,pexpect.TIMEOUT])
> > > > if index == 0:
> > > > m = re.search('((#.+\r\n){20,25})(\s.*)',
> > > > s.before)  #<-- MY PROBLEM
> > > > print m.group(3),
> > > > print ' %s %s' % (ip[0], port)
> > > > s.send(chr(30))
> > > > s.sendline('x')
> > > > s.sendline('disco')
> > > > s.sendline('\n')
> > > > elif index == 1:
> > > > print s.before
> > > > elif index == 2:
> > > > print
> > > > print '%s %s FAILED' % (ip[0], port)
> > > > print 'This host may be down or locked on the TS'
> > > > s.send(chr(30))
> > > > s.sendline('x')
> > > > s.sendline('disco')
> > > > s.sendline('\n')
>
> > > > This is attempting to match the hostname of the connected host using
> > > > the output of a motd file which unfortunately is not the same
> > > > everywhere...  It looks like this:
>
> > > > #
> > > > #   This system is the property
> > > > of: #
> > > > #
> > > > #
> > > > #DefNet
> > > > #
> > > > #
> > > > #
> > > > #   Use of this system is for authorized users
> > > > only.#
> > > > #   Individuals using this computer system without authority, or
> > > > in #
> > > > #   excess of their authority, are subject to having all of
> > > > their   #
> > > > #   activities on this system monitored and recorded by
> > > > system  #
> > > > #
> > > > personnel.  #
> > > > #
> > > > #
> > > > #   In the course of monitoring individuals improperly using
> > > > this   #
> > > > #   system, or in the course of system maintenance, the
> > > > activities  #
> > > > #   of authorized users may also be
> > > > monitored.  #
> > > > #
> > > > #
> > > > #   Anyone using this system expressly consents to such
> > > > monitoring  #
> > > > #   and is advised that if such monitoring reveals
> > > > possible #
> > > > #   evidence of criminal activity, system personnel may provide
> > > > the #
> > > > #   evidence of such monitoring to law enforcement
> > > > officials.   #
> > > > #
>
> > > > pa-chi1 console login:
>
> > > > And sometimes it looks like this:
>
> > > > #
> > > > #   This system is the property
> > > > of: #
> > > > #
> > > > #
> > > > #DefNet
> > > > #
> > > > #
> > > > #
> > > > #   Use of this system is for authorized users
> > > > only.#
> > > > #   Individuals using this computer system without authority, or
> > > > in #
> > > > #   excess of their authority, are subject to having all of
> > > > their   #
> > > > #   activities on this system monitored and recorded by
> > > > system  #
> > > > #
> > > > personnel.  #
> > > > #
> > > > #
> >

xml.sax parsing elements with the same name

2010-01-11 Thread amadain
I have an event log with 100s of thousands of entries with logs of the
form:


   
  
  
  
   
  
  
  
  
   
  
  
  


I am using xml.sax to parse the event log. The trouble with the file
above is when I parse for result value I get the last result value
(Blocked from above). I want to get the result value triggered (the
second in the event).

my code is as follows:

def startElement(self, name, attrs):
if name == 'event':
self.eventTime = attrs.get('eventTimestamp',"")
self.eventUniqueId = attrs.get('uniqueId', "")
if name == 'result':
self.resultValue = attrs.get('value',"")
return

def endElement(self, name):
if name=="event":
result= eval(self.filter)
if result:
...

How do I get the result value I require when events have the same
names like above?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml.sax parsing elements with the same name

2010-01-11 Thread amadain
On Jan 11, 7:26 pm, John Bokma  wrote:
> amadain  writes:
> > I have an event log with 100s of thousands of entries with logs of the
> > form:
>
> >  > uniqueId="1261124569.35725_PFS_1_1340035961">
> >    
> >       
> >           
> >               
> >                    
> >               
> >           
> >           
> >               
> >                    
> >               
> >           
> >       
> > 
>
> > I am using xml.sax to parse the event log. The trouble with the file
> > above is when I parse for result value I get the last result value
> > (Blocked from above). I want to get the result value triggered (the
> > second in the event).
>
> > my code is as follows:
>
> >     def startElement(self, name, attrs):
> >         if name == 'event':
> >             self.eventTime = attrs.get('eventTimestamp',"")
> >             self.eventUniqueId = attrs.get('uniqueId', "")
> >         if name == 'result':
> >             self.resultValue = attrs.get('value',"")
> >         return
>
> >     def endElement(self, name):
> >         if name=="event":
> >             result= eval(self.filter)
> >             if result:
> >            ...
>
> > How do I get the result value I require when events have the same
> > names like above?
>
> You have to keep track if you're inside a filters section, and keep
> track of the filter elements (first, second, etc.) assuming you want the
> result value of the first filter.
>
> --
> John Bokma
>
> Read my blog:http://johnbokma.com/
> Hire me (Perl/Python):http://castleamber.com/

how do I keep track? The first result value is outside a filters
section and the rest are. Do you mean something like:

def startElement(self, name, attrs):
if name == 'event':
self.eventTime = attrs.get('eventTimestamp',"")
self.eventUniqueId = attrs.get('uniqueId', "")
if name == 'result':
self.resultValue = attrs.get('value',"")
if name == filters:
if name == 'result':
self.resultValueF = attrs.get('value',"")
return

A
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml.sax parsing elements with the same name

2010-01-11 Thread amadain
On Jan 11, 9:03 pm, John Bokma  wrote:
> amadain  writes:
> > On Jan 11, 7:26 pm, John Bokma  wrote:
> >> amadain  writes:
> >> >  >> > uniqueId="1261124569.35725_PFS_1_1340035961">
> >> >    
> >> >       
> >> >           
> >> >               
> >> >                    
> >> >               
> >> >           
> >> >           
> >> >               
> >> >                    
> >> >               
> >> >           
> >> >       
> >> > 
> > how do I keep track? The first result value is outside a filters
> > section and the rest are. Do you mean something like:
>
> >     def startElement(self, name, attrs):
> >         if name == 'event':
> >             self.eventTime = attrs.get('eventTimestamp',"")
> >             self.eventUniqueId = attrs.get('uniqueId', "")
> >         if name == 'result':
> >                 self.resultValue = attrs.get('value',"")
> >         if name == filters:
> >             if name == 'result':
> >                 self.resultValueF = attrs.get('value',"")
> >         return
>
> I was thinking about something like:
>
> self.filterIndex = 0
>
> in startElement:
>
>     if name == 'filter':
>        self.filterIndex += 1
>        return
>     if name == 'result' and self.filterIndex == 1:
>        ...  = attrs.get('value', '')
>
> in EndElement
>
>    if name == 'filters':
>       self.filterIndex = 0
>
> If you want the result of the first filter in filters
>
> --
> John Bokma
>
> Read my blog:http://johnbokma.com/
> Hire me (Perl/Python):http://castleamber.com/e e

Thank you. I will try that
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: urllib2.URLError: error using twill with python

2009-06-29 Thread amadain
On Jun 8, 12:58 pm, Steven D'Aprano  wrote:
> On Mon, 08 Jun 2009 12:14:18 +0100, Mark Devine wrote:
> > Hi
> > I wonder if someone could point me in the right direction. I used the
> > following code to access gmail but I got a
> >          urllib2.URLError: 
> > error when I ran it. I have included the Traceback
>
> > import twill, string, os
> > b=twill.commands.get_browser()
> > b.set_agent_string("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB;
> > rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14") b.clear_cookies()
> > b.go('http://www.gmail.com')
> > f=b.get_form("1")
> > b.showforms()
> > f['Email']= email
> > f['Passwd'] =password
> > b.clicked(f, f)
> > b.submit()
>
> My bet is that the above is not the actual code you have run. I bet that
> the offending line is actually something like the following:
>
> b.go("'http://www.gmail.com";)
>
> Note that there is a single character difference.
>
> Consider the last two lines of the traceback:
>
> >     raise URLError('unknown url type: %s' % type)
> > urllib2.URLError: 
>
> It seems to be saying that the url type is 'http -- note the leading
> single quote.
>
> --
> Steven

Actually that is the exact code run from a python shell. Try it
yourself. I could not find anybody who successfully automated sending
a gmail through python with twill so if you know how I would greatly
appreciate any pointers.
-- 
http://mail.python.org/mailman/listinfo/python-list