tags:

views:

111

answers:

4

Can anyone tell me what's wrong in this code:

#!/usr/local/bin/python

import os
import string, sys

a='sys.argv[1]'
b='sys.argv[2]'
os.system("scp a:/export/home/sample/backup.sql  b:/home/rushi/abc.sql")

it's giving the following error:

ssh: a: node name or service name not known
+1  A: 

the very first character of that first line should be #.

Will
yeah i hv written that.
rushi
+5  A: 

What is wrong:

  1. a and b don't have second and third values of sys.argv as you might've intended
  2. a and b are not related to the os.system call
  3. you're using os.system
  4. you're importing module that you're not using

How to fix:

  1. use a = sys.argv[1] without the quotes, same for b.
  2. use .format method or similar % to format a string
  3. have a look at subprocess module
  4. don't import string
SilentGhost
A: 

You have bound the names "a" and "b" to the arguments passed on the command line (actually, you would have done that if you had removed the quotes after the equal sign,).

But in the command that you are executing, you are NOT using them, you are using the literal "a" and "b" drives. This is a dangerous approach because you risk injections. In any case, if you trust the source of the inputs, you can escape the values for a and b, something like

os.system("scp %s:/export/home/sample/backup.sql  %s:/home/rushi/abc.sql" % (a, b))

This is not the recommended way to do it.

Francesco
A: 

Your code should be changed to this

a=sys.argv[1]
b=sys.argv[2]

Since right now you are treating sys.argv[1] as strings by adding quotes

also you want to do this:

os.system("scp %s:/export/home/sample/backup.sql  %s:/home/rushi/abc.sql" % (a,b) )

so that you add the variables to the string you are trying to call with os.system

JiminyCricket
its now giving following error:Permission denied (publickey,keyboard-interactive).lost connection
rushi
You need to include a username and password. I assume 'a' is on your local machine and 'b' is remote. the remote address should look like [email protected]:/home/rushi/abc.sql
JiminyCricket
If you need to use authentication you are going to have to add an identity file containing your private key. Enter 'man scp' to get the docs for that command or here: http://www.computerhope.com/unix/scp.htm
JiminyCricket