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

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.

Related

script to connect to a "list.txt" of servers

I am trying to find a way to connect to a list of servers written in a simple textfile to run one command and write the output to a file...
The small problem is, I have to login with a password... but it would not a problem to paste the password into the script.
the full command would be:
ssh "server_from_list.txt uptime | awk -F, '{sub(".*up ",x,$1);print $1}' >> /home/kauk2/uptime.out
lets assume the password is: abcd1234
Any suggestions??? I am not fit in scripting, sorry...
Many thanks to you all in advance...
regards,
Joerg
Ideally you should set up password-less login, but failing that you can use sshpass. First, get a single command working by trying the following:
export SSHPASS=abcd1234
Then you can try:
sshpass -e ssh user#server1 'uname -a'
When you get that debugged and working, you can use GNU Parallel to run the command on all servers in a file called list.txt
user#server1
user#server2
user#server3
user#server4
The command will be:
parallel -k -a list.txt sshpass -e ssh {} 'uptime'

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!! :)

ChromeOS init problems

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.

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.

SSH to server, Sudo su - then run commands in bash [duplicate]

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 6 years ago.
I have the following
#!/bin/bash
USER='scott'
PASS='tiger'
ssh -t $USER#server006.web.com "sudo su - http"
This Works, but I was trying to get it to run a script afterwards, and if I do, using -c or <
The script does a grep like this:
grep -i "Exception:" /opt/local/server/logs/exceptions.log | grep -e "|*-*-*:*:*,*|" | tail -1 | awk -F'|' '{print $2}' >> log.log
This also works fine on it's own, but I need to be http to do it.
I cannot SCP the output of the script back to server001 either, so I'm stuck here,
Any ideas would be relay appreciated.
Ben
Try
ssh -t $USER#server006.web.com 'sudo -u http grep -i "Exception:" /opt/local/server/logs/exceptions.log | grep -e "|*-*-*:*:*,*|" | tail -1 | awk -F"|" "{print $2}" >> log.log'
Sudo already runs the command as a different user to there's no need to su again.
Only reason to do sudo su is to have a fast way to start a new shell with another user.
You probably want sudo -u instead of sudo su -:
ssh -t $USER#server006.web.com sudo -u http script
Guess I'm late to the party.
My solution:
ssh -t $USER#server006.web.com "sudo cat /etc/shadow"
and replace cat /etc/shadow with your desired program.

Resources