Vagrant provision sed command returns error - sed

I am trying to run a shell sed command as part of the config.vm.provision
config.vm.provision "shell", inline: <<-SHELL
systemctl stop firewalld
systemctl disable firewalld
swapoff -a
sed -i '/\/swapfile/s/^/#/g' /etc/fstab
SHELL
Basically what I want to do is comment the swap entry in the /etc/fstab file.
But I get an error :
master: Running: inline script
master: sed: -e expression #1, char 18: unterminated `s' command
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.
The sed command runs perfectly fine when I run in on the box but errors out when I provide it as a part of my Vagrantfile.
Any idea why this might be ?

Can you try?
$script = <<-SCRIPT
systemctl stop firewalld
systemctl disable firewalld
swapoff -a
sed -i '/\/swapfile/s/^/#/g' /etc/fstab
SCRIPT
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: $script
end

Related

How to log the output along with error messages to a file while running a script on psql command line on Freebsd OS?

On RHEL, the below command works:
psql -h hostname -U username -p port_no -d database -f /tmp/myfile.sql &> logfile01.txt
On FreeBSD, this throws error:
"Invalid null command"
Please suggest.
If you use this only on the command line then there is no need to change the shell.
To redirect stdout and stderr to a file in C-Shell synthax simply use ">& filename".
Different story is, if you want to write shell scripts. Bourne Shell and it's clones (like i.e. Bash) are better suited for writing script. See this Unix FAQ "Csh Programming Considered Harmful": http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
This redirection works in bash
&> logfile01.txt
, but it does not work in csh which is the default shell in FreeBSD.
# set | grep shell
shell /bin/csh
# ls -la &> logfile01.txt
Invalid null command.
Bash is not installed by default. You can install it
pkg install bash
and configure it as the default shell.

Start shrew vpn client (iked & ikec) on start-up of OSMC on Raspberry 2

I would like to connect to a VPN on start-up of OSMC.
Environment:
installed OSMC on Raspberry 2
downloaded, compiled and installed shrew soft vpn on the device
As user 'osmc' with ssh
> sudo iked starts the daemon successfully
> ikec -r "test.vpn" -a starts the client, loads the config and connects successfully
rc.local:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
sudo iked >> /home/osmc/iked.log 2>> /home/osmc/iked.error.log &
ikec -a -r "test.vpn" >> /home/osmc/ikec.log 2>> /home/osmc/ikec.error.log &
exit 0
after start of raspberry iked is as process visible with ps -e
but ikec is not running
osmc#osmc:~$ /etc/rc.local starts the script and connects to vpn successfully
Problem:
Why does the script not working correctly on start-up?
Thank you for your help!
I was also looking to do the same thing as you and ran into the same problem. I'm no linux expert, but I did figure out a workaround.
I created a script called ikec_after_reboot.sh and it looks like this...
$ cat ikec_after_reboot.sh
#!/bin/bash
echo "Starting ikec"
ikec -r test.vpn -a
I then installed cron.
sudo apt-get update
sudo apt-get install cron
Edit the cron job as root and run the ikec script 60 seconds after reboot.
sudo crontab -e
SHELL=/bin/bash
#reboot sleep 60 && /home/osmc/ikec_after_reboot.sh & >> /home/osmc/ikec.log 2>&1
Now edit your /etc/rc.local file and add the following.
sudo iked >> //home/osmc/iked.log 2>> /home/osmc/iked.error.log &
exit 0
Hopefully, this is helpful to you.

Mongo mongod init.d script not working on CentOS

I am trying to figure out why the provided init.d script is not working on CentOS. I tried starting it manually:
/etc/init.d/mongod start
But I get the following error:
Starting mongod: /usr/bin/dirname: extra operand `2>&1.pid'
Try `/usr/bin/dirname --help' for more information.
I looked in the script where it tries to start:
daemon --user "$MONGO_USER" "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
So I looked where mongod var is defined:
mongod=${MONGOD-/usr/bin/mongod}
Also tried:
service mongod start
Same error.
Not sure what I have setup wrong, but I have verified that I have the latest script but I cannot get mongod process to start.
Any ideas???
The following link appears to address the issue well
https://ma.ttias.be/mongodb-startup-dirname-extra-operand-pid/
In a nutshell, a bad script appears to have been distributed but the output it produces is not harmful, mongod still runs. If you run yum update you'll get a fixed script, but likely mongod will still fail because the script was not making it fail. Check your mongo logs (usually /var/log/mongodb/mongod.log, but can be different if specified in differently in /etc/mongod.conf). The log file should tell you the real reason it's failing.
Check the mongo pid file location in the config file /etc/mongod.conf:
awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' /etc/mongod.conf
By default there should be this line in mongod.conf: 'pidfilepath = /var/run/mongodb/mongod.pid'. Add it if it doesn't exist.
If you are using the YAML version of /etc/mongod.conf, check out this issue: https://jira.mongodb.org/browse/SERVER-13595. In short, you need to change this line in /etc/rc.d/init.d/mongod:
PIDFILE=`awk -F= '/^pidfilepath[[:blank:]]*=[[:blank:]]*/{print $2}' "$CONFIGFILE"`
to:
PIDFILE=`awk -F: '/^[[:blank:]]*pidFilePath[[:blank:]]*:[[:blank:]]*/{print $2}' "$CONFIGFILE" | tr -d ' '`
For me problem was in pidfilepath. Init script can't deal with path in format like this
pidfilepath = /var/run/mongodb/mongod.pid
PIDFILE variable inside of init script contains ' /var/run/mongodb/mongod.pid' and not '/var/run/mongodb/mongod.pid'
FIX:
replace PIDFILE line with this and it will work.
PIDFILE=awk -F= '/^pidfilepath[[:blank:]]*=[[:blank:]]*/{gsub(" ", "", $2);print $2}' "$CONFIGFILE"
I have also faced the same issue.
The fix is to make a small change in the script file(/etc/init.d/mongod) as mentioned below:
line 63 i guess:
daemon --user "$MONGO_USER" "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
to
daemon --user "$MONGO_USER" --pidfile "$PIDFILE" "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
Hope this helps !!!
It could be a RedHat bug on the initscript package:
goog.le forum
redhat bugzilla

Running last command with sudo in fish only works if it has no arguments?

I'm trying to write a function that does the equivalent of sudo !! in Bash. It works, but only when the last command has no arguments.
So far the function is:
function s --description "Run last command (or specified command) using sudo"
if test $argv
switch $argv[1]
case '!!'
command sudo (echo $history[1])
case '*'
command sudo $argv
end
else
command sudo fish
end
end
Testing the relevant line:
$ command sudo whoami
root
$ whoami
nick
$ command sudo (echo $history[1])
root
So far so good, now lets try a command with a few args:
$ echo hi >> /etc/motd
An error occurred while redirecting file '/etc/motd'
open: Permission denied
$ command sudo (echo $history[1])
sudo: echo hi >> /etc/motd: command not found
Hmm, strange.
Got it working using eval.
function sudo --description 'Run command using sudo (use !! for last command)'
if test (count $argv) -gt 0
switch $argv[1]
case '!!'
if test (count $argv) -gt 1
set cmd "command sudo $history[1] $argv[2..-1]"
else
set cmd "command sudo $history[1]"
end
case '*'
set cmd "command sudo $argv"
end
else
set cmd "command sudo fish"
end
eval $cmd
end
I had the same problem as you, and I fixed it by using oh-my-fish
(it's a plugin manager for fish shell) https://github.com/oh-my-fish/oh-my-fish. You can install it with this command :
curl -L https://get.oh-my.fish | fish
Then install the plugin bang-bang (to allow !! and !$) with this command :
omf install bang-bang

SUDO - iOS FTP - Stuck

I have a script for MobileTerminal in iOS that requires su,
is there any way I can add a command to the bash script to login as root without having to type su then the password?
Cheers,
Dec
SUDO and system functions are disallowed in iOS, they violate sandboxing and security.
That's the solution worked for me:
Jaibreak
Install sudo from Cydia
Install OpenSSH from Cydia
SSH to your iPhone
$ssh root#<your iphone ip>
Password: alpine //no echo
and run
iPhone:~ root# visudo
add right after
root ALL=(ALL) ALL
this line (varies of what you want)
mobile ALL=(ALL) ALL
Save changes. Now you can sudo. For editing with visudo you should use controls like in vi
After all add to your script hardcoded sudo call like this
#!/bin/bash
sudo -S -p "" echo -n "" <<!
alpine
!
sudo echo "This line is printed as root"
Now explanation:
-S allows reading password from <<!\n ... \n! block, it should be printed on a single line
-p "" suppresses password promt, so the line Password: is not printed
echo -n disallows a newline character in the end
So you can sudo in your script without any password promt. The only thing - in this method password is hardcoded. But you can try command line arguments like $1 (not tested, just an idea)