LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 02-13-2018, 05:10 AM   #1
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
"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.

Last edited by devUnix; 02-13-2018 at 09:07 AM.
 
Old 02-13-2018, 05:37 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,702
Blog Entries: 19

Rep: Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503
I think the command-line utility qpdf will extract specific pages from a pdf.
 
Old 02-13-2018, 05:57 AM   #3
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Originally Posted by hazel View Post
I think the command-line utility qpdf will extract specific pages from a pdf.

I do not see *qpdf* in the Packages directory of CentOS 6.9 DVD 1 and 2.
 
Old 02-13-2018, 06:05 AM   #4
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,702
Blog Entries: 19

Rep: Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503Reputation: 4503
Quote:
Originally Posted by devUnix View Post
I do not see *qpdf* in the Packages directory of CentOS 6.9 DVD 1 and 2.
No, it isn't there, but you can install it. Here's a link to an account by another Centos user.
 
Old 02-13-2018, 06:24 AM   #5
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
@hazel: I appreciate your help.


Yes, I have installed it manually.

Not sure what it is going on here...

My first page in the PDF file has a title of the document. That is it. But the qpdf command is giving this output...
Code:
[root@localhost ~]# qpdf --show-pages=1 manual.pdf
page 1: 114 0 R
  content:
    116 0 R
page 2: 125 0 R
  content:
    126 0 R
page 3: 140 0 R
  content:
    141 0 R
page 4: 300 0 R

---- output cut here ----
[root@localhost ~]#

Last edited by devUnix; 02-13-2018 at 06:25 AM.
 
Old 02-13-2018, 07:41 AM   #6
Mill J
Senior Member
 
Registered: Feb 2017
Location: @127.0.0.1
Distribution: Mint, Void, MX, Haiku, PMOS, Plasma Mobile, and many others
Posts: 1,258
Blog Entries: 2

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
Quote:
Originally Posted by devUnix View Post
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).

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.
You will find a lot of help here https://www.cups.org/documentation.html

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.
 
Old 02-13-2018, 09:04 AM   #7
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Originally Posted by Mill J View Post
You will find a lot of help here https://www.cups.org/documentation.html

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.
 
Old 02-13-2018, 09:10 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,801

Rep: Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002
Quote:
Originally Posted by devUnix View Post
[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.
Very first hit in Google for "how to print one page of a pdf in linux" is:
https://askubuntu.com/questions/2219...-part-of-a-pdf

....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.
 
Old 02-13-2018, 09:35 AM   #9
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Very first hit in Google for "how to print one page of a pdf in linux" is:
https://askubuntu.com/questions/2219...-part-of-a-pdf

....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.)
 
Old 02-13-2018, 10:05 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,801

Rep: Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002
Quote:
Originally Posted by devUnix View Post
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.

Last edited by TB0ne; 02-13-2018 at 10:10 AM.
 
Old 02-13-2018, 06:14 PM   #11
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Quote:
Originally Posted by TB0ne View Post

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.
 
Old 02-13-2018, 07:24 PM   #12
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
Marked the Post as Solved as it did not find a right place here.
 
Old 02-14-2018, 07:11 AM   #13
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,801

Rep: Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002
Quote:
Originally Posted by devUnix View Post
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" ....
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.
 
Old 02-15-2018, 02:51 AM   #14
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Original Poster
Rep: Reputation: 59
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".
 
Old 02-15-2018, 07:51 AM   #15
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,801

Rep: Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002Reputation: 8002
Quote:
Originally Posted by devUnix View Post
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.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can not print to DMP 15" x 6" paper from browser or pdf file nhal SUSE / openSUSE 0 05-15-2014 11:45 AM
Printing large file using 'lpr -P' command causes a "pause" before and during print. JimWallace Linux - Kernel 3 01-17-2012 10:32 AM
Modifying text file with "one command line" SED command... daleo Linux - Newbie 3 01-13-2012 05:32 AM
compare: cups-pdf vs. "print to file" SaintDanBert Linux - Desktop 4 08-15-2011 09:56 AM
bad: Want to insert a line into a text file using "sed" command eliote Linux - General 7 09-19-2010 02:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:43 PM.

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