LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-05-2009, 09:58 AM   #1
nano2
Member
 
Registered: May 2007
Posts: 100

Rep: Reputation: 15
using telnet to connect to windows server inside a shell script


Hi ,

I need to use the telnet command to connect from a unix machine to a windows server to run iside a shell script . Yes I know I shouldn't use telnet I should be using ssh more secure etc - but I am unable to go down that route for this user !

what i have is telnet <windows box> <<EOF
#where i need to pass in the password and username (Yuck)
host=xx.xx.xx.xx
port=xx
login=xxxx
password=xxx
#then i need to mount the window drive
cmd.exe /c net use X: /d
cmd.exe /c X: \\sharedserver\vol password /USER:username
#once i have this set up i execute the a bat file
cmd.exe /c win.bat
EOF


the win.bat is executing java from the X: drive

The telnet session exits right after it connects
get the follow message:

Connected to
Escape character is '^]'.
Connection to 10.0.2.52 closed by foreign host.

Any ideas would be much appreciated !

Thanks
 
Old 08-05-2009, 12:06 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,934
Blog Entries: 5

Rep: Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751
This is easier done with "expect" which is designed specifically for this.

You can download and install it on your Linux/UNIX system (how depends on which one you're using). It comes with a tool called autoexpect which allows you to start a session that records all the steps and responses to build the initial expect script. You likely have to massage it afterwards but it saves time on building from scratch.
 
Old 08-05-2009, 12:13 PM   #3
nano2
Member
 
Registered: May 2007
Posts: 100

Original Poster
Rep: Reputation: 15
Once i am in the windows machine i have to run commands
eg mappping the drive to a location
then running the java command

How can i achieve this from inside the expect script ?

Thanks
 
Old 08-05-2009, 12:52 PM   #4
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,934
Blog Entries: 5

Rep: Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751Reputation: 751
The expect script sends the commands to the session you open. It "expects" the prompt and sends the command when it sees it.

If you can do it manually via telnet then you'll be able to do it in expect.

An example script I use here:
Code:
#!/usr/local/bin/expect -f

log_file -a expect.out

set ip [lrange $argv 0 0]

spawn telnet $ip

expect "Configuration Login:"
send billybob\r

expect "Password:"
send bobspasswd\r

expect "4) Reboot"
expect "Command >"
send 4

expect "Reboot! Are you sure (y/n)?"
send y
sleep 5
On invocation I pass the IP of the device I'm telneting into. The script then does the telnet to that IP and waits for a prompt "Configuration Login:" When it gets it then it sends the user name "billybob". It then waits for the "Password:" prompt and sends the password "bobspasswd". It then waits to see menu option "4) Reboot" then waits for the prompt "Command >" after that and sends menu option "4". It then waits for the prompt for reboot and answers with "y". The sleep at the end is to keep it from exiting too soon. It drops the telent connection once it finishes.

Of course I've changed user name and password in the above to protect the guilty.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
perl inside a shell script baidym Linux - Newbie 2 05-05-2008 06:42 PM
arithmethic function inside shell script mystical dervish Linux - Newbie 3 08-21-2007 04:57 AM
embedded shell script from inside fortran TheBrick Programming 2 05-22-2006 08:23 AM
telnet windows m/c from unix shell script ann124 Programming 1 11-10-2004 01:26 PM
changing the user inside a shell script sanjith11 Programming 2 04-22-2004 07:44 AM


All times are GMT -5. The time now is 02:29 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration