Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Powershell: Script to retrieve path of file given the filename on a remote unix server

93 views
Skip to first unread message

roh

unread,
Nov 7, 2009, 4:50:08 PM11/7/09
to
Hi All, (Powershell n00b here!)
I need to write a script which can run on windows (hence considering Powershell) which can log into a remote unix server and retrieve full path of file given the filename. Can this be done using Powershell? If not can i do this using any other tools on windows? I am considering using plink w/ powershell but cant find enough examples? If possible please provide with ps plink command statement.


Thanks
Roh

EggHeadCafe - Software Developer Portal of Choice
Professional ASP.NET Web Services [Wrox]
http://www.eggheadcafe.com/tutorials/aspnet/3ef0c5cd-a221-4376-a2a0-dfe7948c05a1/professional-aspnet-web.aspx

Al Fansome

unread,
Nov 7, 2009, 5:13:39 PM11/7/09
to
Python will do this, using the standard Python telnetlib. It lets you
telnet into a system, such as UNIX, and feed the shell there command
lines. I have been using Python 2.6.2 under Windows XP and PowerShell 1
& 2 for some time now to remotely access a large number of remote POSIX
(QNX) systems, and they complement each other nicely.

If you are looking for an executable under a Unix/Posix-like system shell

which filename

or

whence filename

may work for you, if it is in your search path.

If it is just a random file

find / -name "filename"

will search the entire directory tree for the file.

Al Fansome

unread,
Nov 7, 2009, 5:36:01 PM11/7/09
to
You will probably have to tweak this for your version of Unix, such as,
what exactly the login and password prompts look like.

import sys
import telnetlib

# make the connection, with possible timeout

try:
tn = telnetlib.Telnet()
tn.open("10.4.1.54", 23)
except:
print "telnetlib.Telnet error:", sys.exc_info()[0], sys.exc_info()[1]
sys.exit(1)

# enter username and password at prompts

tn.read_until("login: ")
tn.write("al\n")
tn.read_until("Password:")
tn.write("fansome\n")

# send the command file to the session

tn.write("which time\n")

# end the session by exiting the shell

tn.write("exit\n")

# read back the output from the session

try:
output = tn.read_all()
except:
print "tn.read_all error:", sys.exc_info()[0], sys.exc_info()[1]
tn.close()
sys.exit(0)

print output

Joe Morris

unread,
Nov 7, 2009, 11:05:37 PM11/7/09
to
<roh> wrote:

> I need to write a script which can run on windows (hence considering
> Powershell) which can log into a remote unix server and retrieve full path
> of file given the filename. Can this be done using Powershell? If not can
> i do this using any other tools on windows? I am considering using plink
> w/ powershell but cant find enough examples? If possible please provide
> with ps plink command statement.


I won't try to answer your question since I'm not that experienced with PS
and there are others here who are far more qualified to answer it than I am.

However, I do have one comment to make that's not related only to
Powershell: assuming that you're doing this for your employer and not as a
personal toy, what plans do you have for security? You need to consider not
only the obvious one of having to somehow secure the script if it contains a
hardcoded userid and password, but also the process for periodic changes to
the password (and coordinating the changes on the remote host with changes
to the script). You also should consider using something more secure that
plain-vanilla telnet, which sends everything (including the login userid and
password) across the network in cleartext.


Joe Morris


roh

unread,
Nov 8, 2009, 1:47:04 PM11/8/09
to
Security is a major concern , which is I would refrain from using telnet and would consider ssh.
Also I would like to run this from windows. i ll see if Python supports ssh. Thanks.

Al Fansome wrote:

Python will do this, using the standard Python telnetlib.

07-Nov-09

Python will do this, using the standard Python telnetlib. It lets you
telnet into a system, such as UNIX, and feed the shell there command
lines. I have been using Python 2.6.2 under Windows XP and PowerShell 1
& 2 for some time now to remotely access a large number of remote POSIX
(QNX) systems, and they complement each other nicely.

If you are looking for an executable under a Unix/Posix-like system shell

which filename

or

whence filename

may work for you, if it is in your search path.

If it is just a random file

find / -name "filename"

will search the entire directory tree for the file.

roh wrote:

Previous Posts In This Thread:

EggHeadCafe - Software Developer Portal of Choice

Confirm Message Box before Deleting
http://www.eggheadcafe.com/tutorials/aspnet/4f445b0a-bb25-4030-8f9b-0ff1ac5655d4/confirm-message-box-befor.aspx

Martin Zugec

unread,
Nov 8, 2009, 2:55:03 PM11/8/09
to

Not my cup of tea, but wouldn't Services for NFS help?

"roh" wrote in message news:200911813473...@gmail.com...

tojo2000

unread,
Nov 8, 2009, 6:27:26 PM11/8/09
to
> Confirm Message Box before Deletinghttp://www.eggheadcafe.com/tutorials/aspnet/4f445b0a-bb25-4030-8f9b-0...

I've used this before, and it works: http://huddledmasses.org/scriptable-ssh-from-powershell/

Herb Martin

unread,
Feb 4, 2010, 4:23:48 PM2/4/10
to

<roh> wrote in message news:200911716502...@gmail.com...

> Hi All, (Powershell n00b here!)
> I need to write a script which can run on windows (hence considering
> Powershell) >which can log into a remote unix server and retrieve full
> path of file given the >filename. Can this be done using Powershell? If
> not can i do this using any other tools >on windows? I am considering
> using plink w/ powershell but cant find enough >examples? If possible
> please provide with ps plink command statement.

Log into how? Telnet, FTP...something else?

Likely you can do this with Powershell but you are going
to need the specific service/server to emulate such a client
in Powershell, or run some external program that does this
like nc.exe (available on the Internet) to actually converse
with the service on Unix, do the directory listing (or whatever)
and return the answers to Powershell for parsing or
processing.


0 new messages