Sponsored Content
Top Forums Shell Programming and Scripting how to direct scp output to a file in bash shell or script Post 302284091 by curleb on Wednesday 4th of February 2009 06:00:07 PM
Old 02-04-2009
The following is a function that I use in a variety of my scripts to enable an SCP session within the process. It pulls its params in from a config file that is parsed elsewhere to provide to this function's call. You can swap in your own vars or even static values to get a sense of what it's doing.

SCP (or SSH, for that matter...) is not overly verbose, unless you specifically tell it to be. The terminal output you see when in the shell is not captured, most likely since it speaks to security anyway. Best bet might be to track the return code(s) and debug as needed in the case of problems. You can, however, modify the verbosity some by incrementing the verbosity switch/flag on your particular scp client. This is seen in the ${verbosity} parameter shown in the function's actual scp call:

Code:
   ###########################
   #  function SCP_mthd ... 
   #     - process SCP transfer 
function SCP_mthd 
{ 

   $_dbg_mode

   print "\n\tGreetings from SCP_mthd ($@) "                 >>${MY_LOG} 2>&1 

   #  Scrap it if there aren't sufficient params... 
   if [[ -z ${param_num1} ]] \
         || [[ -z ${param_num2} ]] \
         || [[ -z ${param_num3} ]] \
         || [[ -z ${param_num4} ]] \
         || [[ -z ${param_num5} ]] \
         || [[ -z ${param_num6} ]] 
   then 
      print "\n ===\n\n ${my_name}: $(date) "                |tee -a ${MY_LOG} 
      print "\n   ${0} called w/ invalid argument(s)... "    |tee -a ${MY_LOG} 
      print "\n                                         "    |tee -a ${MY_LOG} 
      print "\n\tCalled as: $0 ${@} \n" \
         "\tParameters: ${param_num1} \n " \
         "\t            [ -------- ]        \n "  \
         "\t            ${param_num3:-"#"}  \n "  \
         "\t            ${param_num4:-"#"}  \n "  \
         "\t            ${param_num5:-"#"}  \n "  \
         "\t            ${param_num6:-"#"}  \n "  \
         |grep -v "#"                                        |tee -a ${MY_LOG} 
      error_flag='Y' ; logger_heads ;return 2 
   fi 

   #  Hereby reserving the param_num2 column for use as an alternate IdentityFile \
   #     in SSH-based calls (ie, -i "/..." for ssh/scp, -o "/..." for sftp, etc) 
   #     -  placing "not used" string within column in .cfg file will bypass \
   #           usage of alternate file 
   if [[ ! -z ${param_num2} ]] \
      && [[ ${param_num2} != "not_used" ]] 
   then 
      typeset  host_pass=$(print ${param_num2} |tr "_" " " |tr "+" "-" ) 
   fi 
   typeset -l  host_name="${param_num3}" 
   typeset -l  host_user="${param_num4}" 
   typeset     host_dest="${param_num5}" 
   typeset     locl_path="${my_file%/*[pf][rt][nm]}" 
   typeset     locl_file="${my_file#${locl_path}/*}" 

#  print       "host_name=${host_name}" 
#  print       "host_user=${host_user}" 
#  print       "host_pass=${host_pass}" 
#  print       "host_dest=${host_dest}" 
#  print       "locl_path=${locl_path}" 
#  print       "locl_file=${locl_file}" 

   scp ${verbosity} ${host_pass} ${my_file} ${host_user}\@${host_name}\:${host_dest}\/\. \
                                                             >>${MY_LOG} 2>&1 

   rc=${?} 

   if [[ ${rc} != 0 ]] 
   then 
      print "\n ===\n\n ${my_name}: $(date) "                |tee -a ${MY_LOG} 
      print "\n   ${my_mthd} had an error... "               |tee -a ${MY_LOG} 
      print "\n                                         "    |tee -a ${MY_LOG} 
      print "\n\tCalled as: $0 ${@} \n" \
         "\tReturn Code: ${rc} \n " \                        |tee -a ${MY_LOG} 
      error_flag='Y' ; logger_heads ;return 2 
   fi 

   echo "nicely done" 

}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

scp output fm script won't go to file

Have a script that scp's tar file to multiple other servers in a for loop. Need to set monitoring and notification on it for when it fails. Running this line of code in a 'for' loop... scp $SOURCE_RECOVERY_TARFILE ${HOST}:${CURR_RECOV_TARFILE} 2>&1 | tee ${MONFILE} Their are two outputs... (7 Replies)
Discussion started by: nmikes
7 Replies

2. UNIX for Dummies Questions & Answers

direct output to a file then email it

Ok so i have this script and I dont know how to have the output go to a file and then email that file to someone. #!/bin/ksh print "AL" print "AM" print "AN" print "RL\n" nawk '/PROD/ {print $3, $2}' /home/user/switch_listtest | sort -k1,2 print "End of Report" Thank you in... (2 Replies)
Discussion started by: llsmr777
2 Replies

3. Shell Programming and Scripting

Direct the output of a script to a log file

Hi, I have a script to compare 2 files. file1=$1 file2=$2 num_of_records_file1=`awk ' END { print NR } ' $file1` num_of_records_file2=`awk ' END { print NR } ' $file2` i=1 while do sed -n "$i"p $file1 > file1_temp sed -n "$i"p $file2 > file2_temp diff file1_temp... (5 Replies)
Discussion started by: autosys_nm
5 Replies

4. UNIX for Dummies Questions & Answers

How do i tell my bash shell script to test the output of a command against something

How do i tell my bash shell script to test the output of the command i'm using?? I want this script to look for lines not equal to 1 then let me know.. $ cat blah ; echo ---- ; cat blah.sh 1 fe 1 fi 1 fo 0 fum 1 blahda 1 blah 0 blahh 1 bla 1 bl 1 blahhh ---- #!/bin/bash while... (1 Reply)
Discussion started by: phpfreak
1 Replies

5. Shell Programming and Scripting

Been working since 25+ hrs: Bash Script to rename files supposedly direct but difficult to execute

:wall::wall::wall: Hi I have horrible script below, need help in renaming ls -l output into new filename format: Desired output: cp -pv original_path/.* newDirectory/owner_of_file.%dd%mm%y.file_extension.first_8_characters_of_original_filename localuser@localuser:~ vi... (3 Replies)
Discussion started by: wolf@=NK
3 Replies

6. Shell Programming and Scripting

Addition to Bash shell script that emails final output as attachement?

Greetings. I have a nice bash shell script that runs a multi-step analysis well. I already have the SGE options set up to email me the progress of the run (started, completed, aborted), but a final step would be to code the shell script to email the final output (a .txt file) to the same email... (6 Replies)
Discussion started by: Twinklefingers
6 Replies

7. Shell Programming and Scripting

Manipulating sed Direct Input to Direct Output

Hi guys, been scratching round the forums and my mountain of resources. Maybe I havn't read deep enough My question is not how sed edits a stream and outputs it to a file, rather something like this below: I have a .txt with some text in it :rolleyes: abc:123:xyz 123:abc:987... (7 Replies)
Discussion started by: the0nion
7 Replies

8. UNIX for Dummies Questions & Answers

Output of ssh command from localhost - direct to local file.

Hi, i'm trying to gather details from remote hosts and want them to be written to my local linux machine from where i'm using SSH. My command looks some thing like this ssh -q remotehost 'bash -s' <command.txt where command.txt is a file in my local machine containing ps -ef |grep httpd |... (1 Reply)
Discussion started by: poga
1 Replies

9. Shell Programming and Scripting

Disk Space Script to direct output

Hi, I am working on Sun Solaris 5.10 and want to direct the output from a disk space check script to an output file; #!/bin/bash CURRENT=$(df -k /log/logs | grep /log/logs | awk '{ print $5}' | sed 's/%//g') THRESHOLD=30 if ; then echo "Remaining free space is low" > output.txt else... (10 Replies)
Discussion started by: SSKAAB
10 Replies

10. Shell Programming and Scripting

For loop in bash - Direct output to two files

Hello all, i have a code in which when doing a for loop, i need to direct the output to two files, one just a single output, the other to always append (historical reasons). So far i managed to do the following, which is working, but am still considering it as "dirty". ... (4 Replies)
Discussion started by: nms
4 Replies
SCP(1)							    BSD General Commands Manual 						    SCP(1)

NAME
scp -- secure copy (remote file copy program) SYNOPSIS
scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2 DESCRIPTION
scp copies files between hosts on a network. It uses ssh(1) for data transfer, and uses the same authentication and provides the same secu- rity as ssh(1). Unlike rcp(1), scp will ask for passwords or passphrases if they are needed for authentication. File names may contain a user and host specification to indicate that the file is to be copied to/from that host. Local file names can be made explicit using absolute or relative pathnames to avoid scp treating file names containing ':' as host specifiers. Copies between two remote hosts are also permitted. The options are as follows: -1 Forces scp to use protocol 1. -2 Forces scp to use protocol 2. -4 Forces scp to use IPv4 addresses only. -6 Forces scp to use IPv6 addresses only. -B Selects batch mode (prevents asking for passwords or passphrases). -C Compression enable. Passes the -C flag to ssh(1) to enable compression. -c cipher Selects the cipher to use for encrypting the data transfer. This option is directly passed to ssh(1). -F ssh_config Specifies an alternative per-user configuration file for ssh. This option is directly passed to ssh(1). -i identity_file Selects the file from which the identity (private key) for public key authentication is read. This option is directly passed to ssh(1). -l limit Limits the used bandwidth, specified in Kbit/s. -o ssh_option Can be used to pass options to ssh in the format used in ssh_config(5). This is useful for specifying options for which there is no separate scp command-line flag. For full details of the options listed below, and their possible values, see ssh_config(5). AddressFamily BatchMode BindAddress ChallengeResponseAuthentication CheckHostIP Cipher Ciphers Compression CompressionLevel ConnectionAttempts ConnectTimeout ControlMaster ControlPath GlobalKnownHostsFile GSSAPIAuthentication GSSAPIDelegateCredentials HashKnownHosts Host HostbasedAuthentication HostKeyAlgorithms HostKeyAlias HostName IdentityFile IdentitiesOnly KbdInteractiveDevices LogLevel MACs NoHostAuthenticationForLocalhost NumberOfPasswordPrompts PasswordAuthentication PKCS11Provider Port PreferredAuthentications Protocol ProxyCommand PubkeyAuthentication RekeyLimit RhostsRSAAuthentication RSAAuthentication SendEnv ServerAliveInterval ServerAliveCountMax StrictHostKeyChecking TCPKeepAlive UsePrivilegedPort User UserKnownHostsFile VerifyHostKeyDNS -P port Specifies the port to connect to on the remote host. Note that this option is written with a capital 'P', because -p is already reserved for preserving the times and modes of the file in rcp(1). -p Preserves modification times, access times, and modes from the original file. -q Quiet mode: disables the progress meter as well as warning and diagnostic messages from ssh(1). -r Recursively copy entire directories. Note that scp follows symbolic links encountered in the tree traversal. -S program Name of program to use for the encrypted connection. The program must understand ssh(1) options. -v Verbose mode. Causes scp and ssh(1) to print debugging messages about their progress. This is helpful in debugging connection, authentication, and configuration problems. The scp utility exits 0 on success, and >0 if an error occurs. SEE ALSO
rcp(1), sftp(1), ssh(1), ssh-add(1), ssh-agent(1), ssh-keygen(1), ssh_config(5), sshd(8) HISTORY
scp is based on the rcp(1) program in BSD source code from the Regents of the University of California. AUTHORS
Timo Rinne <tri@iki.fi> Tatu Ylonen <ylo@cs.hut.fi> BSD
February 8, 2010 BSD
All times are GMT -4. The time now is 11:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy