Monday, June 23, 2008

Problems with running from the module

I have a code


# --- sshutils.py
import paramiko
import base64
import os
from paramiko import AutoAddPolicy

import wx

EMPTY_STRING = ''


def main():
""" main function that connects to the remote host and perform some
actions there
"""


uname = 'yourlogin' # showTextDialog('Enter user name: ')
hname = 'hostname.address.org' # showTextDialog('Enter hostname')
passwd= showPasswordDialog()


sshClient = getConnection(hname, uname, passwd)

# executing remote commands
(stdin, stdout, stderr) = execCommand(sshClient, 'pwd')
printStream(stdout)

(stdin, stdout, stderr) = execCommand(sshClient, 'ls')
printStream(stdout)


# open the ftp shell
sftpClient = sshClient.open_sftp()
L_PATH= '/tmp/'
F_NAME = 'phello.txt'
R_PATH= '/home/magg/tmp/'
L_F = L_PATH + F_NAME
R_F = R_PATH + F_NAME
print L_F, R_F

# create file
createFile(L_F)
print 'Zawartosc pliku: ' + open(L_F, 'r').read()

# send file from local to remote
sftpClient.put(L_F, R_F)
# get info about the remote file
rf_attr = sftpClient.stat(R_F)
print rf_attr

if rf_attr.st_mode != 0755:
print 'File is not executable'
print 'Changing attributes to exec'
sftpClient.chmod(R_F, 0755)
print sftpClient.stat(R_F)


# send file from remote to local
sftpClient.open(R_F, 'w').write('Zmieniamy plik')
sftpClient.get(R_F, L_F)

print 'Localna zawartosc pliku: ' + open(L_F, 'r').read()


# remove files from remote and local sides
sftpClient.remove(R_F)
os.remove(L_F)


# close the ssh sessions
sftpClient.close()
sshClient.close()

raw_input('Press Enter to exit')

return True

def showPasswordDialog():
app = wx.PySimpleApp()
dlgPasswd = wx.PasswordEntryDialog(None, 'Your password ', 'Enter password')
passwd = EMPTY_STRING

if dlgPasswd.ShowModal() == wx.ID_OK:
passwd = dlgPasswd.GetValue()

dlgPasswd.Destroy()

return passwd

def showTextDialog(text):
app = wx.PySimpleApp()
value = EMPTY_STRING
dlg = wx.TextEntryDialog(None, text, 'enter sth')

if dlg.ShowModal() == wx.ID_OK:
value = dlg.GetValue()

dlg.Destroy()

return value

def getConnection(hname, uname, passwd):
""" connects to the remote system
@param hname - hostname
@param uname - user name
@param password - the password

@return the ssh client
"""

client = paramiko.SSHClient();
# if below policy will be not set by default you will be kick out
# with the exception that the host is unknown
client.set_missing_host_key_policy(AutoAddPolicy())
# connect to the host

client.connect(hname, username = uname, password = passwd)

return client

def execCommand(sshClient, cmd):
""" @return (stdin, stdout, stderr)
"""

return sshClient.exec_command(cmd)



def printStream(stream):
""" prints a stream
@param a stream to be printed out """
for line in stream:
print '... ' + line.strip('\n')


def createFile(fileName):
f = open(fileName, 'w')
f.write('Hello\n')
f.write('Python world\n')
f.close()

return f


main()

# --- end sshutils.py

When I run this file from the command line not from the same directory where this file is located it works and the program executes properly. So the module is in:

c:\tmp\ssh-proj\sshutils.py

then I type

c:\tmp\ssh-proj\> python sshutils.py

This is ok. But when I move the file to

c:\Documents and settings\magg\proj\eclipse\py-wksp\unibus\src\unibus\mediators\stdMediators

Then it doesn't work and just hangs on.

In this file I use paramiko and I suspect that there are problems with loading paramiko since it hangs on paramiko getConnection().

1 comment:

Magda said...

First read documentation:
http://docs.python.org/tut/node8.html

Next:

# if you want to run this from the Python interactive console
# src/> python
# >>> from unibus.mediators.stdMediators.SshDirectMediator import SshDirectMediatorUsage
# >>> med = SshDirectMediatorUsage()
# >>> med.main()
# 1
# 2
# 3
# 4
# 5
# if you want to run this from the command line:
# src/> set PYTHONPATH=C:\\Documents and Settings\\magg\\.......\\src
# DON'T USE " " e.g. PYTHONPATH="C:\\Documents and Settings ...."
# src/> python unibus\mediators\stdMediators\SshDirectMediator.py
# That's all