My staff seem to have a habit of leaving thier PCs on over night so I need to write a short script to shutdown any XP clients logged into the local samba domain that I can run as a cron job at a set time.
I can list the connected clients and their IP addresses with:
$ smbstatus -b
Samba version 3.4.9
PID Username Group Machine
-------------------------------------------------------------------
5190 user1 __USERS__ machine1 (10.0.0.10)
26799 user2 __USERS__ machine2 (10.0.0.6)
4173 user3 __USERS__ machine3 (10.0.0.11)
And I can shutdown individual clients with:
$ net rpc SHUTDOWN -C "shutting down" -f -I 10.0.0.6 -U adminuser%password
What I need the code to do is parse the result of the smbstatus command and pass the IP addresses to the net rpc command for as main IP addresses found.
Thanks
smbstatus -b | awk -F"[()]" '{ print $2 }' | while read IPADDRESS
do
echo "IP address is $IPADDRESS"
done
Thanks Corona;
So my script will look like:
#!/bin/bash
smbstatus -b | awk -F"[()]" '{ print $2 }' | while read IPADDRESS
do
net rpc SHUTDOWN -C "shutting down" -f -I $IPADDRESS -U adminuser%password
done
Will this loop for as many IP addresses found or just the first one?
Thanks
It ought to loop through all of them.
I'd try what I actually posted first, before you get too ahead of yourself. If your version of awk doesn't support regexes in -F it may not work.
OK, here's the output:
~$ smbstatus -b | awk -F"[()]" '{ print $2 }' | while read IPADDRESS
> do
> echo "IP address is $IPADDRESS"
> done
IP address is
IP address is
IP address is
IP address is
IP address is 10.0.0.10
IP address is 10.0.0.11
I guess I need to start on the 5th line of the output from smbstatus -b command? I am using ubuntu10.04
I think I can fix that:
smbstatus -b | awk -F"[()]" '$2 { print $2 }' | while read IPADDRESS
do
echo "IP address is $IPADDRESS"
done
That should make awk only print lines when $2 (the second token) isn't blank.
1 Like
Great thanks, will try the whole script later when we are closed!