I have a Redhat 6.8 cluster with several nodes in it that I'm trying to sync the Active Directory UID and GID to utilizing winbind. I am attempting to sync the output of wbinfo -u to all the nodes but only want the relevant AD accounts with the uid and gid fields populated. I tried this with:
for i in `wbinfo -u`; do id ${i} | awk '/uid/{ print $0}' ; done
I end up getting all of the wbinfo -u results as if I ran it by itself.
Is there a way to just grep/awk/sed the results with uid in the beginning of it? I apologize for not showing the output of what I ran, this system isn't connected.
Ok, I think I figured it out. When I run the:
for i in wbinfo -u; do id ${i} ; done
It outputs all of the errors on accounts without uid/gid as well as the accounts with the successful attempts with uid/gid to the screen, but when I redirected the output of the command to /tmp/test:
for i in wbinfo -u; do id ${i} ; done > /tmp/test
It only outputs the successful results, which is what I needed. So I guess 'id' when redirected only shows successful attempts. Go figure. Thanks guys.
Related
Is it possible to listen for a specific notification on Mac and act upon it using Automator?
I regularly use an app that runs a background job then sends a notification when it's finished. The app stays open after the job is finished so I'd like to use Automator to quit the app when the notification is received.
If it's not possible in Automator is there another way I could do this?
More context: the app is actually launched by a Folder Action created using Automator. It detects when a specific SD card is inserted and runs a backup app on that SD card. So maybe there's something I can add to that Folder Action workflow that can detect the notification?
While it's certainly possible to query the sqlite database containing notifications in macOS, it seems to me like an unnecessarily complicated route, and I would first try the following...
In your workflow, add a 'Run Shell Script' action at the end, containing something like this:
while [ $(ps -e | grep "[N]ameOfBackgroundProcess" | wc -l) -gt 0 ]; do
sleep 3
done
killall "NameOfAppToQuit" 1&>2 /dev/null
The while loop checks whether the background job is still running.
ps -e lists all running processes.
grep "[N]ameOf..." gets all lines containing the name. Brackets around the first letter excludes the grep process itself from the output.
wc -l counts the lines.
-gt 0 checks if the number is greater than zero.
When the loop is done, that means the process has exited so we quit the app with killall.
As for the notification route...
I haven't figured everything out, but this might give you a head start:
#!/usr/bin/env bash
# Get the directory of the Notification Center database (works for me in Big Sur):
db_dir=$(lsof -p $(ps aux | grep -m1 usernoted | awk '{ print $2 }')| awk '{ print $NF }' | grep 'db2/db$' | xargs dirname)
# Get the app_id:
app_id=$(sqlite3 "$db_dir"/db 'SELECT app_id FROM app WHERE identifier="com.example.identifier";')
# Get relevant records:
sqlite3 "$db_dir"/db "SELECT * FROM record WHERE app_id='$app_id';"
# And this is where I leave you.
To explore the database in a GUI, try https://sqlitebrowser.org/
how to write a script in linux to login into several serval one by one and fetch their description ?
I am learning shell scripting, tried few commands but not able to arrange logic into code.
As my question gives a wide range and possiblities to think,so to be exact I want to create a script that opens a file in /tmp name as 'list' which contains many IP addresses, Then I have to login in those IP's one by one by ssh command and for loop and then after logging in by using awk command I want to fetch 7th column which gives info about server.
I am just on intial stage of shell scripting donot ahve that understanding,
I tried below command but this didn't worked out for me.
for line in cat /tmp/list
do
echo $i
echo "***********"
ssh $i |grep tsm |awk -F : '{print $7, "\t", $1}'
echo
done
Overview: I am trying to make a script that will take a list of machines and manually update their /etc/shadow files with a new root passwd. I know this isn't the best method but my boss wants this process automated. we are using a application called puppet for 90% of the update but some machines failed the update or can't have puppet installed, hence this dodgy fix.
(sorry for any stupid errors its only my 3rd week using any unix product, I have been a windows admin my whole life)
Issue:
I need to ssh into the PC's update the /etc/shadow file but only change the root user (not all systems have the same users and I don't want to remove any of those users in the process) I have gotten as far as being able to extract the current user in line 1 through ssh, then check if that user is indeed the root user but I am stuck on then updating the /etc/shadow file on the new machine as my boss has asked that the following standards happen.
I can't have any real user interaction in the script, so no manually typing the new passwd.
I am not allowed to have the new passwd displayed anywhere in clear text (inside the script or in another file)
Ok hopefully that's enough info onto the code.
root=user
unknown='unknown.txt'
filelines=`cat $unknown`
prod='new-shadow'
ohf='option-one-holding-file'
pel=prod-errorlog
for line in $filelines ; do
echo "Attempting to fix $line please wait"
ssh -oBatchMode=yes -l $user $line "awk '{if (NR==1) print \$0}' /etc/shadow" >> $ohf
if grep -q "root:" $ohf ; then
echo "root user located updating to produtcion password"
# ** This is the line that doesn't work **
ssh -oBatchMode=yes -l $user $line "sed -i '1s/.*/$prod/' /etc/shadow"
else
echo "unable to find root user this will require a manual fix this server will be listed in
the prod-errorlog file"
echo "$line" >> $pel
fi
done
The line in bold the sed line doesn't work I know why it doesn't work but I have no idea how to fix it at all, thank you to anyone who takes the time to look at this, I know the codes a bit of a mess, please forgive me.
To replace only the first line:
"echo '$prod' > /etc/shadow.new; tail -n +1 /etc/shadow >> /etc/shadow.new; mv -f /etc/shadow.new /etc/shadow"
Sorry for my previous wrong argument wrong: The '$prod' part in your script is correct, and is expanded OK. Yet $prod contains many reserved characters for regular expressions. Now this new version just create a new file (replacing the first line) and then move/overwrite on to the target one.
I have been using this to check if the process i want to edit is already running.
Now this returns if any user has this process running, but since multiple users now run it, i need this line to only return true if the current user has it running. I already have something to execute something as_user and the username is saved in ME.
if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null
$LOGNAME provides the current user name. So in case you are using the command to run from X user and want to check for that specific user process, then you can add additional grep for $LOGNAME. I am using SUSE-Linux. In case you are using any other OS, Please specify.
I've written a shell script in CentOS. It basically connects to a database and displays certain information.
i have put at the beginning the following instructions:
#!/bin/bash
echo "Content-type: text/plain"
echo ""
If I run it in the Linux Command line I get the results and the output as desired, but If I run it using the web browser, it displays nothing. I have put the script in the cgi-bin directory. As additional information I have some other scripts using nmap and curl working without problem.
Basically I want to display the status of some extensions in Asterisk IP-PBX
for i in `asterisk -rx 'sip show peers'|grep -ai Uns |sort -n |grep ^2| awk '{print $1}' | grep [a-z]`; do
Thanks for your time.
You have put full path to asterisk.
Also you have test script under same user as your webserver. Very likly you have change in /etc/asterisk/asterisk.conf permissions for asterisk ctl file.