ssh and redirection


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ssh and redirection
# 15  
Old 10-07-2011
Quote:
Originally Posted by Corona688
Putting that in quotes instead of quote tags means when I try and quote you, the error message disappears. Use code tags for code.

"no askpass program" means it needs to ask you for a password and can't because it's not in a terminal. Run it in a terminal.

---------- Post updated at 09:12 AM ---------- Previous update was at 09:11 AM ----------

This won't work, again, because the redirection happens before the sudo. The shell doesn't have permissions to overwrite the file, and will fail to redirect into it.

---------- Post updated at 09:14 AM ---------- Previous update was at 09:12 AM ----------

Perhaps:

Code:
ssh -t newuser@server echo "': > /var/spool/mail/user1'" '|' sudo -u user1 /bin/sh

you are right but if newuser hasnot a entry in sudoers then your example also is not solution to me.
in this case sudo is not required and sudo does not get extra rights on files.
maybe `su` is can be usable instead of sudo.
Code:
ssh -tt newuser@server "su -l user1 -c 'echo -n "">/var/spool/mail/user1'"

# 16  
Old 10-07-2011
Thanks for all , it works :

Code:
ssh -t newuser@server echo "': > /var/spool/mail/user1'" '|' sudo -u user1 /bin/sh

regards
Christian
# 17  
Old 10-07-2011
Quote:
Originally Posted by Corona688
That may delete and recreate the file and leave the old file still open hogging disk, unfixable without rebooting the daemon or server! Don't do it!
I had a poke around, but apparently my GoogleFu is weak today. Do you have a link that explains the issues surrounding this?

Also, would
Code:
cat /dev/null > file

have the same potential problem (sudo/redirect issues aside)?

(apologies for drifting off-topic)
# 18  
Old 10-07-2011
Quote:
Originally Posted by CarloM
I had a poke around, but apparently my GoogleFu is weak today. Do you have a link that explains the issues surrounding this?
I can do better than that, I can give you a working example:

Code:
## Demo of deleting a logfile in use
$ echo "the owls are not what they seem" > filename # Create a file
$ exec 5<filename # Open filename for reading, into FD 5
$ rm filename # delete filename.
$ ls -l filename # The directory entry will be gone -
ls: cannot access filename: No such file or directory
cat <&5 # ...but the file still exists, since we have it open.
the owls are not what they seem
$ exec 5<&- # Only on close will it TRULY be deleted from disk.

## Demo of truncating a file in use
echo "the owls are not what they seem" > filename # Create a file
$ exec 5<filename # Open filename for reading, into FD 5
$ : > filename # truncate filename
$ ls -l filename # The same file exists, with zero size
-rw-r--r-- 1 username users 0 Oct  7 11:40 filename
$ cat <&5 # Read from the file.  Nothing there.
$ exec 5<&- # close the file.

cp might overwrite a file, or might delete and recreate it, I wouldn't depend on either -- implementations of things can vary. But a shell redirect > always truncates.

Code:
Also, would 
Code:
cat /dev/null > file
have the same potential problem (sudo/redirect issues aside)?

Nope. cat /dev/null is effectively the same statement as : -- a statement that prints no output. It's the > that's important.

I used to use echo > filename to truncate things until I realized echo does indeed print slightly more than nothing there -- it prints one blank line.
Quote:
(apologies for drifting off-topic)
This is entirely on-topic.

Last edited by Corona688; 10-07-2011 at 02:48 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Ssh script to validate ssh connection to multiple serves with status

Hi, I want to validate ssh connection one after one for multiple servers..... password less keys already setup but now i want to validate if ssh is working fine or not... I have .sh script like below and i have servers.txt contains all the list of servers #/bin/bash for host in $(cat... (3 Replies)
Discussion started by: sreeram4
3 Replies

2. Shell Programming and Scripting

SSH file redirection is not maintaining format

Hi, I'm running a script which would ssh to various ssh-trust enabled servers and get a list of packages installed. The output of this command would be redirected to a file ssh -q $i 'rpm -qa --queryformat '%{NAME}\t\t,%{ARCH}\t\t,%{VERSION}-%{RELEASE}\t\t,%{INSTALLTIME:date}\n'|sed... (2 Replies)
Discussion started by: maverick_here
2 Replies

3. UNIX for Dummies Questions & Answers

about different redirection

explain the redirections 1>, 2>, 3>, ..... and 1< ,2<,3<..... where we use these things thanks Thread moved from AIX forum (2 Replies)
Discussion started by: tsurendra
2 Replies

4. Shell Programming and Scripting

Redirection

Hello All, I am using the below script to gather various tools running by the user, we have more than 100 tools running on the server so my challenge is to redirect memory & cpu load to the file with the name of the tool.so am using the below script i am stucking how to redirect to the file... (2 Replies)
Discussion started by: ajaincv
2 Replies

5. Shell Programming and Scripting

I/O redirection

Hello everyone,I'm reading a book and there's code fragment: exec 3>&1 ls -l 2>&1 >&3 3>&- | grep bad 3>&- exec 3>&- It says that the red part of that code does not close fd 3 but the green does close the fd 3.I can't understand that.....Why?Any predicate will be appreciated.:) (18 Replies)
Discussion started by: homeboy
18 Replies

6. Shell Programming and Scripting

Double redirection

Hi to all. It's possible with a single line redirect to stdout and to a file a echoed string? I need something like this: echo "Pizza" >/tmp/file (and same time print to stout "Pizza")... What can i do? (2 Replies)
Discussion started by: mendez
2 Replies

7. UNIX for Dummies Questions & Answers

Help with Redirection

Hi Guys, I m new to UNIX and new to this forum. Was wondering if someone can help me understand redirection (standard input output pipeline etc) for starters, not too sure what this would mean who | sort > sortedfile | pr | lp im starting to understand common commands but when throwing... (2 Replies)
Discussion started by: jmack123
2 Replies

8. Shell Programming and Scripting

redirection

Hi, The code below works, it's a part of a bash shell script that serve to search a pattern $pattern_da_cercare in the files contained in a directory $directory_iniziale. Now the proble is: How can I redirect stderr to a file? PS: so I want to redirect ALL the errors to a file. I tryed... (9 Replies)
Discussion started by: DNAx86
9 Replies

9. Programming

Help with redirection

Here is my problem. I don't know make this redirection thing work. The output file (called output.c) looks like this #include<stdio.h> int main() { int k; int m; print f("%d\n", k); printf("%d\n", m); return 0; } the input file(called input.c) is this #include<stdio.h> int... (2 Replies)
Discussion started by: Shallon1
2 Replies
Login or Register to Ask a Question