[SOLVED] "lp" Command to Print Out a text file and a PDF File
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
"lp" Command to Print Out a text file and a PDF File
Hi,
[1] The manual page of "lp" command does talk about an option called "-U" to specify a user name along with the command invocation. But there is apparently no separate option for supplying a password with. So I also tried: "-U username/password" and "-U username: password" - in vain.
Is there way to specify a username and a password to execute the "lp" command in a Shell Script (Bash)? Executing the command without the credentials does not prompt for any credentials and the job is held for authentication (can be noticed in the printer's status in GUI).
[2] One more thing. I am trying to print out 1st page of a PDF file. If I use "pdftops" then its metadata are output. This is not desirable. Whatever is on the 1st page of the specified PDF file must be printed out as it is - as it happens in the case of printing out from within a PDF viewer (evince, for example) in the GUI mode.
Yes, I have already done some homework and have not got any help by searching it on Google. So posting my questions here.
Please also note that I do not wish to store my credentials in the settings of the printer itself. Nor do I wish to keep my credentials static. If my script is going to be run by somebody else then I will read in the credentials and then pass them to the related options to be used for printing out.
I am using CentOS 6.9 and my Repo has Packages from DVD 1 and DVD 2.
Please also note:
My Linux OS is running on a VM (a VirtualBox instance) and is connected through a DHCP Server which is a Windows Server (possibly as I have set up the VM in my Lab for demo purposes and used Bridged connection). So, to connect to the printer, I have used Windows SAMBA Share. Whenever I give a print out, I get a prompt for entering my Windows AD (Active Directory) credentials and this works fine as long as I am printing out manually from the GUI / Desktop environment of my CentOS VM.
So, I have not had to setup the CUPS utility. Any user who is running the script that prints out a particular log file or a pdf file needs to enter his/her Windows AD credentials and this happens only if the script is executed from within the GUI mode. If run without entering the credentials, the print job is held for authentication. So, I need to pass the credentials from within the script itself. So, a user can hard-code his/her credentials on the system when wants to run the script. One can in fact use "expect" as well but the prompt is a GUI one and not a CLI based.
[1] The manual page of "lp" command does talk about an option called "-U" to specify a user name along with the command invocation. But there is apparently no separate option for supplying a password with. So I also tried: "-U username/password" and "-U username: password" - in vain.
Is there way to specify a username and a password to execute the "lp" command in a Shell Script (Bash)? Executing the command without the credentials does not prompt for any credentials and the job is held for authentication (can be noticed in the printer's status in GUI).
Yes, I have already done some homework and have not got any help by searching it on Google. So posting my questions here.
Please also note that I do not wish to store my credentials in the settings of the printer itself. Nor do I wish to keep my credentials static. If my script is going to be run by somebody else then I will read in the credentials and then pass them to the related options to be used for printing out.
It is easily possible to allow different users different permissions to print through cups policy. This allows you to print without a password, while being able to set that other users cannot print without a password.
It is easily possible to allow different users different permissions to print through cups policy. This allows you to print without a password, while being able to set that other users cannot print without a password.
Hope this works for you.
Apologies for not specifying in my original post this one key thing:
My Linux OS is running on a VM (a VirtualBox instance) and is connected through a DHCP Server which is a Windows Server (possibly as I have set up the VM in my Lab for demo purposes and used Bridged connection). So, to connect to the printer, I have used Windows SAMBA Share. Whenever I give a print out, I get a prompt for entering my Windows AD (Active Directory) credentials and this works fine as long as I printing out manually from the GUI / Desktop environment of my CentOS VM.
So, I have not had to setup the CUPS utility. Any user who is running the script that prints out a particular log file or a pdf file needs to enter his/her Windows AD credentials and this happens only if the script is executed from within the GUI mode. If run without entering the credentials, the print job is held for authentication. So, I need to pass the credentials from within the script itself. So, a user can hard-code the credentials on his system when wants to run the script. One can in fact use "expect" as well but the prompt is a GUI one and not a CLI based.
[1] The manual page of "lp" command does talk about an option called "-U" to specify a user name along with the command invocation. But there is apparently no separate option for supplying a password with. So I also tried: "-U username/password" and "-U username: password" - in vain.
Is there way to specify a username and a password to execute the "lp" command in a Shell Script (Bash)? Executing the command without the credentials does not prompt for any credentials and the job is held for authentication (can be noticed in the printer's status in GUI).
Yes, sudo will let you do this, as its sole purpose is to allow a user to execute things as another user, and not just root. Read the man page on sudo, paying particular attention to the "-u" flag.
Quote:
[2] One more thing. I am trying to print out 1st page of a PDF file. If I use "pdftops" then its metadata are output. This is not desirable. Whatever is on the 1st page of the specified PDF file must be printed out as it is - as it happens in the case of printing out from within a PDF viewer (evince, for example) in the GUI mode.
Yes, I have already done some homework and have not got any help by searching it on Google. So posting my questions here.
....a how-to, containing MANY options, including the qpdf program stated here. And you say you looked on Google first????
Quote:
Please also note that I do not wish to store my credentials in the settings of the printer itself. Nor do I wish to keep my credentials static. If my script is going to be run by somebody else then I will read in the credentials and then pass them to the related options to be used for printing out.I am using CentOS 6.9 and my Repo has Packages from DVD 1 and DVD 2.
Once you use sudo or su correctly in your script (and if you write your script correctly), it should prompt the user, and they can input their own credentials.
....a how-to, containing MANY options, including the qpdf program stated here. And you say you looked on Google first????
Yes, I did see that page as well before posting my question here. They are talking about "extracting pages from a pdf file" adding an extra step to the job of printing out a specific number of pages only. As suggested here, I have installed "qpdf" which may do the job fine. But this package not being part of a standard installation and not even being shipped with the CentOS 6.9 DVD 1 and 2, I cannot presume it be sitting on another similar Linux machine setup. As we do our jobs, we do them under some security guidelines and have to follow certain policies. I did not wish to describe all those things in my original question. I welcome anything new but I have to do what is allowed in the process/system. I hope you understand the limitations thrown by the system/process created by stupid managers who are not techies. One such instance is: If it is not on the installation media, don't download it from the Internet.
We can drop this PDF part from our discussion for the time being. Now the question remains: How to read credentials from a user and pass those credentials to "lp" or "lpr" command to print out a specific log file that could be a text file or a pdf file? As the "lp" and "lpr" commands are shipped with the installation media, we can use them at will and modify their behaviour if needed. Please note that the credentials are required as the printer has been added as a Windows SAMBA Share. (I have updated my post to include this fact I had missed while posting the question.)
Yes, I did see that page as well before posting my question here. They are talking about "extracting pages from a pdf file" adding an extra step to the job of printing out a specific number of pages only. As suggested here, I have installed "qpdf" which may do the job fine.
You do realize that you can extract the page from the PDF, and pipe it to lp to print, right?? Same as you would to pipe something into grep or any other Linux command.
Quote:
But this package not being part of a standard installation and not even being shipped with the CentOS 6.9 DVD 1 and 2, I cannot presume it be sitting on another similar Linux machine setup. As we do our jobs, we do them under some security guidelines and have to follow certain policies. I did not wish to describe all those things in my original question. I welcome anything new but I have to do what is allowed in the process/system. I hope you understand the limitations thrown by the system/process created by stupid managers who are not techies. One such instance is: If it is not on the installation media, don't download it from the Internet.
So then why/how did you install it? Because if it was on the install media, then there's no problem in listing it in the RPM file you build to deploy your scripts, as a prerequisite, which will then install qpdf as part of the 'secure' installation. Since you say this is the environment you're in, and you're a developer with years of experience, packaging your script(s) into an RPM file would seem to make the most sense for what you describe.
If it wasn't on the install media, then your solution of qpdf won't work anywhere in your 'secure' environment.
Quote:
We can drop this PDF part from our discussion for the time being. Now the question remains: How to read credentials from a user and pass those credentials to "lp" or "lpr" command to print out a specific log file that could be a text file or a pdf file? As the "lp" and "lpr" commands are shipped with the installation media, we can use them at will and modify their behaviour if needed. Please note that the credentials are required as the printer has been added as a Windows SAMBA Share. (I have updated my post to include this fact I had missed while posting the question.)
There are thousands of bash-scripting examples that show you how to prompt a user for input.
There are thousands of bash-scripting examples that show you how to prompt a user for input.
Code:
read -p "Windows AD Login Name: " ad_login_name
read -sp "Windows AD Password: " ad_loging_password
That is not what I meant. There is only "-U" available with the "lp" command that says specify a user name. So, how to specify a password along with the user name?
So that I can use it like this:
Code:
lpr .... -U "$ad_login_name" ....
Yes, I tried out the "qpdf" package on my local test VM.
From your experience of "lp" and "lpr", can you tell if there is a way to specify a password along with a user name when those commands are executed from the command line?
Talking about my experience, I can do my job with a ghost script to extract pages from a PDF, so that is not a problem. The only challenge is I have been asked to use only "lp" or "lpr" command to submit a print job successfully. In fact, the manager in the question is sticking to "lpr" as he is using CygWin that I have not ever used and "lp" command is missing in his environment.
If my query sounds too much vague still, please let me know and I will close it myself.
read -p "Windows AD Login Name: " ad_login_name
read -sp "Windows AD Password: " ad_loging_password
That is not what I meant. There is only "-U" available with the "lp" command that says specify a user name. So, how to specify a password along with the user name? So that I can use it like this:
Code:
lpr .... -U "$ad_login_name" ....
You PROMPT THE USER FOR IT, like you said you were going to....that's how. Or read it from an environment variable from their .bashrc/.profile. But you said from the beginning that you were going to ask the user for in put, so where is the problem on passing that as a variable somewhere else, to some other command?
Quote:
Yes, I tried out the "qpdf" package on my local test VM.
Great.
Quote:
From your experience of "lp" and "lpr", can you tell if there is a way to specify a password along with a user name when those commands are executed from the command line?
Yes...as said numerous times, and as you said you were GOING TO DO: prompt the user to enter them.
Quote:
Talking about my experience, I can do my job with a ghost script to extract pages from a PDF, so that is not a problem. The only challenge is I have been asked to use only "lp" or "lpr" command to submit a print job successfully. In fact, the manager in the question is sticking to "lpr" as he is using CygWin that I have not ever used and "lp" command is missing in his environment. If my query sounds too much vague still, please let me know and I will close it myself.
Can't give you a more clear answer than what you got.
An example of "lp" or "lpr" command in which you pass user name and password from the command line itself?
Here is an example:
lp readme.txt -P 1-0
That will print the first 10 pages of the said file. Please extend that command to include a user name and a password, if it is possible with "lp" or "lpr".
An example of "lp" or "lpr" command in which you pass user name and password from the command line itself?
Here is an example:
lp readme.txt -P 1-0
That will print the first 10 pages of the said file. Please extend that command to include a user name and a password, if it is possible with "lp" or "lpr".
Very possible, and the man page for lp can get you started, and you can also find several examples on the CUPS site, about setting a CUPS_USER environment variable (which can be done via script, as you know), or having CUPS prompt you for a user id/password. Again, the CUPS configuration documentation can tell you how.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.