Can python read Servernames / IP_addresses from a file rather than giving Servernames One by One, i did searched but did not find anything about it. I have below Script but this only takes the Single IP address or Server_name at a time. Wold appreciate any inputs..
This is for Linux and Pyhton version 2.6
#!/usr/bin/env python
import sys
sys.stderr = open('/dev/null') # Silence silly warnings from paramiko
import paramiko as pm
sys.stderr = sys.__stderr__
import os
class AllowAllKeys(pm.MissingHostKeyPolicy):
def missing_host_key(self, client, hostname, key):
return
HOST = '192.1.10.1'
USER = 'karn'
PASSWORD = 'mypass'
client = pm.SSHClient()
client.load_system_host_keys()
client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
client.set_missing_host_key_policy(AllowAllKeys())
client.connect(HOST, username=USER, password=PASSWORD)
channel = client.invoke_shell()
stdin = channel.makefile('wb')
stdout = channel.makefile('rb')
stdin.write('''
hostname
uname -a
uptime
who
exit
''')
print stdout.read()
stdout.close()
stdin.close()
client.close()
Note: I want python to read the file which contains either Servers names or ip address in a squence .. example as below..
# cat ServerList.txt
Server1
Server2
Server3
......
OR
# cat ServerList2
192.168.1.1
192.168.1.2
....
Below code is working now..appreciate your help guys
#!/usr/bin/python
import fileinput
import sys
import paramiko as pm
import os
sys.stderr = open('/dev/null') # Silence silly warnings from paramiko
sys.stderr = sys.__stderr__
class AllowAllKeys(pm.MissingHostKeyPolicy):
def missing_host_key(self, client, hostname, key):
return
with open('/home/karn/PYTHN/host.txt', 'r') as f:
for host in f:
HOSTN = host.rstrip()
USER = 'myuser'
PASSWORD = 'mypass'
client = pm.SSHClient()
client.load_system_host_keys()
client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
client.set_missing_host_key_policy(AllowAllKeys())
client.connect( HOSTN, username=USER, password=PASSWORD )
channel = client.invoke_shell()
stdin = channel.makefile('wb')
stdout = channel.makefile('rb')
stdin.write('''
hostname
lsb_release -a
exit
''')
print stdout.read()
stdout.close()
stdin.close()
client.close()
You could use a csv
file with 3 columns, one for HOST
, USER
and PASSWORD
. Here are the official examples . In your code you'd for loop over the rows and extract the 3 fields in each row like in the example.
Here is an example for you, where I'd get Hostname and IP addresses from /etc/hosts :
with open ('/etc/hosts', 'r') as hosts:
for line in hosts:
l = line.split()
if len(l) > 1:
IP = l[0].rstrip()
print IP
Hostname = l[1].rstrip()
print Hostname
Using with open
makes sure that your file gets handled and closed properly by python.
Then read each line. It comes as a string, split() will return an array. Since I read from /etc/hosts, my first element is the IP address, second is the hostname. I use rstrip to make sure no extra characters get in the way.
From there you need to adapt whatever your file looks like.
after opening your file and reading each line, you can also try and extract by using a regex.
if you know your ip range:
matchIP = re.compile(r"192\.168\.1\.\d{0,225}$") #
and same with your server names
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.