Use of su command in c code - c

I want to use a command in my c code. This command works properly when i am a superuser in terminal (using su) but does not work when I use sudo. I gathered that I can use system() in my c code as follows
system("su -c \"mycommand\"")
myCommand is a command that sends input to some device, like (echo 1 > mydevice)
I compiled my c code using gcc and run the output file. The trouble is, it wants root password from terminal when it comes to this line.
Is there a way I supply password programatically? I did it before with sudo command with -S option, but sudo command does not work for this case. If it is not possible, is there another way to deal with this?

Try this instead,
system("sudo sh -c \"mycommand\"");

Related

How can I answer a command that needs input in Plink?

I run a command in Linux remote machine using plink (from batch file in Windows).
For example:
plink.exe -ssh root#IP -pw pass -m testCommands.txt >> uninstall.log
In testCommands.txt I have a command that perform uninstall of application,
the problem is that command of "uninstall" require an answer (y or n),
How can I send answer in addition to what I already send?
Here is the question I have:
[?7hAre you sure you want to completely remove "APPLICATION" and all of its components?
Yes [y, Enter], No [n]"
There are several options...
Use a -y or auto-confirm option on the command that removes the software, e.g.
pkg rm -yes somePackacge
Use a pipe to send the yes, so that the command you run in PLINK looks like
printf "Yes\n" | pkg rm some_package
or
yes | pkg rm some_package
Make the command that you run on the Linux machine be based on expect which can spawn your remove command, wait for a question and then send it a confirmation. Some expect examples here.

issue with array in shell on ubuntu

I used to have a server running CentOS, and I used to execute shell files this way:
sudo sh /folder/script.sh
Now I have an Ubuntu server. When I'm executing the same command line, I now have the following error message:
/folder/script.sh: ID[0]=ID: not found
I had a look on the internet and it says I need to use:
sudo /bin/bash /folder/script.sh
But when I do so I got the same error message.
The first line of my script is:
ID[0]="ID"
/bin/sh is often a POSIX shell, which does not support arrays.
I suggest you install another shell which does support them, like mksh (disclaimer: I’m its developer), ksh93, zsh, or just use GNU bash instead, and call your script with, for example, sudo mksh /folder/script.sh instead. This will give you more consistent behaviour across systems, too (note that to behave consistent on all platforms is actually an mksh design goal).
Hm… this works for me:
$ cat >x
#!/bin/bash
ID[0]="ID"
echo works for me
$ mksh x
works for me
Do you have any weird characters in your script, like embedded Carriage Return (^M)? Check with: cat -v /folder/script.sh

I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"

I have written the following code:
#!/bin/bash
#Simple array
array=(1 2 3 4 5)
echo ${array[*]}
And I am getting error:
array.sh: 3: array.sh: Syntax error: "(" unexpected
From what I came to know from Google, that this might be due to the fact that Ubuntu is now not taking "#!/bin/bash" by default... but then again I added the line but the error is still coming.
Also I have tried by executing bash array.sh but no luck! It prints blank.
My Ubuntu version is: Ubuntu 14.04
Given that script:
#!/bin/bash
#Simple array
array=(1 2 3 4 5)
echo ${array[*]}
and assuming:
It's in a file in your current directory named array.sh;
You've done chmod +x array.sh;
You have a sufficiently new version of bash installed in /bin/bash (you report that you have 4.3.8, which is certainly new enough); and
You execute it correctly
then that should work without any problem.
If you execute the script by typing
./array.sh
the system will pay attention to the #!/bin/bash line and execute the script using /bin/bash.
If you execute it by typing something like:
sh ./array.sh
then it will execute it using /bin/sh. On Ubuntu, /bin/sh is typically a symbolic link to /bin/dash, a Bourne-like shell that doesn't support arrays. That will give you exactly the error message that you report.
The shell used to execute a script is not affected by which shell you're currently using or by which shell is configured as your login shell in /etc/passwd or equivalent (unless you use the source or . command).
In your own answer, you say you fixed the problem by using chsh to change your default login shell to /bin/bash. That by itself should not have any effect. (And /bin/bash is the default login shell on Ubuntu anyway; had you changed it to something else previously?)
What must have happened is that you changed the command you use from sh ./array.sh to ./array.sh without realizing it.
Try running sh ./array.sh and see if you get the same error.
Instead of using sh to run the script,
try the following command:
bash ./array.sh
I solved the problem miraculously. In order to solve the issue, I found a link where it was described to be gone by using the following code. After executing them, the issue got resolved.
chsh -s /bin/bash adhikarisubir
grep ^adhikarisubir /etc/passwd
FYI, "adhikarisubir" is my username.
After executing these commands, bash array.sh produced the desired result.

Executing ssh command in c program using popen()

When I try executing this using popen it returns this error but when I run this in terminal it works!
popen("ssh -n -f *.*.*.* 'sshfs -o nonempty *.*.*.*:/home/foo/bar/ /foo1/foo2/foo3'", "r");
error:
ssh_exchange_identification: Connection closed by remote host
I use public and private key to ssh without passwords and they work properly as this command run flawlessly in terminal.
I changed it to this :
popen("ssh -n -f *.*.*.* `sshfs -o nonempty *.*.*.*:/home/foo/bar/ /foo1/foo2/foo3`", "r");
It return errors too.
error :
fuse: bad mount point `/foo1/foo2/foo3': No such file or directory
Cannot fork into background without a command to execute.
I also tried escipping the internal "" this way : \" \" but it hangs!
Replace ssh with /usr/bin/ssh, do the same with other commands, like sshfs. Specify the full path of the command, /usr/sbin/foo or whatever the case may be. popen does not necessarily use the same shell you have at the command line to execute commands. Check your documentation.

How to create file and put string on it using shellscript?

I want to create a file in /usr/share/applications/ and put a string on it.
What I have so far:
sudo touch /usr/share/applications/test.desktop
dentry="testing"
sudo echo $dentry >> /usr/share/applications/test.desktop
But this raise an error Permission Denied. What should I do to make it works?
You should create the file using your own pernissions, then sudo cp it into place.
The reason the second command doesn't work is that the redirection is set up by your shell, before sudo even runs. You could work around this by running sudo sh -c 'echo stuff >>file' but this is vastly more risk-prone than a simple sudo cp, and additionally has a race condition (if you run two concurrent instances of this script, they could end up writing the information twice to the file).

Resources