code_junkie

Spawning an interactive telnet session from a shell script

Nov 11th, 2011
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. $ cat ./telnetTest.sh
  2. #!/bin/bash
  3.  
  4. while read line
  5. do
  6. telnet $line
  7. done
  8. $
  9.  
  10. $ echo "testhost" | ./telnetTest.sh
  11. Trying 192.168.1.1...
  12. Connected to testhost (192.168.1.1).
  13. Escape character is '^]'.
  14. Connection closed by foreign host.
  15. $
  16.  
  17. #!/bin/bash
  18.  
  19. for HOST in `cat`
  20. do
  21. echo Connecting to $HOST...
  22. telnet $HOST </dev/tty
  23. done
  24.  
  25. #!/usr/bin/expect -f
  26. spawn telnet $host_name
  27. expect {
  28. "T0>" {}
  29. -re "Connection refused|No route to host|Invalid argument|lookup failure"
  30. {send_user "r******* connection error, bye.n";exit}
  31. default {send_user "r******* connection error (telnet timeout),
  32. bye.n";exit}
  33. }
  34. send "commandn"
  35. expect -timeout 1 "something"
  36.  
  37. #!/bin/bash
  38.  
  39. TTY=`tty` # Find out what tty we have been invoked from.
  40. for i in `cat hostnames.csv` # List of hosts/ports
  41. do
  42. # Separate port/host into separate variables
  43. host=`echo $i | awk -F, '{ print $1 }'`
  44. port=`echo $i | awk -F, '{ print $2 }'`
  45. telnet $host $port < $TTY # Connect to the current device
  46. done
  47.  
  48. $ for i in `cat hostnames.txt`; do telnet $i; done
  49.  
  50. for i in adele betty
  51. do
  52. ssh all@$i
  53. done
  54.  
  55. xterm -e telnet $host $port
  56.  
  57. #!/bin/sh
  58.  
  59. #SSG_details is file from which script will read ip adress and uname/password
  60. #to telnet.
  61.  
  62. SSG_detail=/opt/Telnet/SSG_detail.txt
  63.  
  64. cat $SSG_detail | while read ssg_det ; do
  65.  
  66. ssg_ip=`echo $ssg_det|awk '{print $1}'`
  67. ssg_user=`echo $ssg_det|awk '{print $2}'`
  68. ssg_pwd=`echo $ssg_det|awk '{print $3}'`
  69.  
  70.  
  71. echo " IP to telnet:" $ssg_ip
  72. echo " ssg_user:" $ssg_user
  73. echo " ssg_pwd:" $ssg_pwd
  74.  
  75. sh /opt/Telnet/Call_Telenet.sh $ssg_ip $ssg_user $ssg_pwd
  76.  
  77. done
  78.  
  79.  
  80. exit 0
  81.  
  82. #!/bin/sh
  83.  
  84. DELAY=1
  85. COMM1='config t' #/* 1st commands to be run*/
  86. COMM2='show run'
  87. COMM3=''
  88. COMM4=''
  89. COMM5='exit'
  90. COMM6='wr'
  91. COMM7='ssg service-cache refresh all'
  92. COMM8='exit' #/* 8th command to be run */
  93.  
  94.  
  95. telnet $1 >> $logfile 2>> $logfile |&
  96. sleep $DELAY
  97. echo -p $2 >> $logfile 2>> $logfile
  98. sleep $DELAY
  99. echo -p $3 >> $logfile 2>> $logfile
  100. sleep $DELAY
  101. echo -p $4 >> $logfile 2>> $logfile
  102. sleep $DELAY
  103. echo -p $5 >> $logfile 2>> $logfile
  104. sleep $DELAY
  105.  
  106. sleep $DELAY
  107. sleep $DELAY
  108. sleep $DELAY
  109. echo -p $COMM7 >> $logfile 2>> $logfile
  110. sleep $DELAY
  111. echo -p $COMM8 >> $logfile 2>> $logfile
  112. sleep $DELAY
  113.  
  114. exit 0
  115.  
  116. $> ./test3.sh
  117.  
  118. #!/bin/sh
  119. ( echo open hostname
  120. sleep 5
  121. echo username
  122. sleep 1
  123. echo password
  124. sleep 1
  125. echo some more output, etc. ) | telnet
Add Comment
Please, Sign In to add comment