Hi All,
I have a problem with auto telnet script, but I want to tell u something
a) I am only a member access on the server, so not able to access 'root' account
b) not able to install any software on server
3) On server, there is not install 'except'
The problem statement, all variables and given/known data:
I have to write a script, which is ran from A server, login/telnet to B server, do 'ls -lrt' and send to me the output via mail.
Relevant commands, code, scripts, algorithms:
I have only basic stucture....
** run from Server A
1) login to B
2) cd landing path
3) ls -l > a.txt
4) mail -s "Status" user@domain.com < a.txt
5) connection closed
The attempts at a solution (include all code and scripts):
telnet ServerB
echo user
sleep 1
echo password
sleep 1
cd landing_zone
ls -l > output.txt
mail -s "status" user@gmail.com < outout.txt
echo exit
Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Gautam Budh technical university
Uttar Pradesh
India
Mr. Nanhey Singh Inderprastha Engg Collegesyllabus/CS_IT_b_tech_3_4_year.pdf
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
The best way to do this is with expect, and you can record an expect script with autoexpect. There are expect features in bash, as well. However, your attempt to get by with sleep is OK if fragile. You need to collect all the stdout with () and pipe it into telnet stdin.
(
echo user
sleep 1
echo password
sleep 1
cd landing_zone
ls -l
) | telnet . . . | mailx . . . .
Yes, that's because you have to pipe things into telnet, it reads from standard input, it doesn't just magically take lines that happen to be below it.
ssh is much easier to automate than telnet. It comes with the ability to do noninteractive logins built in. You'll find passwordless ssh tutorials all over the internet. The gist is, you have to create a key for the server to recognize you with before noninteractive logins will work.
Once it does work you can do ssh username@host command and it will run that command
I honestly don't understand what in the above provided solution isn't working for you...no I don't think you should do it through 'ssh' if there is no specific reason to use Secure Socket.
Telnet should (and actually does) work just fine .
I've used it for something and it is working flawlessly.
I'll give you a step-by-step walkthrough/example below and we'll take it from there...
I'll enable telnet on my computer ,create a unixforum user,give him a password, run a command-line script, login in via telnet with the unixforum user , execute a 'pwd',an 'ls', exit from telnet and then disable telnet and delete the user.
let's go!!!
michnmi@OpenSolaris:~/Documents/Scripts/Unix.comForum$ svcs -a | grep telnet
disabled 15:29:03 svc:/network/telnet:default
michnmi@OpenSolaris:~/Documents/Scripts/Unix.comForum$ svcadm enable svc:/network/telnet:default
michnmi@OpenSolaris:~/Documents/Scripts/Unix.comForum$ svcs -a | grep telnet
online 15:29:20 svc:/network/telnet:default
michnmi@OpenSolaris:~/Documents/Scripts/Unix.comForum$ ls -l
total 3
-rwx------ 1 michnmi staff 89 2011-06-03 13:31 a.txt
-rw-r--r-- 1 michnmi staff 637 2011-06-03 13:41 my.log
michnmi@OpenSolaris:~/Documents/Scripts/Unix.comForum$ pfexec useradd -d /export/home/unixforum -m unixforum
UX: useradd: unixforum name too long.
80 blocks
michnmi@OpenSolaris:~/Documents/Scripts/Unix.comForum$ pfexec passwd unixforum
New Password:
Re-enter new Password:
passwd: password successfully changed for unixforum
michnmi@OpenSolaris:~/Documents/Scripts/Unix.comForum$ (sleep 1; echo "unixforum";sleep 1 ; echo "password"; sleep 2 ; echo 'pwd'; sleep 2 ; echo 'ls -la'; sleep 1) | telnet localhost
Trying ::1...
Connected to OpenSolaris.
Escape character is '^]'.
login: unixforum
Password:
Oracle Corporation SunOS 5.11 snv_151a November 2010
unixforum@OpenSolaris:/export/home/unixforum$ pwd
/export/home/unixforum
unixforum@OpenSolaris:/export/home/unixforum$ ls -la
total 11
drwxr-xr-x 2 unixforum other 8 2011-06-03 15:32 .
drwxr-xr-x 7 root root 7 2011-06-03 15:31 ..
-rw-r--r-- 1 unixforum other 280 2011-06-03 15:31 .bashrc
-rw-r--r-- 1 unixforum other 530 2011-06-03 15:31 .profile
-rw------- 1 unixforum other 16 2011-06-03 15:32 .sh_history
-rw-r--r-- 1 unixforum other 960 2011-06-03 15:31 local.cshrc
-rw-r--r-- 1 unixforum other 988 2011-06-03 15:31 local.login
-rw-r--r-- 1 unixforum other 927 2011-06-03 15:31 local.profile
unixforum@OpenSolaris:/export/home/unixforum$ Connection to OpenSolaris closed by foreign host.
michnmi@OpenSolaris:~/Documents/Scripts/Unix.comForum$
michnmi@OpenSolaris:~/Documents/Scripts/Unix.comForum$ svcadm disable svc:/network/telnet:default
michnmi@OpenSolaris:~/Documents/Scripts/Unix.comForum$ svcs -a | grep telnet
disabled 15:44:34 svc:/network/telnet:default