Sudo and backticks? - linux

When running the following command:
sudo sh -c "sed s/sda1-uuid/`blkid -s UUID -o value /dev/sda1`/ -i /etc/crypttab"
the result is wrong, because blkid -s UUID -o value /dev/sda1 is not executed with root privileges.
What am I doing wrong?

By trying the suggestions in the comments, the following command is now working as expected:
sudo sh -c 'sed s/sda1-uuid/$(blkid -s UUID -o value /dev/sda1)/ -i /etc/crypttab'

Related

Nested bash command quote issues

I have an app that sends commands to bash like so:
/bin/bash -c "<command goes here>"
This works great but I have hit a problem with a slightly more complicated command. This command grabs a tar from an SSH server, shows a progress bar with pv and then saves it to a local user's directory.
su -c "ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar" localuser
Running this command on the command line manually works great but I can't for the life of me work out how to pass this as an argument to /bin/bash/.
I have tried:
/bin/bash -c "su -c "ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar" localuser"
And various combinations using different syntax but I am just guessing as I don't really understand why it's not working.
I have broken it down to a simpler example and realised it works if inner command uses single quote like this simple example that gets the home path:
bash -c "su -c 'cd ~ && pwd' localuser"
but trying that on larger command causes it to fail:
/bin/bash -c "su -c 'ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar' localuser"
It says no passwd entry for user /home so the command is getting broken up I guess buy the nested single quotes but im not sure how to fix this.
I tried putting double quotes outside the single quotes:
/bin/bash -c "su -c 'ssh -p 1234 remoteuser#123.123.123.123 "'cd /home/ && tar -cf - remoteuser/'" | pv > /home/staging/localuser/staging.tar' localuser"
But then it says it can't find the directory. It looks like I just need to tweak the command a bit but I can't figure it out, can anyone help?
It is a matter of quoting in the right way. There is more than one way to do this. I find double quotes easier to work with for this case:
echo "su -c \"ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar\" localuser"
Prints:
su -c "ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar" localuser
Which I think is what you are looking for. That is, escaping with \" any double quotes inside the outer double quotes. So try:
/bin/bash -c "su -c \"ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar\" localuser"
This is where here-documents come in handy:
bash <<'END'
su -c "ssh -p 1234 remoteuser#123.123.123.123 'cd /home/ && tar -cf - remoteuser/' | pv > /home/staging/localuser/staging.tar" localuser
END
Note the -c option has been removed: bash will read the commands from stdin.

How to store output of sudo -S su -c <user> <command> to any variable

I am trying to execute the following command but the output is not coming as required.
var=$(echo "<password>"|sudo -S su -l <user> -c "<command>")
Please help if anyone can?
Expected Result:
var=$(echo ""|sudo -S su -l -c "pwd")
echo $var /home/bhushan
$:
Actual Result:
echo $var
$:
You can use backticks
var=`sudo -S su -l -c ""`
or the $(command) syntax
var=$(sudo -S su -l -c "")
(keep in mind though that sudo -S su -l -c "" doesn't output anything so $var will be empty)
You can workaround it by storing the output of the command into a file, then change its permission so that all users will see it and in a following command load it from the file:
sudo -S "<command> > /tmp/sudocmd.out && chmod 644 /tmp/sudocmd.out"
var=$(cat /tmp/sudocmd.out)

Using genisoimage and cdrecord in Linux

I want to perform an encrypted multisession CD by first using genisoimage followed by cdrecord libraries.
Here are the steps that are I would need to do.
sudo su -c yum install cdrecord
sudo apt-get install loop-aes-utils aespipe
sudo modprobe cryptoloop
genisoimage -quiet -r ~/Desktop | aespipe -T -e aes256 > image.iso
genisoimage -quiet -r ~/Desktop | cdrecord dev=ATAPI:0,1,0 -v -multi -pad `enter code here`-data -
sudo mkdir /media/iso
sudo mount -o loop,encryption=aes256 image.iso /media/iso
wodim --devices
wodim dev=/dev/sg1 image.iso
cdrecord dev=ATAPI:0,1,0 -v -audio track*wav
cdrecord dev=ATAPI:0,1,0 -v -audio track01.cdda.wav track01.cdda.wav `enter code here`middle.wav last.wav
cdrecord dev=ATAPI:0,1,0 -v -dao -audio track*wav
cdrecord -v -eject speed=4 dev=0,4,0 -multi cd-iso-image-file-1.iso
cdrecord dev=0,4,0 -msinfo
mkisofs -J -r -V SecondBurn -o cd-iso-image-file-2.iso -C 0,16774 -M `enter code here`0,4,0 <directory-with-files-to-add>
cdrecord -v -dummy dev=2,0 cdimage.raw -audio track*.cdaudio
I do hope this will be okay. If anybody has any questions or comments regarding this task, please do not hesitate to reply.
JohnDB

command hangs head / netcat

I am using two linux machines to simulate some firewall tests... I execute the tests by running nc through ssh on a remote machine... if I spawn the ssh like this, it works...
ssh -i id_dsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
-p 2224 root#a2-idf-lab nc -s 10.26.216.82 10.195.18.132 \
21 < /var/log/messages
However, if I try to control how much of /var/log/messages with head -c 20 /var/log/messages, the command hangs but I don't understand why...
ssh -i id_dsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
-p 2224 root#a2-idf-lab nc -s 10.26.216.82 10.195.18.132 \
21 < head -c 20 /var/log/messages
I also tried this with no better success...
ssh -i id_dsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
-p 2224 root#a2-idf-lab nc -s 10.26.216.82 10.195.18.132 \
21 < (head -c 20 /var/log/messages)
Question: Why does the second command hang, and how can I accomplish what I need?
FYI, these experiments were really in preparation for sending cat /dev/urandom | base64 | head -c 20 - into nc... bonus points if you can give me cli that would work with nc through an ssh session...
< is shell redirection, it redirects the input stream to read from a file, not to execute a command. try:
head -c 20 /var/log/messages | ssh -i id_dsa -o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
-p 2224 root#a2-idf-lab nc -s 10.26.216.82 10.195.18.132 21
this pipes /var/log/messages from the local machine into nc on the remote machine.
if you want to use the /var/log/messages file on the remote machine, use quotes around the command:
ssh -i id_dsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
-p 2224 root#a2-idf-lab "head -c 20 /var/log/messages |\
nc -s 10.26.216.82 10.195.18.132 21"
Try to use
head -n 20
My guess is the problem is the lack of carriage return at the end.

Bash: wrong pipe output when using "sudo sh -c"

I have a pipe of commands and I use "sudo sh -c" for getting sudo permission throughout the whole pipe commands.
The problem that I am facing is that commands like awk have different behaviour when "sudo sh -c" is used.
In particular,
sudo wc -c Mybib.bin |sudo awk '{print $1;}'
gives 1509644
while
sudo sh -c "wc -c Mybib.bin |awk '{print $1;}'"
gives 1509644 Mybib.bin
So, in the second case looks like the awk command is not invoked at all.
Thanks for any help.
It is, but the double quotes are allowing the $1 to be replaced before invocation, resulting in {print ;}.
sudo sh -c "wc -c Mybib.bin |awk '"'{print $1;}'"'"
Also...
sudo sh -c "wc -c < Mybib.bin"
I also found that you can escape the elements in sh -c as well.
For example:
sudo sh -c "wc -c Mybib.bin |awk '{print \$1;}'"
At least..that's working for me right now with a problem I ran into that was similar to yours.

Resources