1748157 Members
4057 Online
108758 Solutions
New Discussion юеВ

Re: scp option

 
SOLVED
Go to solution
Dewa Negara_4
Regular Advisor

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
7 REPLIES 7
Horia Chirculescu
Honored Contributor

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.
Dewa Negara_4
Regular Advisor

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
Horia Chirculescu
Honored Contributor
Solution

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.
Horia Chirculescu
Honored Contributor

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.
rmueller58
Valued Contributor

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.
rmueller58
Valued Contributor

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"
Dewa Negara_4
Regular Advisor

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