Help with script to create printers on unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with script to create printers on unix
# 1  
Old 05-07-2010
Help with script to create printers on unix

Herwith is script thus far, i need to delete the printers within my function, how can i achieve this
Code:
#!/bin/ksh
BIN=/usr/lbin
LOCAL_FILE=`$BIN/lpstat -v >> sun5-printers.txt
REMOTE_FILE=`rsh sun8 /usr/lbin/lpstat -v >> sun8-printers.txt
awk 'BEGIN {while ( getline < "sun5-printers.txt") {arr[$0]++ }}{ if (!($0
 in arr ) ) {print} }' sun8-printers.txt
#Amend,create or delete Printer
prExists=$?
function printerExists()
{
prName=" "
prAddress=" "
prLocation=" "
prdescrip=" "
prfilter=" "
prport=" "
prdriver=" "
   if [ $prExists -eq 1 ]; then
        echo "\n\t Printer already exists. "
   else
        echo
        $BIN/lpadmin -x $Prname
        echo "\n\t Deleting printer $Prname !!!"
   fi
  # Add Printer Command 
  lpadmin -p "${prName}" -E -D "${prDescript}" -L "${prLocation}" -i /u1/cups/mac/"$prFilter_filter -v \
  "$Prdriver"://"${prAddress}":"Prport"
fi

any help will be highly appreciatedSmilie

---------- Post updated at 11:07 ---------- Previous update was at 07:38 ----------

everyone is viewing..but no help forth-coming...
# 2  
Old 05-07-2010
I am not totally sure what you want to do and maybe othere people did not quite understand it neither so that could be the reason because so many look in but do not answer.

Whatever, there are some lines, I see no sense in:
Code:
awk 'BEGIN {while ( getline < "sun5-printers.txt") {arr[$0]++ }}{ if (!($0
 in arr ) ) {print} }' sun8-printers.txt
#Amend,create or delete Printer
prExists=$?

You compare 2 files to get the difference and getting the difference, you do nothing with that list. The output is not used any further and also the $? is not worth anything since awk will nearly always give you a 0 back, no matter if there is a difference or not.
I recommend you do a simple diff instead and check it for it's $? to notice if there is a difference or not. If you really need to process the difference list, you might use a simple grep -f against those 2 files so you can a) check it's result with $? and b) if you need the output, process/parse it.

Also your function printerExists() is not closed with a } which will produce an error, maybe just a typo.
So far I got the impression you want to create those print queues that do not exist on some hosts yet.
Then you might want to write your printerExists() so that it can parse a) a printer name, that you might already have gotten by checking which do not exist, b) an action like [add|delete].
Also all those variable inside the function have not been filled with usable values, which could have happened outside this function or in another function so you can hand them over to printerExists() as parameters too.

Maybe get a clear picture of what you want to do 1st and write the script according to that and/or tell what exactly is not working currently.
By taking a glimpse at it there should be several things that might not work, like stated above.
# 3  
Old 05-07-2010
The awk was too compare the two files from a local server and a remote server.Then i added this to get the printer names the two files
Code:
while read printer
do
printer="$(echo $printer | cut -d: -f1)"
echo $printer
done < diffs.txt
exit 0

The function was an example, i wanted to use in order to delete,amend or recreate the printers in that order, which i found when doing some research.

I am still stuck with the function to the above
Yes it is true that i want to delete,amend or recreate a printer that is not on the server.

The function I know its not correct as i am stil very much a junior when it comes to shell scripting, functions etc
But i do know the lpadmin commands i will use to create the printers
Code:
$BIN/lpadmin -x $Printer
$BIN/lpadmin -p "${prName}" -E -D "${prDescript}" -L "${prLocation}" -i /u1/cups/mac/"$prFilter_filter -v \
     "$Prdriver"://"${prAddress}":"Prport"

Now too figure out, how to set them in a function like you suggested.Smilie

I do not not know how parse the variable in a function?????
# 4  
Old 05-07-2010
I agree, the script fragment contains too many syntax and apparent logic errors to follow what the O/P intended.

The (apparently pointless) function starts but does not end. There is no closing } . There is however a spurious "fi" on the last line.
Code:
function printerExists()
{

The function itself is never called and I can't see a place where a list of printers is processed.

On the logic front the script appears to be intended to delete all printers on the home machine and create all printers to match the remote computer. Was this the intention?

On the command logic don't you have to issue the sequence: lpshut (and wait until it stops), lpadmin -x, lpsched when deleting a printer?


An example of the printer lists from each computer showing which ones should be deleted and which ones should be created would be useful.
# 5  
Old 05-07-2010
No i don't have to do a lpshut, as we use the cups software...
The diffs.txt file displays the printers i need to amend.
Code:
device for 0117bd1: lpd://172.25.xx.xx:515
device for 2201bl7: socket://172.25.xx.xx:9100

in this instance the printers differ by the IP address, that will be the reason to delete then recreate it.
# 6  
Old 05-07-2010
Ok here a basic snippet which you might just want to adopt into your script, supposing you already have the name of the printer to be created, deleted or altered etc.:

Code:
...
LOCAL_FILE=file1
REMOTE_FILE=file2

# Not sure which you want to add and which you want to delete, but you can pick it out by this I think:

ADD_QUEUE=`grep -f file1 file2`
DEL_QUEUE=`grep -vf file1 file2`
...

do_queue()
{
     # $1 of this function is the queue name
     # $2 the type of action
     case $2 in
          "create")     here comes your command to create $1;;
          "delete")     here comes your comand to delete $1;;
     esac

     #those commands could be place in another separate function themselves which could be called with parameters of course too, if you like
}

if [[ -n $ADD_QUEUE ]]; then
     # first comes the function name, and then it's parameters
     do_queue $ADD_QUEUE create
fi

if [[ -n $DEL_QUEUE ]]; then
     # first comes the function name, and then it's parameters
     do_queue $DEL_QUEUE delete
fi
...

# 7  
Old 05-07-2010
herewith is the current code:
Code:
#!/bin/ksh
BIN=/usr/lbin/
LOCAL_FILE=`$BIN/lpstat -v >> sun5-printers.txt
REMOTE_FILE=`rsh sun8 /usr/lbin/lpstat -v >> sun8-printers.txt
DIFF_FILE=/usr/local/bin/diff.txt
awk 'BEGIN {while ( getline < "sun5-printers.txt") {arr[$0]++ }}{ if (!($0
 in arr ) ) {print} }' sun8-printers.txt >> diff.txt
while read printer
do
printer="$(echo $printer | cut -d: -f1)"
echo $printer
done < diffs.txt
exit 0
#Amend,create or delete Printer
f_Delete()
{
 if [ $printer -eq 1 ]; then
      echo "\n\t printer to be deleted"
      else 
      $BIN/lpadmin -x $Printer
      echo "\n\t Deleting printer $Printer !!!"
 fi
}
f_Create()
{
  if [ $printer -eq 0 ]; then
  echo "Enter Printer Name: \c"
  read printer
  echo "Enter IP Address  : \c"
  read ip
  echo "Enter Location    : \c"
  read loc
  echo "Enter Description : \c"
  read desc
  echo "Enter Filter      : \c"
  read filt
  echo "Enter Port        : \c"
  read port
  echo "Enter Driver      : \c"
  read driv
  echo "\n\t Printer does not exists. "
  else 
  $BIN/lpadmin -p "$Printer" -E -D "$Desc" -L "$Loc" -i /u1/cups/mac/"$Filt"_filter -v \
  "$driv"://"$IP":"Port"
  echo "\n\t Printer $printer created"
 fi
}

what about debugging????Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create a UNIX script file with multiple commands

Hi Good morning all, I want to create script file with multiple commands. For ex: pmrep connect is one of the command to connect to repository. pmrep objectexport is another command to export objects to a file. These commands should run sequentially.But when i try to execute this, the first... (4 Replies)
Discussion started by: SekhaReddy
4 Replies

2. Shell Programming and Scripting

Need a script to add mutiple printers in solaris box

Hi , i need to configure around 80 printers in a server. can someone please help me with the script. i have a file that has printer name and its ip. like. printer1 1.1.1.1 printer2 0.0.0.0 and so on.. can some one please help me to do it via script. i am using solaris 10 ... (0 Replies)
Discussion started by: chidori
0 Replies

3. UNIX for Dummies Questions & Answers

Unix script to create a backup on a remote computer

Hello, I have a problem - I attended a UNIX course a couple of years back but, unfortunately, I don't remember how to write scripts for shell commands. Now I want to make a script that makes a backup of a folder on a remote computer and I have no idea how to begin. :D So the idea is that I want... (3 Replies)
Discussion started by: Benedit
3 Replies

4. AIX

AIX Bunch of printers queue creation script - HELP

I'd seek for help on how to create a bunch of printers in AIX 6.x or equal or above in one go – say like I have 35 printers to create in 4 different AIX Nodes every month – I currently create it manually like below:- How can I automatic this creation on all the 4-5 Nodes – not actually automatic... (3 Replies)
Discussion started by: shiv2001in
3 Replies

5. Shell Programming and Scripting

How to create a file with full permission in unix script

In my script, I am creating a file ----> then writting one line (i.e. Timestamp) ----> then FTP'ing. The same script can be executed by many other users. While other users executing this script, they couldn't Over write this one line (i.e. Timestamp) My expectation So I wanted to create a... (2 Replies)
Discussion started by: sbmk_design
2 Replies

6. Shell Programming and Scripting

How to create xls file using unix script

Hi All, I want to create an xls file using shell script. I mean that I have written one script. It is having sql output. So I want to save that sql output into one xls file and I want to send that file as a attachment to my team MAIL_IDs. Can You any guy help me in this? Thanks in... (3 Replies)
Discussion started by: raghu.iv85
3 Replies

7. Shell Programming and Scripting

Script for reporting network printers

Does anyone have a script that would detect (and create a flat-file) of all printing devices (printers/plotters) attached on a network? I need a single script or tool that would allow me to collect a data file for our asset management tool. Example format: manufacturer, model name, serial... (0 Replies)
Discussion started by: glefko
0 Replies

8. UNIX for Advanced & Expert Users

Password protect UNIX printers?

Is there a way to password protect a printer that is on a LAN network? Our security officer said our UNIX printers need to be password protected. Is this possible? I am running Solaris 7 and 8... Thanks in advanced for any suggestions. (4 Replies)
Discussion started by: rtoba
4 Replies

9. Shell Programming and Scripting

How To create Flat Files using Unix Shell Script?

Hi all, How to create Flat Files using Unix Shell Script. The Script is supposed to be sheduled to run at a particular time? Thanks in advance Appu (4 Replies)
Discussion started by: Aparna_k82
4 Replies

10. UNIX for Dummies Questions & Answers

quering unix boxes for attaced printers

Greetings All, I was wanting some info seeing i am a ms geek looking to go into UNIX if i want to query a UNIX box for info regarding printers, LPDs and such source how would i go about this !!!! All comments that would point me in the right direction would be most useful. Cheers (1 Reply)
Discussion started by: seekerO
1 Replies
Login or Register to Ask a Question