Re: scp option (137 Views)
Reply
Regular Advisor
Dewa Negara_4
Posts: 194
Registered: ‎04-14-2003
Message 1 of 8 (137 Views)
Accepted Solution

scp option

Hi All,

I have many linux servers. we have a script running on linux client using the following command:

scp /tmp/stat filemgr@server:/var/tmp/stat

In order to run the script successfully, we need to copy root public key from all the clients to /home/filemgr/.ssh/authorized_key file. The issue comes when we forgot to copy the public key from one of the client. This is causing filemgr accout is disabled on the server and effect the script on all other client won't run well.

What option I have to add on scp command in order to check if there is no public key exist on the server for that particular client, the script won't run and will give an error?

Thanks in advance.
Best Regards,
Dewa
Santos
Please use plain text.
Honored Contributor
Horia Chirculescu
Posts: 829
Registered: ‎07-11-2009
Message 2 of 8 (137 Views)

Re: scp option

Hello,

I assume that in the case when no public key exists on remote server, scp is asking for a password, right?

If this is the case, then your script should provide that password. For the case when the key exists, you should instruct your script to wait a reasonable amount of time then will pass to the next instruction from the script.

I am saying this having in mind expect.

Best regards,
Horia.
Best regards from Romania,
Horia.
Please use plain text.
Regular Advisor
Dewa Negara_4
Posts: 194
Registered: ‎04-14-2003
Message 3 of 8 (137 Views)

Re: scp option

Thanks. When the public key doesn't exist on the server, for sure the script will ask to key the password. manual intervene is required here. What I want is that..when the public key of the remote server does not exist on that server, the script will exit with error message without asking the password.

What is scp option need to be added here?

Thanks and Regards,
Dewa

Santos
Please use plain text.
Honored Contributor
Horia Chirculescu
Posts: 829
Registered: ‎07-11-2009
Message 4 of 8 (137 Views)

Re: scp option

You can use -B (scp -B) to prevent asking for password.

scp will fail in this case. You must catch the return code in your script.

Horia.

Best regards from Romania,
Horia.
Please use plain text.
Honored Contributor
Horia Chirculescu
Posts: 829
Registered: ‎07-11-2009
Message 5 of 8 (137 Views)

Re: scp option

In order to prevent this (the fail from scp) you could use expect. It seems to me the perfect candidate for your job.

The porting page for expect:

http://hpux.connect.org.uk/hppd/hpux/Tcl/expect-5.43/

(Follow the run-time dependencies, as always!)

Best regards from Romania,
Horia.
Best regards from Romania,
Horia.
Please use plain text.
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 6 of 8 (137 Views)

Re: scp option

scp -B /source/sourcefile user@hostname:/destinationdir/destination.file

if you have established the trust this will work, if the trust is not established it won't..

you will need to complete the key exchange.
Please use plain text.
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 7 of 8 (137 Views)

Re: scp option

if you do the "expect" thing, it is a bit more complex, you can embed the password in the expect script.

See below:

You'd need to modifyu parameters to fit your needs..


#!/usr/bin/expect -f

# This script needs three argument to(s) connect to remote server:
# password = Password of remote UNIX server, for root user.
# ipaddr = IP Addreess of remote UNIX server, no hostname
# set Variables

set password "ENTER PASSWORD HERE"
set ipaddr "www.xxx.yyy.zzz"
set arg1 [lrange $argv 3 3]
set timeout -1
# now connect to remote UNIX box (ipaddr)
spawn sftp USERNAME @$ipaddr $arg1
match_max 100000

expect {
-re ".*Are.*.*yes.*no.*" {
send "yes\r"
exp_continue
#look for the password prompt
}

"password:" {
send -- "$password\r"
}
}
expect "sftp>"
send "lcd /localdir/localfile/\r"
expect "sftp>"
send "cd /incoming/\r"
expect "sftp>"
send "put localfile\r"
Please use plain text.
Regular Advisor
Dewa Negara_4
Posts: 194
Registered: ‎04-14-2003
Message 8 of 8 (137 Views)

Re: scp option

Hi All,

Thanks for your help. I prefer to use -B option for this case as what you advice. Thanks again for all of you.


Best Regards,
Negara
Santos
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation