[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Why chdir command doesn't work with client.get_transport() ?

charles

2/2/2008 11:42:00 PM


I am new to paramiko. I wrote a script to copy files between Solaris and XP
machines as below:

import paramiko
def exec_command(trans, command):
chan = trans.open_session()
chan.exec_command(command)
stdin = chan.makefile('wb')
stdout = chan.makefile('rb')
stderr = chan.makefile_stderr('rb')
return stdin, stdout, stderr
def main():
client = paramiko.SSHClient()
client.connect(hostname, username=username, password=password)
trans = client.get_transport()
dummy_chan = trans.open_session()
exec_command(trans, 'cd temp')
stdin, stdout, stderr = exec_command(pwd)

>From the last line I found from stdout that the pwd is not changed. I tried
to type the full path in 'cd', did not help. Other UNIX commands such as ls,
mkdir, cp, rm will work. Only this 'cd' never works. I got stuck here.

Please help. Thanks!

Charles
2/2
--
View this message in context: http://www.nabble.com/Why-chdir-command-doesn%27t-work-with-client.get_transport%28%29---tp15248798p152...
Sent from the Python - python-list mailing list archive at Nabble.com.

2 Answers

Martin v. Loewis

2/3/2008 9:17:00 AM

0

> import paramiko
> def exec_command(trans, command):
> chan = trans.open_session()
> chan.exec_command(command)
> stdin = chan.makefile('wb')
> stdout = chan.makefile('rb')
> stderr = chan.makefile_stderr('rb')
> return stdin, stdout, stderr
> def main():
> client = paramiko.SSHClient()
> client.connect(hostname, username=username, password=password)
> trans = client.get_transport()
> dummy_chan = trans.open_session()
> exec_command(trans, 'cd temp')
> stdin, stdout, stderr = exec_command(pwd)
>
>>From the last line I found from stdout that the pwd is not changed. I tried
> to type the full path in 'cd', did not help. Other UNIX commands such as ls,
> mkdir, cp, rm will work. Only this 'cd' never works. I got stuck here.
>
> Please help. Thanks!

It works. However, each exec_command opens a new session. Within the
first session, you do the cd command, and in the second session, the
process in whose context the cd was executed has already terminated.
You get a new process, and that starts out with the default directory
again.

You need to open a remote shell, and send commands to its command
line; AFAICT, you can't use exec_command for that.

Regards,
Martin

charles

2/3/2008 5:59:00 PM

0


Thank you, Martin! I think that you are right.
But I can't use rsh since I am on XP to send commands to UNIX. I used telnet
before. Now I am converting to ssh/sftp, which is my purpose.

I put some more efforts in the following code:

t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password, hostkey=None)
sftp = t.open_sftp_client()
sftp.chdir('temp')
pwd = sftp.getcwd() # Successful
s = t.open_session()
s.exec_command('ls')
stdout = s.makefile('rb')
for line in stdout:
print '... ' + line.strip('\n') # Find: the default
directory shown

This means the channel s created above has nothing to do with the sftp
client which successfully changed the pwd. Any idea that we can link s and
sftp together? Thanks!

I can't find something equivalent to Telnet in paramiko.

Charles
2/3/2008



--
View this message in context: http://www.nabble.com/Why-chdir-command-doesn%27t-work-with-client.get_transport%28%29---tp15248798p152...
Sent from the Python - python-list mailing list archive at Nabble.com.