Pages

Showing posts with label ssh. Show all posts
Showing posts with label ssh. Show all posts

Monday, July 13, 2020

Python client for ssh


Sample code to access ssh by providing username and password 
#!/usr/bin/python3

#from __future__ import print_function

import os
import socket
from ssh2.session import Session


host = 'myserver'
user = os.getlogin()

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, 22))

session = Session()
session.handshake(sock)
session.userauth_password('oracle', 'mypassword')

channel = session.open_session()
channel.execute('date ')
print (channel.read())

#size, data = channel.read()   ## More managed reading like size etc 
#while size > 0:
#    print(data)
#    size, data = channel.read()

channel.close()



#print("Exit status: %s" % channel.get_exit_status())  ## Shows exit status
## pip install ssh2-python

Tuesday, November 3, 2015

Useful shell scripts used as alias for fedora and Linux machine

Useful shell scripts used as  alias for fedora and Linux machine

Here is list of useful shell scripts which used as alias in day to day work






1. connect script .

This list all the hosts to connect , we can pass it in commadline or by choice
if [ $# != 1 ]

then


echo 1.Xtest as root
echo 2.Xtest2 as oracle
echo 3.Xtest3 as oracle
echo 4.Xs5 as oracle
echo 5. Xcluster1 xx.yyy.zz.214 as oracle
echo 6. Xcluster2 xx.yyy.zz.215 as oracle
echo 7. X-portal as oracle
echo 8.X-pc2 as oracle

read ch

else

ch=$1

fi


case $ch in
1)ssh -l root xx.yyy.zz.139 ;;
2)ssh -l oracle xx.yyy.zz.140 ;;
3)ssh -l oracle xx.yyy.zz.141 ;;
4)ssh -l oracle xx.yyy.zz.147 ;;
5)ssh -l oracle xx.yyy.zz.137 ;;
6)ssh -l oracle xx.yyy.zz.138 ;;
7)ssh -l oracle xx.yyy.zz.132 ;;
8)ssh -l oracle xx.yyy.zz.145 ;;
*)echo Enter the right choice ;;
esac

To add in .bashrc
alias connect='/home/sterin/Templates/nm.sh'





2.Pack command : it will make tar file for folder or files

if [ $# != 1 ]

then

echo " usage pack <folderORfileName> "

exit
fi

dateformat=`date +%H%M_%d_%m_%y`


echo $1

folderName=`echo $1 | cut -f1 -d"/"`

echo $folderName

tar -cvf  "$folderName.tar"  "$1"




 



3. Starting browsers ,email client and other apps for day to day work


echo "starting firefox"

firefox "gmail.com" &

sleep 1
echo "starting chrome"

google-chrome "https://support.us.oracle.com/oip/faces/secure/srm/sr/SRQueue.jspx?mc=true" &

echo "starting thunderbird"

sleep 1
thunderbird &

echo "starting pidgin"

sleep 1
pidgin &

sleep 1

gnome-terminal &



sleep 1



gedit "FILE" &





4. Checking server status :

All the server details are menstion in one file . Script will check from the file


#Script:

if [ $# != 1 ]

then

echo "Parameter should pass like checkport <port> for specific port "
echo "OR checkport all for all port "

exit 0

fi

DIR=/home/oracle/latest_opatch/check_bug/list

if [ $1 = "all" ]

then

a=user_projects

else

a=$1

fi



if grep $a $DIR/yo.txt > /tmp/re

then

echo "below servers found from domain list file "
rm /tmp/uot

cat /tmp/re

else

echo "No server found for the keyword "

exit 0

fi

echo "<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>"

g=2

cat /tmp/re | while read LINE

do

g=`echo $LINE | awk -F"," '{print NF-1}'`
count=1

while [ $count -le $g ]
do
p=`echo $LINE | awk '{print $2}' | cut -f$count -d","`
if netstat -an | grep $p | grep LISTEN > /dev/null
then
echo " $p is running & pid is `netstat -anp | grep $p | grep LISTEN | awk '{print $7}' | tail -1` located at `echo $LINE | awk '{print $1}'` " >> /tmp/uot
fi
count=`expr $count + 1`
done

done

echo "<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>"

echo "status of the ports are "


cat /tmp/uot




## Format of the file having IP details
/domains/base_domain 7033,16201,16233
/domains/ps2 7032,16332,16232,
domains/stepsr3 7043,16332,16243


5. Copying boot.properties to every new domain

LOC=/home/oracle/latest_opatch/check_bug/boot.p

if ls -d ucm

then

echo " copying"

else

echo " go to domain dir and run the command "

exit 0

fi

cd servers/UCM_server1

cp -r $LOC/security .

Other required files

$ls -l /home/oracle/latest_opatch/check_bug/boot.p
total 8
-rwxr-xr-x 1 oracle oracle 201 Nov 12 2013 sec.sh
drwxrwxr-x 2 oracle oracle 4096 Nov 12 2013 security

$ ls -l /home/oracle/latest_opatch/check_bug/boot.p/security
total 4
-rw-rw-r-- 1 oracle oracle 36 Nov 12 2013 boot.properties

more /home/oracle/latest_opatch/check_bug/boot.p/security/boot.properties
username=weblogic
password=welcome1

Entry in .bashrc

alias sec='/home/oracle/latest_opatch/check_bug/boot.p/sec.sh'



6.Upload to FTP site :

It will be uploaded per defined FTP site:

HOST='HOSTADDRESS'
ftp -i $HOST <<END_SCRIPT

cd /ftp/anonymous/support/outgoing
#mkdir $1
#cd $1
put $FILE
quit
END_SCRIPT
echo "file copied to ftp URL is ftp://HOSTADDRESS/support/outgoing/$1 "
exit 3

Also you need create .netrc file in home directory for login with out entering password

Format of .netrc

default login hero password heropassord



7 . Run same command in multiple hosts from master server 

(Example to start all hadoop nodes from Master server )

SERVERS="172.31.27.144 172.31.27.145 172.31.27.146 "
# SSH User name
USR="hduser"



command="source ~/.bashrc; cd /usr/local/hadoop/sbin ; ./hadoop-daemon.sh stop datanode ; sleep 5 ;./yarn-daemon.sh stop nodemanager "



# connect each host and pull up user listing
for host in $SERVERS
do
echo "--------------------------------" 

echo "Server $host"

ssh $USR@$host  $command
echo "going to next hostname " 
done




8. Setting ec2 to hostname and using it


alias setec2='echo $1 > /tmp/ec2'
alias ec2='cat /tmp/ec2'

9. Fulltext search example :
To search one word all the files inside folder .

$1 will the keyword to search


alias fulltext='find . -exec grep "$1" {} \; -print'

Tuesday, October 6, 2015

Static IP is not coming up in Vmware after restoring from backup



Network is not working in VM ware . After the reboot guest OS is up but not able to access in network




Steps to Debug

1. Get the mac address from Virtual Box console , by clicking on settings and Network

2. Make sure that “Brigded” is selected

3. In Guest OS try these  steps

4. Verify the mac address by running

ifconfig and grep hardware

ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:5A:29:A5
inet addr:IP Bcast:IP Mask:255.255.252.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:718 errors:0 dropped:0 overruns:0 frame:0
TX packets:50 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:57933 (56.5 KiB) TX bytes:3516 (3.4 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:32 errors:0 dropped:0 overruns:0 frame:0
TX packets:32 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1882 (1.8 KiB) TX bytes:1882 (1.8 KiB)

Here mac address is 00:0C:29:5A:29:A5

ifconfig | grep Hwaddr
eth0 Link encap:Ethernet HWaddr 00:0C:29:5A:29:A5


it should match with step 1

if mac  address is not matching then follow step 10 .



5. Search in  dmesg for  etho

[root@OS ~]# dmesg|grep eth0
e1000 0000:02:01.0 eth0: (PCI:66MHz:32-bit) 00:0c:29:5a:29:a5
e1000 0000:02:01.0 eth0: Intel(R) PRO/1000 Network Connection
IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready
IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready

it should match mac from step 1

6.Try with disabling Ipv6

to disable Ipv6 edit : /etc/sysctl.conf and add net.ipv6.conf.all.disable_ipv6 = 1



7. Set NM_CONTROLLED=no in /etc/sysconfig/networking-script/if-eth0

8. Copy /etc/sysconfig/networking-script/if-eth0 to
             /etc/sysconfig/networking/devices/ifcfg-eth0

9.After restarting , issue resolved



Step 10 :
If mac address is not matching with VM IP and  ifconfig | grep Hwaddr

Change using below steps
 

  
# ifconfig eth0 down
# ifconfig eth0 hw ether 00:0C:29:5A:29:A5 
# ifconfig eth0 up
# ifconfig eth0 |grep 
 
Details : http://www.aboutlinux.info/2005/09/how-to-change-mac-address-of-your.html 







Thursday, September 17, 2015

How to login to unix machines with out password using SSH ?





1. Windows to UNIX server

    1. Create your private key using Putty ken gen



Click on Generate, then drag the mouse the save the key as private key

    1. Save the file as test.ppk
    2. Log on to UNIX server
    3. Cd .ssh ( under home dir )
    4. Create new file authorized_keys or edit the file
    5. Then add your private key their …. ( it is single line )

Example : ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAklJzYkmTbFKi6xE9o1kYqEU3YS+iOmY6aNOPm2QmljbigiziqI6w+zrckBXiWvABtRdp712mrkTUPA7Idmss+0lWGRtHdgxxfDQsvJj5bpm56K/4Cw83nSvj3TdtvwUiri7X4sje12Qlr71iECqDZFwKayGTcK0M0bLuZY4C+a8= rsa-key-20080102 - test


    1. Also add test at the end of the line
    2. Again open putty
    3. Enter Hostname
    4. Click on ssh then auth , give the location of ppk file




Now we can login without password!!!!!!!!!!!!!!


Few Troubleshooting

Server refused our key : Problem on Unix server , check the authorized_keys file

Correct login 


 

Then enter the password (Passphrase) for your private key




UNIX to UNIX (from server A to Server B)

On Server A
    1. Logon to .ssh folder on Server A
    2. Ran ssh-keygen -t rsa
    3. Give blank for all the details ( screen shot )

Generating public/private rsa key pair.
Enter file in which to save the key (/home/beausr/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/beausr/.ssh/id_rsa.
Your public key has been saved in /home/beausr/.ssh/id_rsa.pub.
The key fingerprint is:
98:97:fc:12:d3:2c:dc:7c:25:25:79:87:89:2f:f2:0d beausr@cic00152

    1. that will generate two files
-rw------- 1 beausr beausr 883 Jan 02 08:46 id_rsa
-rw-r--r-- 1 beausr beausr 225 Jan 02 08:46 id_rsa.pub
1.5 copy the id_rsa.pub to text file (it is single line )

    1. log on to server B
    2. log on to .ssh folder
    3. Create new file authorized_keys or edit the file
    4. Then add server A’s private key their …. ( it is single line )
Step 5 to 8 can be done in  single command 

ssh-copy-id -i ~/.ssh/id_rsa.pub <IP of server B>



Example : ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAklJzYkmTbFKi6xE9o1kYqEU3YS+iOmY6aNOPm2QmljbigiziqI6w+zrckBXiWvABtRdp712mrkTUPA7Idmss+0lWGRtHdgxxfDQsvJj5bpm56K/4Cw83nSvj3TdtvwUiri7X4sje12Qlr71iECqDZFwKayGTcK0M0bLuZY4C+a8= rsa-key-20080102 - userid@serverA


1.9 Save authorized_keys and log out
1.10 Now you can login from Server A to B without password (Only A to B will work)

Few Troubleshooting

authorized_keys is important one and edit it carefully .. There may be other users using this
authorized_keys : remove unnecessary spaces on the file

1. Your home directory ~, your ~/.ssh directory  should be on permission ( chmod 700 )
 drwx------
2.Your ~/.ssh/authorized_keys should be ( chmod 600 )
3.SE linux can cause problem in some linux version , so you might need to disable it