This code writes google.de has address 216.239.34.117 to the shell
import os
os.system("host google.de")
Now i want to save the IP-Address into a string variable, but i can`t figure out how i read from the command line. Or is there even an easier way to get the IP-Adress to a variable?
Thanks and Greetings,
Alex
Why won't you use socket library instead of os?
import socket
host_ip = socket.gethostbyname('google.de')
print(host_ip)
Related
i have a .properties file that statesserver-ip= and i made a python program that identifies my ipv4, but i want it to go to the proprieties file through the same program and get server-ip=(my ipv4)
import socket
socket.gethostbyname(hostname)
I've tired many diferent types of solutions, please a need help
If .properties doesn't have sections (usually doesn't) you could check this answer: - link
Else, you could use configParser:
import configparser
config = configparser.ConfigParser()
config.read('filename')
host = config['section-name']['server-ip']
I'm using subprocess within a python script to send variables into a bash script which is designed to send emails.
My python code:
import re
import subprocess
message="https://example.com/"
subprocess.run(["./send_email.bash",message])
My "./send_email.bash" script:
#!/bin/bash
body=$1
echo "$body" | ssmtp example#gmail.com
It keeps sending a blank email. However, if I use a string which doesn't come from subprocess, the email contains it. There may be a better way of doing this, but the point of my post is to understand why this doesn't work. What perplexes me is that the variable sent from subprocess may be appended to a file. But, it cannot be recognized by loops or "if" expressions. etc.
Thank you!
I should have been more specific. The "message" within my python code was an email address: "https://example.com/". It appears that when I remove the "https://" from the string, it works perfectly fine!
I have been unsuccessful in finding a way to read a file from a remote server over ssh and then writing a similar file in the same way. Paramiko doesn't seem to work with 3.5 and i'm not sure what else i can do.
Example of what i'm trying to do:
from shutil import copyfile
copyfile('10.1.1.5:v3/ec/s//01_inventory.txt', '10.1.1.5:v3/ec/s//01_inventory_Bkup.txt')
You could use scp I suppose (https://ss64.com/bash/scp.html). You would have something like this to replace your copyfile call:
import subprocess
subprocess.call("scp 10.1.1.5:v3/ec/s/01_inventory.txt 10.1.1.5:v3/ec/s/01_inventory_Bkup.txt", shell=True)
You could also use scp to copy to your local host, modify it as you would normally and then replace the original.
I have the following program where I'm redirecting an output on terminal to a text file :
import telnetlib
import time
import string
tn = telnetlib.Telnet("192.168.1.102")
print "Attempting Telnet connection...."
tn.read_until("login: ")
tn.write("root1\n")
time.sleep(1)
tn.read_until("Password:")
tn.write("\n")
tn.write("\n")
tn.read_until("root1#mypc:~$")
tn.write("su\n")
tn.read_until("root#mypc:/home/root1")
tn.write("\n")
print "Telnet logged in successfully....\n"
tn.write("head /proc/meminfo > /home/a.txt")
tn.write("\n")
I would like to copy the textual contents of this file to a buffer variable and process it. That is, I don't want to read from the console/terminal. I just want to redirect the output to a text file and then read from the text file. Does telnetlib offer any direct function to achieve this or any alternate way to do the same?
TELNET protocol is more or less a distant terminal emulation. It offers no file transfert facilities because other protocols deal with that. That means that once you have written the file on remote system, you will have to display it with cat and store the output of the cat command.
Alternatively you could use a protocol meant for file transfert like FTP, RSYNC, SFTP, FTPS, etc. to download the remote file. Just use the one that is accessible on your remote system.
EDIT this code reads from a local file on the remote host
Please try the following code, which assume that after you execute your command, you get the following string: "root#mypc:/home/root1"
tn.write("cat /home/a.txt")
tn.write("\n")
data = ''
while data.find("root#mypc:/home/root1") == -1:
data = tn.read_very_eager()
print data
I have a text file with a list of about 50 hostnames and I am looking to script a way to run through them to get each associated IP address in the Command Prompt.
I thought pasting the hostname list in to the following code might be the easiest way but socket.gethostbyname will take no more than 1 argument at a time.
import socket
socket.gethostbyname("***hostnames***")
Is there a way to work around this argument issue, or is there a way to have the hostnames read from the textfile?
The easiest work around is to pass a filename and iterate through it:
#!/usr/bin/python
import sys
import socket
file_nm = sys.argv[1]
with open(file_nm, 'r') as f:
for host in f:
print socket.gethostbyname(host.strip())