how to redirect output of echo over ssh to a file
In my case the problem solved with this command:
ssh $MachineIP " echo \"$CM_Config\" > \"$mName/CM_CONFIG\" "
In fact without \" enclosing my variable, my problem didn't solved. Maybe it is because that the content of these variables are somehow like bash command and are in multiple lines.
Piping the output of ssh sudo
The best solution I've come up with is to use sudo -S
and disable local echo so the password isn't shown as you type it:
$ { stty -echo; ssh remote sudo -S echo hello; stty echo; echo 1>&2; }
[sudo] password for user:
hello
This leaves sudo in charge of the password prompting, so it works properly if the user types the password wrong.
I don't think any solution using ssh -t
can ever work properly, since it combines stderr and stdout.
When redirecting in bash to stdout or stderr, I actually get redirected to a file
@JonathanLeffler caught the problem right away -- you're not using bash, but tcsh.
tcsh has its own syntax incompatible with bash.
It doesn't matter though: since ssh
and sshpass
return the remote command's exit code, this is how you should be doing it:
if sshpass -p password ssh user@ip 'echo "Message1"; su -lc "./rootscript.sh"'
then
echo "The command succeeded (exit code $?)"
else
echo "The command failed (exit code $?)"
fi
If you really wanted to run something in bash on the remote shell, you could do use Charles Duffy's code with a minor change:
# v-- shell specified here
output=$(sshpass -p password ssh user@ip bash << 'EOF'
echo "Message1" >&2
su -lc "./rootscript.sh" >&2
echo "$?"
EOF
)
Capture output of bash script over ssh within script
Since you pass in literal single quotes, the command executed becomes 'ls -lt'
which gives the same error locally too (as opposed to ls -lt
without quotes).
The easiest solution is just removing the quotes:
op="ls -lt"
cmd="ssh remServer $op"
res=`$cmd`
echo $res
The better solution is using proper arrays and escaping:
op=(ls -lt)
cmd=(ssh remServer "$(printf '%q ' "${op[@]}")" )
res=$("${cmd[@]}")
echo "$res"
Redirect all output to file in Bash
That part is written to stderr, use 2>
to redirect it. For example:
foo > stdout.txt 2> stderr.txt
or if you want in same file:
foo > allout.txt 2>&1
Note: this works in (ba)sh, check your shell for proper syntax
Writing outputs to log file and console
exec 3>&1 1>>${LOG_FILE} 2>&1
would send stdout and stderr output into the log file, but would also leave you with fd 3 connected to the console, so you can do
echo "Some console message" 1>&3
to write a message just to the console, or
echo "Some console and log file message" | tee /dev/fd/3
to write a message to both the console and the log file - tee
sends its output to both its own fd 1 (which here is the LOG_FILE
) and the file you told it to write to (which here is fd 3, i.e. the console).
Example:
exec 3>&1 1>>${LOG_FILE} 2>&1
echo "This is stdout"
echo "This is stderr" 1>&2
echo "This is the console (fd 3)" 1>&3
echo "This is both the log and the console" | tee /dev/fd/3
would print
This is the console (fd 3)
This is both the log and the console
on the console and put
This is stdout
This is stderr
This is both the log and the console
into the log file.
Related Topics
Limit Useable Host Resources in Docker Compose Without Swarm
How to Produce Stand Alone Haskell Executable
Linux Get Process Name from Pid Within Kernel
How to Install the Specific Version of Postgres
Cron Error with Using Backquotes
How to Install a Node.Js Server at Chat.Mydomain.Com on a Hostgator Vps Hosting
Fatal: Git Was Built Without Support for Git-Add--Interactive (No_Perl=1)
How to Export a Modified Kernel Header
Calculate Mean of Each Column Ignoring Missing Data with Awk
Getting Github Files (And Updates) Onto an Ubuntu Web Server
Passing Environment Variables Not Working with Docker
Makefile for Linux Kernel Module
Gdb Warning: Loadable Section Not Found in Added Symbol-File System-Supplied Dso at 0X7Ffff7Ffd000
Linux Kernel API Changes/Additions
Linux Iterate Over Files in Directory