Reading netstat Output to Install Printer - port

Long story behind it, but here's what I'm trying to do:
I am working on a remote virtual machine implementation, where depending on the client device's location, the appropriate network printer will be installed via batch file (no VBS or PowerShell).
So, my idea is this:
Run netstat -an -p tcp to find the line containing port 49404.
Filter that output to grab the second IP address that will be
returned
Replace last octet of that IP with "250" (the printer IP
for each network)
Run nslookup on newly calculated IP to obtain
the name of that printer
Install printer by name.
Here's what I do have so far, pieced together from older posts around the web (I haven't gotten to steps 4 or 5 yet):
#echo off
netstat -p tcp -an | FIND "49404" > %temp%\TEMPIP.txt
FOR /F "tokens=2 delims=:" %%a in (%temp%\TEMPIP.txt) do set IP=%%a
del %temp%\TEMPIP.txt
set IP=%IP:~9%
set "ip=%IP%"
for /f "tokens=1-4 delims=. " %%a in ("%ip%") do (
set octetA=%%a
set octetB=%%b
set octetC=%%c
set octetD=232
)
I'm sure there are cleaner or more efficient ways to perform this task, so I'm hoping you all can point me in the right direction. Thanks!

Related

How can i know who is using a port?

When I browse "localhost:8080" it ask me to authenticate, but I do not know which program is using that port.
My question is: how can I know which program is using that port?
You could use the following two methods, depending on your OS :)
Windows
Open the command prompt - start » run » cmd or start » All Programs »
Accessories » Command Prompt.
Type netstat -aon | findstr '[8080]'
If the port is being used by any application, then that application’s
detail will be shown. The number, which is shown at the last column
of the list, is the PID (process ID) of that application. Make note
of this.
Type tasklist | findstr '[PID]'. Replace the [PID] with the number
from the above step and hit enter.
You will then be shown the application name that is using your port
number
Linux (Debian/Ubuntu)
open the terminal and enter sudo apt install net-tools
open the terminal and enter netstat -ltnp | grep -w ':8080'
You should then see the application using said port on the far right
of the terminal [PID]/SomeApplicationName
https://www.tecmint.com/10-lsof-command-examples-in-linux/
lsof: is the command you need

socat struggle to create serial ports

For testing purposes I want to use socat to create virtual serial ports to use in my Python program.
I have limited success, but struggle again and again with the many options in socat. I use this command in Ubuntu Linux:
sudo socat -d -d pty,b9600,raw,echo=0,link=/dev/ttyS90 pty,b9600,raw,echo=0,link=/dev/ttyS91
As it should, it creates the virtual ports like /dev/pts/2 and 4, and links them to /dev/ttyS90 and *91. It does not work without sudo (it fails with unable to unlink for the *90, *91 ports, although the regular user is in the dialout group).
But as you see the permissions 'lrwxrwxrwx' look like reading/writing for everybody. However, this is NOT true: I CANNOT use these devices unless I am root. The file manager (=Nemo) gives this result:
The permissions are significantly different. Huh?
After issuing 'sudo chmod 777 /dev/ttyS90' (and same for *91) nothing changes in the terminal output, because it is already, but incorrectly, showing 777 permissions, but the Nemo output changes to
And now I can use the ports as regular user! How comes? Am I doing something wrong?
And one more socat problem: the above socat command gives an 8-bit, no-parity connection, but I really need a 7-bit, even-parity connection. My attempts to implement this by juggling some of the many options all failed. I am lost; any insight?
Try changing the permission on /dev/pts/2 and /dev/pts/4 instead of on the link

make an lp script into functioning printer

I'm trying to print to separate printers simultaneously in Ubuntu 14.04
From all of my reading the best option I've seen is to write a script
that sends an lp command to the separate printers.
This is the script I've written so far
!/bin/bash
lp -d printer "$#"
lp -d printer2 "$#"
where printer and printer 2 are the actual printers installed on the system
This script works from a terminal, however I would like to be able to send print jobs directly to a "printer" that is actually the script I've written.
How can I make this lp script into a "printer"
Ok I didn't find a way to do this the way I originally intended, however it is possible with tea4CUPS
great cups backend tool with an easy config file
http://www.pykota.com/software/tea4cups/download
The install instructions are on the download page.
As for printing to multiple printers, add this command in the config file for every printer you wish to print to.
prehook_firstprinter: /usr/bin/lp -d Name of Printer -o raw $TEADATAFILE
Here are the simplest instructions I could write
1. download the tea4cups.gz
Extract it to the home folder, rename it to tea4cups
Open a terminal and run these commands
sudo cp /home/manifester/tea4cups/tea4cups.conf /etc/cups
sudo cp /home/manifester/tea4cups/tea4cups /usr/lib/cups/backend/
sudo chmod 700 /usr/lib/cups/backend/tea4cups
Run this command
sudo gedit /etc/cups/tea4cups.conf
Paste this in the bottom of the document
prehook_firstprinter: /usr/bin/lp -d Name of Printer yes the literal name in the printer window -o raw $TEADATAFILE
you will need a new line for every printer you have, so if you want to print to 3 printers you will need three of the above line each having the name of the printer it will be talking to.
save and close everything
open a terminal and run
sudo service cups restart
open a web browser and go to the browser cups controller
http://localhost:631/admin
go to Add Printer
you should see a printer named "tea4CUPSnothing"
If you don't see it go back and press "Find Printers"
it should be there
Change the info of the printer to "Print all" for all type fields"
Press Continue
The generic printer driver works because the printer doesn't actually exist.
Press Continue
Set Defaults
you should be done, go to your printer window on ubuntu and do a test print.

Script to add network printer to all users x64 bit print server

I use to use this script for Windows XP but since we're doing the switchover to 7 I tried to use it on the new images. It seems like it tries but it doesn't work. I have created a 64bit print server and these machines are 64bit which is the only difference. I read up on this and saw there was a GPO that needed to be set to allow this to work. Which was - Computer Configuration > Administrative Templates > Printers > Allow Print Spooler to accept client connections.
I have tried everything and can't get this to work, it doesn't give me an error or anything. It gives me the prompts for PC name and Printer name, then says 'Adding printer' from the echo command, and just sits there. I can run the command by itself and it doesn't work either....... Please help!
The main thing is that the printer needs to be added from a print server and to the computer for all users as their default.
#echo off
echo PC Name
set /p PC=
echo Printer Name
set /p PRINTER=
ECHO Adding Printer...
\\ghostserver\installs\pstools\psexec \\%PC% -n 3 cmd /c rundll32
printui.dll,PrintUIEntry /y /ga /c\\%PC% /n\\PRINTSERVER\%PRINTER%
ECHO Restarting Print Spooler...
start /wait sc \\%PC% stop spooler
start /wait sc \\%PC% start spooler
Do you want to install or map the printer from the network ?
First thing to try : map the printer manually on a 7 x64 client. If it fails, your problem is not the batch.
Also try the simple rundll32 printui.dll PrintUIEntry /in n\\PRINTSERVER\%PRINTER% with a non-admin non-elevated account on the client to validate the print server configuration.
Is the "Disallow installation of printer using kernel-mode drivers" GPO disabled ? (Have to be)
Check the firewall settings, UAC/elevation configuration, admin access. Run a gpupdate /force and restart the client.
Check the event log on both the client and print server for any errors.
With an admin account (both print server and client), try to push the installation from the print server.
Have you tried to force adding the provider ? /j "LanMan Print Services"
If you have 2008 servers or DCs, you can use Print Management or Group Policy Preferences to deploy printers (easier than bat+psexec+printui.dll).
If you really want to do it via login script, there are also a bunch of tools in Vista/7/8 for print management in %WINDIR%\System32\Printing_Admin_Scripts, like this one.
Side note : start /wait is inefficient since sc.exe doesn't wait any response of the service. So, if you stop and start without a pause, chances are the service will not be stopped before the restart and skip the second order. You have to simulate a pause (ping 127.0.0.1 -n 5 >nul 2>&1) between stop & start or use a safer script to check the state of the service.
Thank you so much! That fixed it.
Here's my add script:
#echo off
echo PC Name
set /p PC=
echo Printer Name
set /p PRINTER=
echo Adding Printer...
\\servername\installs\pstools\psexec -s -i -accepteula \\%pc% rundll32 printui.dll PrintUIEntry /in /y /ga /n\\PRINTSERVER\%PRINTER%
echo Restarting Print Spooler...
start sc \\%pc% stop spooler
pause
start sc \\%pc% start spooler
pause
Here's my remove script:
#echo off
echo PC Name
set /p PC=
echo Printer Name
set /p PRINTER=
echo Adding Printer...
\\servername\installs\pstools\psexec -s -i -accepteula \\%pc% rundll32 printui.dll PrintUIEntry /gd /n\\PRINTSERVER\%PRINTER%
echo Restarting Print Spooler...
start sc \\%pc% stop spooler
pause
start sc \\%pc% start spooler
pause

Rails port of testing environment

I'd like to test the HTTP API of our Rails app using Faraday and RSpec. Faraday needs the host url + port. Unfortunately the port of the testing environment does always change. How do I access the current port programmatically in the spec?
If using Capybara you can set the port in the spec_helper.rb like so:
Capybara.server_port = 1234
See also: https://github.com/jnicklas/capybara/pull/123
There's probably more than one way to do this, but this is working for me right now:
The command
port = `lsof -p #{Process.pid} -ai TCP -as TCP:LISTEN -Fn | grep ^n | cut -c 4- | uniq`.strip
Note that you'll have to do this at some point after the app has loaded - i.e., you can't use this in your environment.rb or application.rb file.
The explanation
Basically what this command does is as follows:
lsof is the Unix command for LiSt Open Files
-p #{Process.pid} limits it to the current process (i.e., your test web server instance)
-ai TCP limits it to "files" of type TCP (i.e., open TCP ports). (Note: the -a is to make it AND the search with the pid one - the default, using just -i would OR the search)
-as TCP:LISTEN limits to just TCP ports that are being listened on (as opposed to any open port - like your app's connection to Postgres for example)
-Fn tells it to only output the "name" column, which in this case will be the IP/port that is being listened on
The output of that part by itself will be something like this:
p12345
n*:5001
n*:5001
The first line, starting with p is the process ID. There's no way to suppress this.
The next 2 lines (not sure why it can output multiples, but we'll take care of it in a minute) are the "name" column (hence n), followed by the IP + port. In our case (and I imagine yours as well, in a test environment), the web server listens on all available local IPs, thus *. Then it tells us that the port is, in this case 5001.
Finally, we pipe it through...
* grep ^n to eliminate the first line (the process id)
* cut to say "cut from columns 4 on" - i.e., remove the n*: to return just the port, and
* uniq to just get the one instance
(It will also have a trailing newline, thus the strip call.)
The usage
In my case, I'm using this in my Cucumber env.rb thusly, to reconfigure the URL options for ActiveMailer so my email links get generated properly as working links in test:
port = lsof -p #{Process.pid} -ai TCP -as TCP:LISTEN -Fn | grep ^n | cut -c 4- | uniq.strip
MyApp::Application.configure do
config.action_mailer.default_url_options[:host] = "0.0.0.0:#{port}"
end
No doubt you could do the same thing in a helper/config for Rspec as well.

Resources