Operating System - HP-UX
1836131 Members
1802 Online
110095 Solutions
New Discussion

Script to telnet & ftp without login id & password entry

 
Thin Lui Cheat
Occasional Contributor

Script to telnet & ftp without login id & password entry

Greetings,

My job requires me to frequently telnet/ftp to other servers to access or send data. However, some login ids & passwords are too long to be entered manually. I am thinking of setting up a script to telnet/ftp to the the sever. Meaning by entering a simple "telnet server", the system will be able to connect to the remote server without me entering the login id & password. I know I will need to create a text file to keep the login id & password.

Thanks for all the help
10 REPLIES 10
Jagadeesh Kumar
Advisor

Re: Script to telnet & ftp without login id & password entry

Hi,

Rlogin can be configured to achieve your requirement instead of telnet.
& rcp can be used instead of ftp.

This will avoid keeping passwords in a text file.

To enable rlogin work without prompting password for a user ( user1) from server1 to a user ( user2 ) on server2 you will need to do the following:

In Server1 :
Create a file .rhosts in the home directory of user1 with following contents
Server2 user2
/etc/hosts.equiv should contain
Server2

In Server2
Create a file .rhosts in the home directory of user2 with following contents
Server1 user1
/etc/hosts.equiv should contain
Server1

For more information refer to ?man hosts.equiv?

After the above configuration user1 from server1 can rlogin to server2 as user2 without password. ( same with rcp also )

Hope this helps.

Jagadeesh

Brendan Newport
Frequent Advisor

Re: Script to telnet & ftp without login id & password entry

I had a similar need some time ago, trying to connect occasionally to around 150-odd servers not running NIS, scattered around the country.

One of my HP-UX servers had the OpenView server distribution on it that comes with Expect. I created a text file with the hostnames in one column, and the root passwords in the other, then encrypted the file using crypt. Then a Bourne Shell script prompted for the hostname as arg 1, and arg 2 as either telnet or ftp, then prompted for the password to decrypt the password file, grep'ped for the hostname in the password file and took the 2nd field with the password, and then passed both hostname and password, and connection method as arguments to an Expect script that did either an ftp or telnet as required...logged-in for me as root and then handed-off.

To be reasonably safe you could encrypt the text file several times with different passwords to increase its robustness.
"It doesn't have to be like this. All we need to do is make sure we keep talking"(Dave Gilmour)
Magdi KAMAL
Respected Contributor

Re: Script to telnet & ftp without login id & password entry

Hi Thin,

You better may use the remote copy command "rcp" which didn't ask for anything if you configure your .rhosts file.

example :
Consider server1 ( local ) and server2 ( remote )

1. Edit the file ".rhosts" in the home directory of the user who will issue the remote command "rcp" on the remote server "server2".

2. Add the following line:
server1 +

3. set permissions on that file ".rhosts" to 400 :

#chmod 400 .rhosts

4. On your local server ( server1 ) issue the command :

#rcp -p -r server2:/home/user1/ex01 remoteEx01

This will copy recursively ( option "-r" ) the directory /home/user1/ex01 on node server2 to server1 local directory with the name remoteEx01.

Notice : "-p" option will preserve ( duplicate ) modification times and modes ( permissions ) of source files, ignoring the current setting of your actual umask file creation mode mask. If this option is specified, rcp preserves the sticky bit only if the target user is superuser.

Synatxe:

#rcp -p -r :/pathDir1 :/pathDir2

sourceServer or destinationServer could be exclusively optional.

Magdi


Thin Lui Cheat
Occasional Contributor

Re: Script to telnet & ftp without login id & password entry

Hi Guys,

Thanks for your help. However, the other server I am accessing to is a customer server and I cant do any editing in there. Well the idea of rlogin is good. But if I managed to configure the servers, do I need to borther about login id & password ?

Maybe a shell script is enough for me. Can anyone shows me a sample script to read in the text file with the login id & password ??
Bill Hassell
Honored Contributor

Re: Script to telnet & ftp without login id & password entry

Since you said that you are able to login to the remote servers to send or receive files, you can create the .rhosts locally and simply ftp the file to your $HOME directory on the remote server. Make sure that .rhosts contains the IP address of your local system and your login ID on the local system. This tells the remote system to trust you is you are connecting from this IP address and logged in as the named user.

Also, make sure the permission on .rhosts is 600! Only the owner can read or write the file. Test with: remsh remote-system pwd

For ftp, it is actually easier: create a file called .netrc in your local system. Change the pwermissions to 600 (very important) and read the man page for ftp which describes the format. Now you can simply type: ftp remote-system and you are immediately logged in.


Bill Hassell, sysadmin
Thin Lui Cheat
Occasional Contributor

Re: Script to telnet & ftp without login id & password entry

Hi Guys,

it works with the rlogin staff but I still need to update the /etc/hosts.equiv to get things going. However, can someone provide me a solution by writing a script which reads in a text file containging the login id and password for telnet & ftp usage ??

Thanks
Kherwin Cheng Chua
New Member

Re: Script to telnet & ftp without login id & password entry

for ftp, you can have a file called .netrc in your home directory. The format inside the file is like this:

machine host1 login user1 password pass

The words "machine", "login", and "password" are constants. Replace the word after each constant with the appropriate values. Make 1 line/entry for each host that you want to be automatically logged into. Everytime you ftp a host, your system will first check the ~/.netrc file if you have configured auto login for that host. If you have, then your system will automatically log you in! :)

Hope that helps!

Cheers!

Kherwin :)
Imagine what we'll know tomorrow!
Mark van Hassel
Respected Contributor

Re: Script to telnet & ftp without login id & password entry

Hi,

For ftp it is quite simple using a HERE document:

for HOST in `cat hostlist`
do
ftp -v -n $HOST << HERE
user username passwd
put file
bye
HERE
done

offcourse you can use variables for username and passwd.

For telnet it is a bit trickier since telnet expects a tty port. You can use "expect" or the following example which we use to change the passwd of a user on multiple systems:

for HOST in `cat hostlist`
do
( sleep 2
echo root
sleep 2
echo ${rootpasswd}
sleep 2
echo "passwd ${username}"
sleep 2
echo p # when trusted system
sleep 2
echo "${userpasswd}"
sleep 2
echo "${userpasswd}"
sleep 2
echo exit
sleep 5 ) | telnet $HOST
done

The sleeps are necessary to slow the echo statements.

Offcorse other commands than "passwd" can be issued.

Hope this helps
The surest sign that life exists elsewhere in the universe is that none of it has tried to contact us
Ralf Hildebrandt
Valued Contributor

Re: Script to telnet & ftp without login id & password entry

The correct solution is to use rsync, preferrably over ssh:

This offers compressed, incremental, encrypted filetransfer without the need to specify passwords at all:

http://www.securityportal.com/cover/coverstory20000814.html
Postfix/BIND/Security/IDS/Scanner, you name it...
surya_asl
New Member

Re: Script to telnet & ftp without login id & password entry

hi Mark van Hassel .thank u for your solution . can i have your email address?

mine is suryabanerjee2002@yahoo.co.in