ChromeOS init problems - bash

Currently having issues when attempting to run a MAC changing script on startup.
Currently running ChromeOS which is using upstart as it's init system however I've attempted multiple different ways to make the script run at boot with no luck.
Running the script alone via sudo sh updatemac.sh has it working correctly, code here:
#!/bin/bash
NEW_MAC=`echo -n 00; hexdump -n 5 -v -e '/1 ":%02X"' /dev/urandom;`
ifconfig wlan0 down
ifconfig wlan0 hw ether $NEW_MAC
ifconfig wlan0 up
I've attempted to create a startup job a few different ways.
First way was placing updatemac.conf in /etc/init with the code:
start on star-user-session
task
script
NEW_MAC=`echo -n 00; hexdump -n 5 -v -e '/1 ":%02X"' /dev/urandom;`
ifconfig wlan0 down
ifconfig wlan0 hw ether $NEW_MAC
ifconfig wlan0 up
end script
However that didn't work. I also tried replacing the script / end script section with both
exec /home/user/chronos/Downloads/updatemac.sh
And
exec sh /home/user/chronos/Downloads/updatemac.sh
Neither of which worked.
The other way I tried was adding the script directly to /etc/init.d/updatemac.sh
However that also failed for me.

Solved it, seems it needed
start on started system-services
In the .conf for /etc/init instead of
start on star-user-session
Also removed "task" from the script.

Related

how does bash do `ssh` autocompletion?

I've got the "bash-completion" package installed.
ssh completion on the command line (in bash) is working: ssh TAB-TAB will complete past used hosts and ssh -TAB-TAB will complete available ssh options.
However when I search for currently defined completions:
$ complete | grep ssh
complete -F _known_hosts ssh-installkeys
complete -F _service /etc/init.d/ssh
... I find that there's no completion registered for ssh ?!
complete -p ssh
bash: complete: ssh: no completion specification
When I check the ssh completions script under /usr/share/bash-completion/completions/ssh then I see that indeed it does register ssh completions:
$ grep complete /usr/share/bash-completion/completions/ssh | grep ssh | grep -v '^#'
shopt -u hostcomplete && complete -F _ssh ssh slogin autossh sidedoor
So why doesn't the ssh completion show up in complete | grep ssh? How does bash complete the ssh options?
If there is no completion defined for a command (or a function or whatever) then the "default" completer kicks in.
That default completer can be seen here:
$ complete -p -D
complete -F _completion_loader -D
When the bash-completion package is installed, then that will source /usr/share/bash-completion/bash_completion via /etc/bash.bashrc and that will assign the _completion_loader function.
_completion_loader will dynamically load specific completers upon completion request for commands the shell's completer doesn't know about yet (see here).
In the case of ssh, the terminals where I was doing the poking around had not yet loaded the ssh completer and so I was not seeing it with complete | grep ssh or complete -p ssh. It's only at the moment when you press TAB-TAB for the first time that the completer for ssh gets loaded.

How to ssh to a server and get CPU and memory details?

I am writing a shell script where i want to ssh to a server and get the cpu and memory details data of that displayed as a result. I’m using the help of top command here.
Script line:
ssh -q user#host -n “cd; top -n 1 | egrep ‘Cpu|Mem|Swap’”
But the result is
TERM environment variable is not set.
I had checked the same in the server by entering set | grep TERM and got result as TERM=xterm
Please someone help me on this. Many thanks.
Try using the top -b flag:
ssh -q user#host -n "cd; top -bn 1 | egrep 'Cpu|Mem|Swap'"
This tells top to run non-interactively, and is intended for this sort of use.
top need an environment. You have to add the parameter -t to get the result:
ssh -t user#host -n "top -n 1 | egrep 'Cpu|Mem|Swap'"
Got it..!! Need to make a small modification for the below script line.
ssh -t user#host -n "top -n 1 | egrep 'Cpu|Mem|Swap'"
Instead of -t we need to give -tt. It worked for me.
To execute command top after ssh’ing. It requires a tty to run. Using -tt it will enable a force pseudo-tty allocation.
Thanks stony for providing me a close enough answer!! :)

How to make Automator run Shell Script that requires sudo password?

I used this script below to change mac address randomly every time it is ran.
openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//' | xargs sudo ifconfig en0 ether
ifconfig en0 | grep ether
I want Automator to do it for me. When I run this Shell Script, it runs successfully but when I actually open Terminal and run
ifconfig en0 | grep ether
to see if it changed MAC address I find out it didn't.
If i manually enter such script into Terminal, it works perfectly.
What should I do?
Try creating an AppleScript instead:
on run {input, parameters}
do shell script "openssl rand -hex 6 | sed 's/\\(..\\)/\\1:/g; s/.$//' | xargs sudo ifconfig en0 ether" with administrator privileges
do shell script "ifconfig en0 | grep ether"
return input
end run
It should ask you to enter your adminstrator password, then change the mac address. Automater is better suited to run Applescripts generally, as shell scripts can sometimes be problematic.
You can place them directly in your root directory. You will need to have root privileges to access the function but it will not require pass phrase input.

ssh blocking when trying to store command output to variable

I'm not quite sure what the issue is. I'm on Kali Linux 2.0 right now, fresh install. The following worked on Ubuntu 14.04 but it's not working anymore (maybe I accidentally changed it?). It looks correct to me, but every time it runs it blocks.
backup_folder=$(ssh -i /home/dexter/.ssh/id_rsa $server 'ls -t '$dir' | head -1')
This is part of a larger script. $server and $dir are set. When I run the command alone, I get the correct output, but it doesn't end the connection.
I don't know if this may help to solve the question but your command doesn't handle dirs with space in the filename. Add double quotes inside the single quote section like this:
SERVER='remoteServer' && REMOTE_DIR='remoteDir' && backup_folder=$(ssh -i /home/dexter/.ssh/id_rsa "${SERVER}" 'ls -t "'${REMOTE_DIR}'" | head -n1'); echo "${backup_folder}"
If it doesn't help try to add increasing number of -v switch to ssh to debug eventually reaching:
SERVER='remoteServer' && REMOTE_DIR='remoteDir' && backup_folder=$(ssh -vvv -i /home/dexter/.ssh/id_rsa "${SERVER}" 'ls -t "'${REMOTE_DIR}'" | head -n1'); echo "${backup_folder}"
If the verbose output does not help may be an MTU problem (these kind of problems are not of binary type, acts strangely).
You can try lowering MTU (usually 1500) on your side to solve:
sudo ifconfig eth0 mtu 1048 up
eth0 is obviously an example interface, use your own.

Is it possible to run two programs simultaneously or one after another using a bash or expect script?

I have basically two lines of code which are:
tcpdump -i eth0 -s 65535 -w - >/tmp/Captures
tshark -i /tmp/Captures -T pdml >results.xml
if I run them both in separate terminals it works fine.
However I've been trying to create a simple bash script that will execute them at the same time, but have had no luck. Bash script is as follows:
#! /bin/bash
tcpdump -i eth0 -s 65535 -w - >/tmp/Captures &
tshark -i /tmp/Captures -T pdml >results.xml &
If anyone could possibly help in getting this to work or getting it to "run tcpdump until a key is pressed, then run tshark. then when a key is pressed again close."
I have only a little bash scripting experience.
Do you need to run tcpdump and tshark separately? Using a pipe command will feed the output of tcpdump to the input of tshark.
tcpdump -i eth0 -s 65535 | tshark -T -pdml > results.xml

Resources