how to direct scp output to a file in bash shell or script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to direct scp output to a file in bash shell or script
# 1  
Old 02-04-2009
how to direct scp output to a file in bash shell or script

I can run this from the command line:
scp -i identfile /path/file_to_send remotelogin@remotebox:/path_to_put_it/file_to_send

and I get:
file_to_send 100% |***************************************************************************| 0 00:00

but if I do:
scp -i identfile /path/file_to_send remotelogin@remotebox:/path_to_put_it/file_to_send > logfile

I get nothing for output at the command line (which I would expect since I redirected it) and a file called logfile is created but it is empty.

I tried:
scp -i identfile /path/file_to_send remotelogin@remotebox:/path_to_put_it/file_to_send > logfile 2>&1

but still nothing...

Can someone please tell me what I am doing wrong?
# 2  
Old 02-04-2009
you're doing nothing wrong.
scp detects when it's writing to a tty or a pipe or file or whatnot
and suppresses that *********** line if writing to a pipe or file.

Maybe just checking the $? status?
# 3  
Old 02-04-2009
maybe someone knows how to fool scp but i don't...
i tried various things like:

echo | scp . . ..

( scp . .. . ) > log 2>&1

( thinking it was similar to "tty" ) but nothing worked.

Oh.... this worked, but I couldn't get it to work in a script:

script
scp . . .
exit
# 4  
Old 02-04-2009
I can't figure anything out either... This is really bumming me out, seems like it should be easy.
# 5  
Old 02-04-2009
Try running script beforehand and close it after scp'ing the file.
It won't suppress displaying it on the screen, but it will capture everything that happens during the script session.

Last edited by avronius; 02-04-2009 at 05:41 PM.. Reason: moved emphasis to the correct word...
# 6  
Old 02-04-2009
Quote:
Originally Posted by avronius
Try running script beforehand and close it after scp'ing the file.
It won't suppress displaying it on the screen, but it will capture everything that happens during the script session.
can I use the script command in a bash script?
# 7  
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" 

}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question

Featured Tech Videos