os.popen output different from native shell output

2009-08-25 Thread nickname
Hi all,
   I am a relative newbie to python, I am using os.popen to run an
ls command. The output that I get using the read() function is
different in look and feel from when I run the ls command natively
from the shell (not via python). I display the ouput via python by
using the print function on the variable that accepts the os.popen
().read() function.

For example:

output from native shell (as seen on the bash shell)

file1 file2 dir1(highlighted in blue color)
file3longnamewhichwillcausenextfiletoappearonnextline

file 4

output from python (as seen on the bash shell)

file1
file2
dir1 (no blue color)
file3longnamewhichwillcausenextfiletoappearonnextline
file4

Is there an easy way to "mirror" the output. When python displays the
output, how can it tell the bash shell that some of the entries are
directories and they should appear blue on the bash shell, and that
everything should not be appearing on 1 column only.

Thanks for any pointers.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.popen output different from native shell output

2009-08-25 Thread nickname
On Aug 25, 6:16 am, Nobody  wrote:
> On Tue, 25 Aug 2009 01:36:08 -0700, nickname wrote:
> >        I am a relative newbie to python, I am using os.popen to run an
> > ls command. The output that I get using the read() function is
> > different in look and feel from when I run the ls command natively
> > from the shell (not via python).
>
> As others have pointed out, the default behaviour of ls is different if
> its output is a terminal.
>
> > Is there an easy way to "mirror" the output. When python displays the
> > output, how can it tell the bash shell that some of the entries are
> > directories and they should appear blue on the bash shell, and that
> > everything should not be appearing on 1 column only.
>
> You can get the terminal-style behaviour even when using a pipe with:
>
>         ls -x --color
>
> But why are you reading this information into Python then writing it
> back out to the terminal?
>
> If you're planning on processing the output within Python, both the
> multi-column format and the escape sequences used for colour will make
> such processing awkward.
>
> If you want to enumerate the contents of a directory within Python, use
> os.listdir().
>
> If you want to generate coloured output, use the curses module, e.g.:
>
> #!/usr/bin/env python
>
> import sys
> import curses
>
> curses.setupterm()
> setaf = curses.tigetstr('setaf') or ""
> setab = curses.tigetstr('setab') or ""
> origp = curses.tigetstr('op') or ""
>
> def fg(c):
>     sys.stdout.write(curses.tparm(setaf, c))
>
> def bg(c):
>     sys.stdout.write(curses.tparm(setab, c))
>
> def orig():
>     sys.stdout.write(origp)
>
> # example
> bg(curses.COLOR_BLUE)
> fg(curses.COLOR_YELLOW)
> print "hello, world"
> orig()

wow guys! thanks for all the great leads! this is awesome!

The reason why I want to do this is because I am going to do a little
project. I will write a python script called ls which will log the
time and username and then will show the actual ls output. I want this
to be transparent and so want to throw the ls output (via python)
exactly as it will be in native shell execution.

I know there's history files I can look up, but I just am exploring my
own intermediate-logging-layer the functionality for which is executed
right before the actual command is executed.

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


Re: os.popen output different from native shell output

2009-08-25 Thread nickname
On Aug 25, 11:57 am, Thomas Guettler 
wrote:
> In one of the first chapters of "Advanced programming in the unix
> environment (second edition)" there is explained how a unix shell works.
>
> You could write you own shell using python. This way the python
> interpreter gets stared only once, and not for every call to "ls".
>
> Have fun,
>   Thomas
>
> nickname schrieb:
>
> > wow guys! thanks for all the great leads! this is awesome!
>
> > The reason why I want to do this is because I am going to do a little
> > project. I will write a python script called ls which will log the
> > time and username and then will show the actual ls output. I want this
> > to be transparent and so want to throw the ls output (via python)
> > exactly as it will be in native shell execution.
>
>

very good point, I will look it up :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


how to log connection refused attempt with python-paramiko

2009-09-09 Thread nickname
hello all,
I am getting to grips with paramiko. I am trying to open a
ssh connection to my server 127.0.0.1 using paramiko and want to
specify the password via the program itself. I want to generate  and
change passwds via the program very frequently and hence am avoiding a
key based approach.

I am using

[code]
def send_ssh(ip):
  ssh = paramiko.SSHClient()
  ssh.connect('127.0.0.1', username='username', password='thispasswd',
port=22)
[/code]

and I get

Traceback (most recent call last):
  File "./client.py", line 76, in 
send_ssh(ip)
  File "./client.py", line 46, in send_ssh
ssh.connect('127.0.0.1', username='username',
password='thispasswd', port=22)
  File "/var/lib/python-support/python2.5/paramiko/client.py", line
299, in connect
self._policy.missing_host_key(self, hostname, server_key)
  File "/var/lib/python-support/python2.5/paramiko/client.py", line
83, in missing_host_key
raise SSHException('Unknown server %s' % hostname)
paramiko.SSHException: Unknown server 127.0.0.1

The screen just "hangs", looks like it is waiting/timing-out and then
if I use ctrl-C, it ouputs

Exception exceptions.KeyboardInterrupt in  ignored

In case I specify a different port, like 5. I got a socket.error:
(111, 'Connection refused') . Is there a log file where this is
recorded. I looked up /var/log/auth.log and syslog, messages but could
not find a reference to this erroneous login attempt.

[code]
def send_ssh(ip):
  ssh = paramiko.SSHClient()
  ssh.connect('127.0.0.1', username='testusername',
password='thisshouldbethepasswd', port=5)
[/code]

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


Re: how to log connection refused attempt with python-paramiko

2009-09-09 Thread nickname
On Sep 9, 3:35 pm, nickname  wrote:
> hello all,
>             I am getting to grips with paramiko. I am trying to open a
> ssh connection to my server 127.0.0.1 using paramiko and want to
> specify the password via the program itself. I want to generate  and
> change passwds via the program very frequently and hence am avoiding a
> key based approach.
>
> I am using
>
> [code]
> def send_ssh(ip):
>   ssh = paramiko.SSHClient()
>   ssh.connect('127.0.0.1', username='username', password='thispasswd',
> port=22)
> [/code]
>
> and I get
>
> Traceback (most recent call last):
>   File "./client.py", line 76, in 
>     send_ssh(ip)
>   File "./client.py", line 46, in send_ssh
>     ssh.connect('127.0.0.1', username='username',
> password='thispasswd', port=22)
>   File "/var/lib/python-support/python2.5/paramiko/client.py", line
> 299, in connect
>     self._policy.missing_host_key(self, hostname, server_key)
>   File "/var/lib/python-support/python2.5/paramiko/client.py", line
> 83, in missing_host_key
>     raise SSHException('Unknown server %s' % hostname)
> paramiko.SSHException: Unknown server 127.0.0.1
>
> The screen just "hangs", looks like it is waiting/timing-out and then
> if I use ctrl-C, it ouputs
>
> Exception exceptions.KeyboardInterrupt in  usr/lib/python2.5/threading.pyc'> ignored
>
> In case I specify a different port, like 5. I got a socket.error:
> (111, 'Connection refused') . Is there a log file where this is
> recorded. I looked up /var/log/auth.log and syslog, messages but could
> not find a reference to this erroneous login attempt.
>
> [code]
> def send_ssh(ip):
>   ssh = paramiko.SSHClient()
>   ssh.connect('127.0.0.1', username='testusername',
> password='thisshouldbethepasswd', port=5)
> [/code]

Update: I tried restarting ssh from /etc/init.d/ but no luck
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to log connection refused attempt with python-paramiko

2009-09-09 Thread nickname
On Sep 9, 3:37 pm, nickname  wrote:
> On Sep 9, 3:35 pm, nickname  wrote:
>
>
>
> > hello all,
> >             I am getting to grips with paramiko. I am trying to open a
> > ssh connection to my server 127.0.0.1 using paramiko and want to
> > specify the password via the program itself. I want to generate  and
> > change passwds via the program very frequently and hence am avoiding a
> > key based approach.
>
> > I am using
>
> > [code]
> > def send_ssh(ip):
> >   ssh = paramiko.SSHClient()
> >   ssh.connect('127.0.0.1', username='username', password='thispasswd',
> > port=22)
> > [/code]
>
> > and I get
>
> > Traceback (most recent call last):
> >   File "./client.py", line 76, in 
> >     send_ssh(ip)
> >   File "./client.py", line 46, in send_ssh
> >     ssh.connect('127.0.0.1', username='username',
> > password='thispasswd', port=22)
> >   File "/var/lib/python-support/python2.5/paramiko/client.py", line
> > 299, in connect
> >     self._policy.missing_host_key(self, hostname, server_key)
> >   File "/var/lib/python-support/python2.5/paramiko/client.py", line
> > 83, in missing_host_key
> >     raise SSHException('Unknown server %s' % hostname)
> > paramiko.SSHException: Unknown server 127.0.0.1
>
> > The screen just "hangs", looks like it is waiting/timing-out and then
> > if I use ctrl-C, it ouputs
>
> > Exception exceptions.KeyboardInterrupt in  > usr/lib/python2.5/threading.pyc'> ignored
>
> > In case I specify a different port, like 5. I got a socket.error:
> > (111, 'Connection refused') . Is there a log file where this is
> > recorded. I looked up /var/log/auth.log and syslog, messages but could
> > not find a reference to this erroneous login attempt.
>
> > [code]
> > def send_ssh(ip):
> >   ssh = paramiko.SSHClient()
> >   ssh.connect('127.0.0.1', username='testusername',
> > password='thisshouldbethepasswd', port=5)
> > [/code]
>
> Update: I tried restarting ssh from /etc/init.d/ but no luck

Solved the problem using info from 
http://ubuntuforums.org/showthread.php?t=743119
-- 
http://mail.python.org/mailman/listinfo/python-list