Hello,

I want to copy a jpeg file from linux to my windows machine using scp command.The program will be asking for a password later in the run after it executes the scp command.
Is there a way that I can send the password to the program without a keyborad interaction like storing the password in a string within the program and send the password to the command prompt?

Thanks,
Sravanthi

Recommended Answers

All 11 Replies

Thank you.But, I am running these commands in a python script which uses Python2.6.when I run this script in the command prompt,it is asking for a password but I want the script to take the password as a subprocess and complete the run.

Well, here is the code I use to transfer files with paramiko, ssh and sftp

import paramiko

COMP = "computername"

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(COMP, username="myusername", password="mypassword", allow_agent = False)

src = "/cygdrive/c/Documents*Settings/myusername/myfile.txt"
dst = "/home/myusername/myfile.txt"

ftp = ssh.open_sftp()
ftp.get(src , dst)
ftp.close()

To upload a file to the host, call ftp.get(dst, src)

commented: Post as code snippet also, pal! +2

Is there any option to do this using scp and subprocess functions?

Is there any option to do this using scp and subprocess functions?

You could use the module pexpect to simulate human interaction with the scp command in a shell.
Here is an example (with ftp), taken from pexpect's documentation

# This connects to the openbsd ftp site and
   # downloads the recursive directory listing.
   import pexpect
   child = pexpect.spawn ('ftp ftp.openbsd.org')
   child.expect ('Name .*: ')
   child.sendline ('anonymous')
   child.expect ('Password:')
   child.sendline ('noah@example.com')
   child.expect ('ftp> ')
   child.sendline ('cd pub')
   child.expect('ftp> ')
   child.sendline ('get ls-lR.gz')
   child.expect('ftp> ')
   child.sendline ('bye')

url: http://www.noah.org/wiki/pexpect

but pexpect just works until python2.5 but i am using 2.6

but pexpect just works until python2.5 but i am using 2.6

Here is what my console says

Python 2.6.5 (r265:79063, Jul 14 2010, 13:38:11) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>>

so pexpect works for python 2.6.5. :)
There is a package python-pexpect on my Mandriva 2010. Check packages for your os.

Mine is as below: I am running the script on windows and copying the files from linux to windows machine

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************

Here is what my console says

Python 2.6.5 (r265:79063, Jul 14 2010, 13:38:11) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>>

so pexpect works for python 2.6.5. :)
There is a package python-pexpect on my Mandriva 2010. Check packages for your os.

Requires POSIX system though:

C:\Documents and Settings\Veijalaiset>easy_install pexpect
Creating j:\python26\lib\site-packages\site.py
Searching for pexpect
Reading http://pypi.python.org/simple/pexpect/
Reading http://pexpect.sourceforge.net/
Reading http://sourceforge.net/project/showfiles.php?group_id=59762
Best match: pexpect 2.4
Downloading http://pypi.python.org/packages/source/p/pexpect/pexpect-2.4.tar.gz#
md5=fe82d69be19ec96d3a6650af947d5665
Processing pexpect-2.4.tar.gz
Running pexpect-2.4\setup.py -q bdist_egg --dist-dir c:\docume~1\veijal~1\locals
~1\temp\easy_install-ts3q8d\pexpect-2.4\egg-dist-tmp-aexfev
zip_safe flag not set; analyzing archive contents...
Adding pexpect 2.4 to easy-install.pth file

Installed j:\python26\lib\site-packages\pexpect-2.4-py2.6.egg
Processing dependencies for pexpect
Finished processing dependencies for pexpect

C:\Documents and Settings\Veijalaiset>j:\Python26\python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build\bdist.win32\egg\pexpect.py", line 86, in <module>
ImportError: No module named resource

A critical module was not found. Probably this operating system does not
support it. Pexpect is intended for UNIX-like operating systems.
>>>

Requires POSIX system though:

C:\Documents and Settings\Veijalaiset>easy_install pexpect
Creating j:\python26\lib\site-packages\site.py
Searching for pexpect
Reading http://pypi.python.org/simple/pexpect/
Reading http://pexpect.sourceforge.net/
Reading http://sourceforge.net/project/showfiles.php?group_id=59762
Best match: pexpect 2.4
Downloading http://pypi.python.org/packages/source/p/pexpect/pexpect-2.4.tar.gz#
md5=fe82d69be19ec96d3a6650af947d5665
Processing pexpect-2.4.tar.gz
Running pexpect-2.4\setup.py -q bdist_egg --dist-dir c:\docume~1\veijal~1\locals
~1\temp\easy_install-ts3q8d\pexpect-2.4\egg-dist-tmp-aexfev
zip_safe flag not set; analyzing archive contents...
Adding pexpect 2.4 to easy-install.pth file

Installed j:\python26\lib\site-packages\pexpect-2.4-py2.6.egg
Processing dependencies for pexpect
Finished processing dependencies for pexpect

C:\Documents and Settings\Veijalaiset>j:\Python26\python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build\bdist.win32\egg\pexpect.py", line 86, in <module>
ImportError: No module named resource

A critical module was not found. Probably this operating system does not
support it. Pexpect is intended for UNIX-like operating systems.
>>>

I didn't know that, perhaps the OP could run pexpect from his linux machine ...

Or OP could open one ftp directory in Linux machine or have only fixed name file in certain place open for urllib2.urlopen if that is enough secure. The file could be encrypted zip to improve security.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.