LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 09-09-2009, 06:56 AM   #16
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208

As your output shows, the error comes on the second pass through the loop when N has been changed by the assignment N=$[N+1] which sets its value to 1 (I don't know why, but it does). $IP is not an array but may be referenced as an array (I did not know that was possible). On the second pass through the loop the smbclient command includes -L ${IP[$N]}. The value of ${IP[$N]} is unset and so smbclient is passed -L without a following IP address. If set -o nounset had not been commented out I think it would have raised an error which would have helped track down the bug.

Given that $IP and $SERVER are not arrays, the script would be clearer like this (the indentation has been adjusted to make it easier to see what's going on)
Code:
#!/bin/bash
set -o nounset
echo "This script will search out shared files and printers on a network."
echo " "
echo "The mountpoint for shared files will be in /home/server/"
echo "An entry will be added to fstab to mount the shared files using cfs"
echo " "
echo "Shared printers will be added to cups via lpadmin. Cups will be given the same name as the shared printers."
echo "Do you wish to procede Y or N" ; read X

case $X in
    y|Y|Yes|YES)
	;;
    *)
        exit 0
	;;
esac

shopt -s extglob # Turn on extended globbing, required in the parameter expansions below
NMBSCAN=$(/usr/bin/nmbscan -a)		#Scan available ipaddresses and server names on a lan and put it into NMBSCAN
buf="${NMBSCAN#*server }" 	#buf strips "server" and only stores info after server.
SERVER="${buf%%+([$IFS])*}"
buf="${buf#*ip-address }"
IP="${buf%%+([$IFS])*}"

cmd="smbclient -L $IP -A /root/.smbpass"
SMBZ="$($cmd 2>1)"
rc=$?
if [[ $rc -ne 0 ]]; then
    echo "ERROR: exit status $rc from $cmd. Output was:
$SMBZ"
    exit 1
fi

FILES=( $(echo "$SMBZ" | grep -v '.*\$' |  awk '{if ($2 ~ /Disk/) print $1}') )
old_IFS="$IFS"
IFS='
'
for LIST in ${FILES[@]}
do
    IFS="$old_IFS"  # Restore now to avoid any unwanted side-effects
    if [ ! -d /home/server/$numb/$FILES ]; then
        # @@@ Why make a different directory than the one tested for?
        echo "mkdir -p /home/server/$SERVER/$LIST"
    fi
    if [ "$(cat /etc/fstab | grep '$SERVER/$LIST')" != "$SERVER/$LIST" ] ; then
        #Add cfs to fstab
        #need to add >> /etc/fstab to make official
        echo "//$IP/$LIST /home/server/$SERVER/$LIST cifs credentials=/etc/.smbpass,_netdev,uid=1000,gid=100 0 0"
    fi
done 

##########Network printer setup for cups	
PRINTERZ=( $(echo "$SMBZ" |  awk '{if ($2 ~ /Printer/) print $1}') )
for PRINTZ in ${PRINTERZ[@]}
do    #this is another way to do it.
    echo "lpadmin -p $PRINTZ -v smb://$SERVER/$PRINTZ"
    echo "lpadmin -d $PRINTZ"
done
 	
exit 0
Not tested! I might have garbled your script while editing it to change the indentation.
 
Old 09-09-2009, 04:53 PM   #17
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Original Poster
Rep: Reputation: 38
Having only briefly viewed your script, I wanted to point out that I have five computers on the network. At the time I ran the script, there was only one other computer running. When the other computers are running, the nmbscan will print additional ip addsresses and server names. Thus the need for an array.


Code:
buf="${NMBSCAN#*server }" 	#buf strips "server" and only stores info after server.
SERVER="${buf%%+([$IFS])*}"
buf="${buf#*ip-address }"
IP="${buf%%+([$IFS])*}"
I assumed that the code above will enable more then one entry thus the array.

Here is another example of nmbscan -a
Code:
dp@bt:/home/dp/down$ nmbscan -a
nmbscan version 1.2.5 - bt - Wed Sep  9 14:40:44 PDT 2009
domain MSHOME
  master-browser LEN 192.168.1.19 -
  server KIDS
    ip-address 192.168.1.20
      mac-address 
    server-software Windows 2000 LAN Manager
    operating-system Windows 5.1
  server LEN
    ip-address 192.168.1.19
      mac-address 
    server-software Windows 2000 LAN Manager
    operating-system Windows 5.1
Having run the script.
I guess I have run into another problem.
Perhaps you are correct the code below will not allow an array. It only uses one set of ip addresses and computer names. Thus bringing me back to an earlier version of the script.

Code:
buf="${NMBSCAN#*server }" 	#buf strips "server" and only stores info after server.
SERVER="${buf%%+([$IFS])*}"
buf="${buf#*ip-address }"
IP="${buf%%+([$IFS])*}"



EDIT: A closer look at your version.
Code:
cmd="smbclient -L $IP -A /root/.smbpass"
This will only allow for one ip address.

Code:
 IFS="$old_IFS"  # Restore now to avoid any unwanted side-effects
    if [ ! -d /home/server/$numb/$FILES ]; then
        # @@@ Why make a different directory than the one tested for?
        echo "mkdir -p /home/server/$SERVER/$LIST"
Should be
Quote:
IFS="$old_IFS" # Restore now to avoid any unwanted side-effects
if [ ! -d /home/server/$SERVER/$LIST ]; then
# @@@ Why make a different directory than the one tested for?
echo "mkdir -p /home/server/$SERVER/$LIST"
Probably my mistake.

Last edited by okos; 09-09-2009 at 05:09 PM. Reason: A closer look at your edit.
 
Old 09-23-2009, 11:08 AM   #18
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Just seen your last post and finally understand why $IP and $SERVER are arrays. Have you got the script working OK now?

EDIT: and working OK when there are 0, 1 and several computers on the network?

Last edited by catkin; 09-23-2009 at 11:10 AM.
 
Old 10-12-2009, 09:13 PM   #19
okos
Member
 
Registered: May 2007
Location: California
Distribution: Slackware/Ubuntu
Posts: 609

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by catkin View Post
Just seen your last post and finally understand why $IP and $SERVER are arrays. Have you got the script working OK now?

EDIT: and working OK when there are 0, 1 and several computers on the network?
I greatly appreciated your help.
I bought a new computer and had not had a chance to work on the script. I wanted to get everything setup for my business before I started editing the script again.

There are currently 5 computers on my network.

I am not only writing the script for the learning experience, but to expedite things in the future.

This script would make it easy to add a new computer to the network with file sharing and network printer setup.
I do not know of such a program currently available for download.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SMBClient Error "NT_STATUS_DUPLICATE_NAME" yah0m Linux - Networking 1 07-21-2008 07:12 PM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM
smbclient error message xgreen Slackware 1 04-13-2005 03:41 AM
smbclient "Error returning browse list: NT_STATUS_OK" b0nes Linux - Networking 2 02-18-2005 07:13 AM
smbclient: ERROR: Could not determine network interfaces uselpa Slackware 1 12-10-2004 12:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 02:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration